I've started writing some code and I'm trying to use the docmd.TransferDatabase method to import the tables.
My code thus far loops through all .dbf files in a folder and uses the file names to create my union query.
But in order for the query to work I need to import the tables at the same time. I am having trouble getting the TransferDatabase syntax right.
Here is what I have so far:
Code:
Sub AddClimate()
Dim MyFile As String
MyFile = Dir("F:\Lorna\BroadscaleMonitoring\DerivedData\Climate\1961_1990\OutputTables\T*.dbf")
Dim FileName As String
Dim LoopNum As Integer
LoopNum = 1
Dim Query As String
Dim QryDef As QueryDef
Set QryDef = New QueryDef
Do While MyFile <> ""
'Get the name of the file without the extension
FileName = Left(MyFile, 8)
'Add the file name to the query
' If this is the first time through, no "union" is necessary"
If LoopNum = 1 Then
Query = "SELECT UCID, MEAN FROM " & FileName
' If this is not the first time through, then "union" must be added
ElseIf LoopNum > 1 Then
Query = Query & " UNION SELECT UCID, MEAN FROM " & FileName
End If
'Import the table
docmd.TransferDatabase (acImport,"dBase IV",MyFile,acTable,FileName,FileName,False,False)
MyFile = Dir()
LoopNum = LoopNum + 1
Loop
Debug.Print Query
End Sub