Hi,
I have the following piece of code for importing the .csv files from a selected folder and then renaming them into 'imported & filename' if succesful and 'failed & filename' if import failes for whatever reason (bad formating, etc.).
The problem is that the first time it encounters a 'bad' file after another, instead of going again to the Error handler, the command
gives me the default MS Access error, namely 2391. I would like it to go the the Error_handler again ang follow the course of actions and rename the files into 'failed' and 'imported'.
Here's the code:
Also, how can I make the routine stop cycling through the files after I renamed them all?
Many thanks for the help in advance!
I have the following piece of code for importing the .csv files from a selected folder and then renaming them into 'imported & filename' if succesful and 'failed & filename' if import failes for whatever reason (bad formating, etc.).
The problem is that the first time it encounters a 'bad' file after another, instead of going again to the Error handler, the command
Code:
DoCmd.TransferText acImportDelim, , "Activitate", filename, True
gives me the default MS Access error, namely 2391. I would like it to go the the Error_handler again ang follow the course of actions and rename the files into 'failed' and 'imported'.
Here's the code:
Code:
Sub ImportActivitate()
Dim strFile As String 'Filename's
Dim strFileList() As String ' File Array
Dim intFile As Integer 'Number of files
Dim filename As String, sFullName As String, sFilename As String
DoCmd.SetWarnings False
'--------------------------------------------------------------------------
'This created a folder dialog picker from where to import
'the excel files
'Directory selection
Dim path As String
Dim fldr As FileDialog
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Please select the import directory:"
.AllowMultiSelect = False
.InitialFileName = CurrentProject.path & "\"
If .Show <> -1 Then Exit Sub
path = .SelectedItems(1) & "\"
End With
'--------------------------------------------------------------------------
'Creates a look to build the file list
strFile = Dir(path & "*.csv")
While strFile <> ""
'add files to the list
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
strFile = Dir()
Wend
'Check for files
If intFile = 0 Then
MsgBox "There are no files to import! Plese select another directory.", _
vbExclamation + vbOKOnly, "No files found"
GoTo SelectareDirector
End If
'cycle through the list of files
For intFile = 1 To UBound(strFileList)
filename = path & strFileList(intFile)
'filename = sFullPath = "C:\dir\dir\dir\file.txt"
sFullFilename = Right(filename, Len(filename) - InStrRev(filename, "\")) ' spre exemplu 01-09-2012.csv
sFilename = Left(sFullFilename, (InStr(sFullFilename, ".") - 1)) ' spre exemplu 01-09-2012
On Error GoTo Error_Handler
DoCmd.TransferText acImportDelim, , "Activitate", filename, True
Name filename As path & "imported_" & sFullFilename
Nextfile:
Next intFile
MsgBox "Import Complet!", _
vbInformation + vbOKOnly, "Import complete"
Exit Sub
Error_Handler:
Dim response As Long
response = MsgBox("MS Access a generat urmatoarea eroare:" & vbCrLf & vbCrLf & "Numar eroare: " & _
Err.Number & vbCrLf & "Sursa erorii: ImportTranzactii" & vbCrLf & "Descrierea erorii: " & _
Err.Description & vbCrLf & _
"Eroarea a aparut la importul fisierului " & sFullFilename & vbCrLf & _
"Daca apasati OK, importul fisierelor ramase va continua." & vbCrLf & _
"Daca apasati CANCEL, importul fisierelor va fi intrerupt." _
, vbCritical + vbOKCancel, "A aparut o eroare!")
If response = vbCancel Then
Exit Sub
Else
Name filename As path & "failed_" & sFullFilename
Err.Clear
GoTo Nextfile
'Resume Next
End If
DoCmd.SetWarnings True
End Sub
Also, how can I make the routine stop cycling through the files after I renamed them all?
Many thanks for the help in advance!