Dalt said:
It's not that I don't want to do table joins or don't know how, i'm just
trying to learn a good method of combining lists through sheer code. In
other words, what coding principals are at work when several tables are
joined together?
Ok, great. Now you've clarified your goals and what you're trying to
accomplish here, and put it in the context that you just trying to learn and
thinking of some different techniques to use here, then we can help you.
Remember when people come here to ask for help, and say they have to do
something a particular way (and likely a wrong way), if you don't explain
why you want to do something that different way, then we feel obliged to
give you good advice as to the correct approach. So you ask for medical
advice on how to gain weight when weight gain is not needed, you not
likely get anyone willing to give you that bad advice. So, people here are
naturally against giving
advice to do things the wrong way.
So lets take a crack at this problem now that we all realize it would be
easier, less work and the code likey would run faster if we used
sql quires. (and, further since the data would be in a table, then we
could easily export the data back out to a text file.
For one, I guess it's much faster to combine lists when they're sorted
alphabetically so you can do a binary search for items within an array
instead of standard for loop. So I'm guessing a good start to manually
joining 3 "tables" would be to sort them first.
Actually I would use a collection. The reason why it's better to use a
collection is that you can pull a value out of this dynamic structure based
on a key value without having to use a for/next loop to scan the data.
Were are also going to assume that for each of the three files is no
duplicate names. (each name appears once in each of the 3 files).
The following code would join the data
together. Were also going to assume that each file has two columns and is
comma delimited.
Name,Value
Albert,Steel Worker
Joe,Plumber
The code would look like:
Public Sub MyTest()
Dim cList As New Collection
Dim intFile As Integer
Dim strOneLine As String
Dim strName As String
Dim strValue As String
Dim itemData(1 To 3) As String
' read in 1st file (name + occupation
intFile = FreeFile
Open "c:\datatest\test1.txt" For Input As intFile
Do While EOF(intFile) = False
Line Input #intFile, strOneLine
strName = Split(strOneLine, ",")(0)
strValue = Split(strOneLine, ",")(1)
itemData(1) = strName
itemData(2) = strValue
cList.Add itemData, strName
Loop
Close intFile
' 2nd file (name + Salary)
intFile = FreeFile
Open "c:\datatest\test2.txt" For Input As intFile
Do While EOF(intFile) = False
Line Input #intFile, strOneLine
strName = Split(strOneLine, ",")(0)
strValue = Split(strOneLine, ",")(1)
cList(strName)(2) = strValue
Loop
Close intFile
' 3nd file (name + HireDate)
intFile = FreeFile
Open "c:\datatest\test3.txt" For Input As intFile
Do While EOF(intFile) = False
Line Input #intFile, strOneLine
strName = Split(strOneLine, ",")(0)
strValue = Split(strOneLine, ",")(1)
cList(strName)(3) = strValue
Loop
Close intFile
' at this point, you can display the results:
For Each itemData In cList
Debug.Print itemData(1), itemData(2), itemData(3)
Next
End Sub
The above is not tested code, but it should quite well do the trick...
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
(e-mail address removed)
a primary key to plus the new one colu
so why would likely be fastidious SQL and table joins, the following code to
do your Perot would look something like the following
forehand of pull lightens out of the list without having to do a search