4

I have a number of collections and objects set up that have a one-to-many relationship that is four levels deep. CollectionA contains a number of CollectionB objects and CollectionB contains a number of CollectionC objects and CollectionC contains a number of Content objects.

After the user has spent time populating all of these collections and objects with the necessary data, they need to be able to save all of the properties of each object and then read the output file and start where they left off.

I know this is a somewhat broad question so I'm looking to see which direction I should head in my research on how to do this. Right now I'm most concerned with the File I/O and I'm thinking of two different routes.

It's an excel project, so I was thinking I could loop through all of the Collections and objects from the top down and write their values to a spreadsheet and then loop through the spreadsheet rows and output those to a delimited text file. Then do the reverse to read the text file back in - from text to spreadsheet, then from spreadsheet into collections and objects.

Maybe the other option would be to bypass the spreadsheet and use a TextStream? Would that be possible and would it be the way to go if I'm planning on converting this VBA application to standalone VB.NET sometime in the future?

Community
  • 1
  • 1
cmerrell
  • 503
  • 3
  • 7
  • 16
  • 1
    Have you ruled out a relational database for some reason? It sounds like all of this work maintaining collections of collections with one-to-many relationships would be neatly bypassed by using a database to store the data. – mwolfe02 Mar 08 '11 at 16:57
  • @mwolfe02: The main problem is that I don't know anything about databases. Do you have any suggestions on where I can look for more information? Also, at this point I've got the gui and computational part of the program working the way I want, do you think it would take much time to rewrite everything using a database? and can I still use excel? – cmerrell Mar 08 '11 at 17:27
  • There's definitely a learning curve if you currently know nothing about databases. If you plan on doing this sort of thing for several future projects, it's probably worth the investment of time to learn about databases. If this is just a one-time thing, you may be better off sticking with what you know. If you're interested, I'd suggest starting with http://allenbrowne.com/Tips.html. It has a good mix of beginner through advanced topics dealing specifically with MS Access (the "full-stack" database solution included in MS Office). – mwolfe02 Mar 08 '11 at 17:46
  • For integration with Excel, this book (http://www.amazon.com/Microsoft-Excel-Access-Integration-Office/dp/0470104880) looks like what you would be most interested in. TechRepublic.com offers a free download of a sample chapter from the book, "Chapter 3 Data Access from Excel VBA", which may be all you need for this project (http://www.techrepublic.com/downloads/integrate-microsoft-excel-and-access-with-vba-to-create-sophisticated-applications/173274). – mwolfe02 Mar 08 '11 at 17:49
  • why don't you just write the collections to worksheets within the workbook, then load the collections when the workbook is opened? Then all the data is contained in the same program. – Fink Mar 08 '11 at 21:45
  • If you don't have much data to store, @Fink's solution is probably the simplest. You can use hidden worksheets if you want to prevent the users from seeing that working data. – mwolfe02 Mar 08 '11 at 22:34
  • Storing four-dimensional data on two-dimensional sheets? Unless you really have very, very few elements per collection layer, this will quickly become unmanageable... – Jean-François Corbett Mar 09 '11 at 13:08

1 Answers1

4

One good option is to store your collections as XML files. It will be relatively easy to add or read individual elements afterwards; and you'll save yourself from the indexing nightmare that will inevitably occur if you try to arrange your 4+ dimensional data onto a two-dimensional spreadsheet. They're text files, so human readable; if file size becomes a problem, just zip them.

You'll just have to devise an appropriate format, i.e. tag names etc., see example at the end of this answer.

EDIT: If having the data packaged into your Excel workbook is really important to you, then you can just slap the xml code onto one of the sheets and store it there... I've done this before.

Create an XML document as shown below:

Set xmlFatAssCollectionA = CreateObject("Msxml2.DOMDocument.6.0")

Then edit it: loop through your collections, appending each element as a child of the larger collection, as appropriate. When done, save the XML document.

xmlFatAssCollectionA.save("C:\MyDir\MyCollection.xml")

Documentation: http://msdn.microsoft.com/en-us/library/ms756987%28v=VS.85%29.aspx

You can use XPath to access the required nodes/elements. XPath tutorial

Here's what your resulting xml file might look like. I only added one collectionB object, and omitted the whole collectionA layer, but you get the idea.

<?xml version="1.0" encoding="ISO-8859-1"?>

<bObject>
  <cObject>
    <contentObject>
      <title lang="da">Kejserens nye klæder</title>
      <author>H.C. Andersen</author>
      <price>priceless</price>
    </contentObject>
  </cObject>
  <cObject>
     <contentObject>
      <title lang="en">Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </contentObject>
    <contentObject>
      <title lang="fr">Les Schtroumpfs</title>
      <author>Some person</author>
      <year>1985</year>
    </contentObject>
  </cObject>
</bObject>
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
  • Francois Corbett: Interesting suggestion, Can excel with vba write to xml and then read and parse it as well? I'm not sure how Xpath fits into the equation. I need this to standalone without installing additional libraries etc. I mean, it needs to work with just the "vanilla" install of excel. In the mean time I have figured out a way to write the data to a spreadsheet, save it as a csv file and then import it back in. – cmerrell Mar 09 '11 at 16:16
  • 1
    @cmerell: Yes, that's the whole point. The two command examples above are indeed VBA. You create this Msxml object, which has all these built-in methods that will read, parse, save, etc. the xml for you. It will indeed be standalone. As for XPath, it's just a way to refer to the various elements in your xml "database" -- a bit like a Windows path ("C:\mydir\myotherdir\myfile.dat") is a way to refer to a specific file on your disk. You just have to learn where to put the : and \ and . characters. XPath is sort of analogous (a bit more complicated, of course). – Jean-François Corbett Mar 10 '11 at 09:04
  • Is there a way to store the xml file inside of the .xlsm/.xlsx zip so that the data can be packaged with the Excel file when you send it around to people? – Jay Killeen Jun 09 '16 at 05:14
  • @JayKilleen: Yeah, I had the same idea once and tried it, but then Excel wouldn't open the workbook file. Upon opening, Excel performs a check to see whether the zip archive only has expected content. If not, it reports the file as corrupted. Presumably to prevent the spread of malicious content. – Jean-François Corbett Jun 09 '16 at 08:29
  • Cheers for letting me know. I'm searching for a better way then storing data in worksheets and hiding/locking them down. Doesn't seem to be a good way so probably should just stick to what is easy. Great to know you have tried it though. Cheers! – Jay Killeen Jun 09 '16 at 08:31