I need to import multiple text (csv) files into multiple tables in access. At the moment I do it one by one using External data, browsing to the file, importing it, setting specifications and saving it as a table. It's tedious. I've found plenty of code which let me import multiple files to multiple tables. My issue is that each import has different specs.
I have this code which works but it still means that i have to select table one then go back and select table two then go back and select table 3 etc.
Is there a way I can loop through the directory, and import the files with their specific spec? Table names never change, spec for each table never changes. They need to be separate tables. Any and all help gratefully recieved!
Private Sub Command1_Click()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
'In case we have to run everything more than once, make sure table is clear before appending data
DoCmd.SetWarnings (False)
DoCmd.RunSQL "DELETE * FROM " & "TABLEONEHeader"
'Explorer window opens
With Application.FileDialog(msoFileDialogFilePicker)
'Only show CSV files
.Filters.Add "Text Files", "*.csv", 1
.Title = "Select TABLE ONE..."
'Prevent user selecting multiple files.
.AllowMultiSelect = False
'If the users selects a file use it, otherwise show an error message
If .Show Then
strFile = .SelectedItems(1)
Else
MsgBox "You haven't selected a file.", vbCritical
End If
End With
'Import data to table using specific specifications
DoCmd.TransferText acImportDelim, "TABLEONE", "TABLEONEHeader", strFile, True
MsgBox "TABLEONEHeader has been imported"
'move on to next file
'In case we have to run everything more than once, make sure table is clear before appending data
DoCmd.SetWarnings (False)
DoCmd.RunSQL "DELETE * FROM " & "TABLETWO"
'Explorer window opens
With Application.FileDialog(msoFileDialogFilePicker)
'Only show CSV files
.Filters.Add "Text Files", "*.csv", 1
.Title = "Select TABLE TWO file..."
'Prevent user selecting multiple files.
.AllowMultiSelect = False
'If the users selects a file use it, otherwise show an error message
If .Show Then
strFile = .SelectedItems(1)
Else
MsgBox "You haven't selected a file.", vbCritical
End If
End With
DoCmd.TransferText acImportDelim, "TABLETWO", "TABLETWOHeader", strFile, True
MsgBox "TABLETWO has been imported"
'move on to next file