views:

116

answers:

2

Hi, I know I'v done stuff like this before, but I'm trying to find the most efficient way of doing this.

Basically, I want to store some addresses in a text file and then read specific portions of the file, based on group membership. I've done all of the group membership stuff so I don't need any help for that.

But I'm not sure if I should use a plain txt file or an INI file? The thing is, the post addresses are in two or three lines and I need linebreak. I tried using a plain txt file, but I couldn't manage to get a line break correctly.

So INI files would be preferable?

The INI file could look like this:

[London]
Address 1
Postbox 3245
58348 London

[Copenhagen]
Address 2
Postbox 2455
5478347 Copenhagen

I'm not quite sure if this is possible in an INI file though, perhaps I need to name each line as well. OR, I could possibly use a plain txt file and search for the word [london] and then read each line until there's a line break. Then store all of those lines in a variable that I'll pass along?

How would you guys solve this?

+1  A: 

You could store the addresses in one line and use a special character, for example an underscore, to indicate a line break. When you read the address, you just need to replace the special character with a line break.

[London]
Address = "Postbox 3245_58348 London"

[Copenhagen]
Address = "Postbox 2455_5478347 Copenhagen"

That allows you to store addresses with more lines or without a postbox line, as well. In my experience, information like "our addresses always have exactly two lines and the first one is always a postbox" is very often incorrect...

Treb
Try `Replace(ReadIni, "_", vbCrLf)`, without any quotes around `vbCrLf`.
Treb
+1  A: 

I would probably use CSV file instead where each row will represent a country.

Country,Address1,Address2,Address3,Address4
London,Address 1,Postbox 3245,58348 London
Copenhagen,Address 2,Postbox 2455,5478347,Copenhagen

If you can easily identify your data then you could probably have more descriptive column names (i.e. Street1, Street2, Town, Postcode, etc.).

This file format is also easy to read since you only read one line of the input file at a time and split it using something like

aAddress = split(sLine, ",")

To make it even easier to work with you could use dictionary object and use country as a key and array as a value

'sLine should be read from input file'
sLine = "Copenhagen,Address 2,Postbox 2455,5478347,Copenhagen"

'Create dictionary for addresses'
Set dic = CreateObject("Scripting.Dictionary")

'Split line into array'
aAddressParts = Split(sLine, ",") 

'Remove the first element of the array'
sValues = Mid(sLine, InStr(sLine, ",")+1)
aValues = Split(sValues, ",")

'Add new entry into dictionary'
dic.Add aAddressParts(0), aValues

'Usage'
MsgBox "Address for Copenhagen: " & vbNewLine & _
    Join(dic("Copenhagen"), "," & vbNewLine)

Thanks, Maciej

Maciej Zaleski
Good idea :) The whole idea about this all is that a client has to be able to edit the data easily, in a readable format. CSV can be opened with Excel. Great tip! :)
Kenny Bones