Link to home
Start Free TrialLog in
Avatar of upobDaPlaya
upobDaPlaya

asked on

MS Access - How do I trap an error for a bad import

In the below code I want to:
1-display a message box that the import failed if there was a problem with this specific line of code ... DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, "tblImportExcel", sOpenFile,
2-Exit the module and go back to my home form

NOTE, I am calling this module from a form.  Thus, if an error occurs I need to make sure I exit this module and I am placed back at my form object where I called Import()



Public Sub Import ()
'Get sheet name
Dim sOpenFile As String
'
'display the open file dialog box
sOpenFile = OpenFileDialog("Import Data File")
If sOpenFile <> "" Then
'the user has selected a file to import, the filename and path
'are contained in the variable sOpenFile
sOpenFile = Trim$(sOpenFile) 'remove all trailing spaces
'make sure it's an Excel file
If Right(sOpenFile, 4) <> ".xls" Then
MsgBox "You must select an Excel (.xls) file.", vbOKOnly
Else
KRunQuery ("qImportDeleteTableExcel")' just a call to the same query running code that Access Wizard generates, which I saved as a subroutine. This query deletes the data in the temporary import table. You would need to create your own delete query, or substitute code to do so.
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel97, "tblImportExcel", sOpenFile, True
MsgBox "Initial transfer from spreadsheet to Import Table is COMPLETE! Proceed to next step.", vbOKOnly
End If
End If
End Sub
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of upobDaPlaya
upobDaPlaya

ASKER

Very nice...thank you...this is good as I can trap any non import errors and then trap an error specific to the import..thanks for shring your knowledge..