Hi folks
I am trying to import multiple csv files into a temp table, here is the code i am using
Private Sub ImportMultiple()
Dim strFile As String
Dim strFileList() As String
Dim intFile As Integer
Dim filename As String
Dim path As String
Dim Response As String
Dim myStrFilter As String
Dim strInputFileName As String
Dim strSQL As String
path = "c:\Database\"
strFile = Dir(path & "*.csv")
'Lists each file in that directory with .csv extension and stores filename in an array
While strFile <> ""
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
strFile = Dir()
Wend
If intFile = 0 Then
MsgBox "No files found"
DoCmd.SetWarnings True
Exit Sub
End If
'Imports each file
For intFile = 1 To UBound(strFileList)
filename = path & strFileList(intFile)
DoCmd.TransferText acImportDelimi, "ImportSpec", "tblImport", path & strFileList(intFile), True
DoCmd.RunSQL ("UPDATE tblimport SET tblimport.f1 = " & Chr(34) & Replace(strFileList(intFile), ".csv", "") & Chr(34) & " WHERE tblImport.f1 is null ")
Next intFile
End Sub
However when i run the code i get the runtime 3011 error - however the error message displays one of the filenames I am trying to import, so it must see the files in the directory.
I have highlighted in red the line it selects when i hit the debug button.
Any Ideas???
I am trying to import multiple csv files into a temp table, here is the code i am using
Private Sub ImportMultiple()
Dim strFile As String
Dim strFileList() As String
Dim intFile As Integer
Dim filename As String
Dim path As String
Dim Response As String
Dim myStrFilter As String
Dim strInputFileName As String
Dim strSQL As String
path = "c:\Database\"
strFile = Dir(path & "*.csv")
'Lists each file in that directory with .csv extension and stores filename in an array
While strFile <> ""
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
strFile = Dir()
Wend
If intFile = 0 Then
MsgBox "No files found"
DoCmd.SetWarnings True
Exit Sub
End If
'Imports each file
For intFile = 1 To UBound(strFileList)
filename = path & strFileList(intFile)
DoCmd.TransferText acImportDelimi, "ImportSpec", "tblImport", path & strFileList(intFile), True
DoCmd.RunSQL ("UPDATE tblimport SET tblimport.f1 = " & Chr(34) & Replace(strFileList(intFile), ".csv", "") & Chr(34) & " WHERE tblImport.f1 is null ")
Next intFile
End Sub
However when i run the code i get the runtime 3011 error - however the error message displays one of the filenames I am trying to import, so it must see the files in the directory.
I have highlighted in red the line it selects when i hit the debug button.
Any Ideas???
Last edited: