Purdue2479
Registered User.
- Local time
- Today, 08:41
- Joined
- Jul 1, 2003
- Messages
- 52
I am using the below code to import .csv files from a directory and it is working fine. But, I would like for it to prompt me when there is an import error (ex. field names don't match), allow me to skip the file, and then continue with the import. Any suggestions on how to do this would be appreciated. Thanks
I would also be interested in knowing how to show the time it took to import in the msgbox.
I would also be interested in knowing how to show the time it took to import in the msgbox.
Code:
Public Sub subImport()
On Error GoTo Err_subImport
Dim stDocName As String
Dim fs As FileSearch
Dim ifn As String
Dim sql As String
Dim today As String
Dim fso As Scripting.FileSystemObject
Dim oktogo As Boolean
Dim specname As String
Dim repdate As String
Dim myfile As Scripting.TextStream
Dim i As Long
Dim y As Integer
Dim ShortFn As String
'Dim specname As String
'specname = "Import Specs"
DoCmd.SetWarnings False
sql = "DELETE FROM tbl_temp_Import_2006"
DoCmd.RunSQL sql 'Empty Temp Table
DoCmd.SetWarnings False
oktogo = False
ifn = "C:\Projects"
Set fs = Application.FileSearch
With fs
.LookIn = ifn
.FileName = "*.csv"
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
For i = 1 To .FoundFiles.Count
ShortFn = Right(.FoundFiles(i), Len(.FoundFiles(i)) - InStrRev(.FoundFiles(i), "\"))
DoCmd.TransferText acImportDelim, , "tbl_temp_Import_2006", .FoundFiles(i), True
'subArchive .FoundFiles(i)
y = y + 1
Next i
Else
MsgBox "Please ensure that the source file is present and try again" & vbCr _
& "Required file location: " & vbCr & ifn, vbExclamation + vbOKOnly, "Input File Missing"
Exit Sub
End If
End With
MsgBox "Import complete. " & y & " files Imported", vbOKOnly + vbInformation, "Import Complete"
Exit_subImport:
' Turn warning messages back on
DoCmd.SetWarnings True
Exit Sub
Err_subImport:
MsgBox Err.Description
Resume Exit_subImport
End Sub