I am letting the user select an Excel file to import a spreadsheet into a table with the following code:
Private Sub Command1_Click()
On Error GoTo Error_Handler
Dim fdg As FileDialog, vrtSelectedItem As Variant
Dim strSelectedFile As String
Set fdg = Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.InitialView = msoFileDialogViewDetails
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
strSelectedFile = vrtSelectedItem
Dim StrSQL As String
StrSQL = "Delete * from Scrap;"
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Scrap", strSelectedFile, True, , False
Set fd = Nothing
DisplayUnexpectedError Err.Number, Err.Description
And the error handling code:
Public Sub DisplayUnexpectedError(ErrorNumber As String, ErrorDescription As String)
Select Case Err.Number
MsgBox "You did not select an Excel file"
If they choose cancel in the FileDialog window nothing gets imported and the table is left empty. I get the message box that I want, but the code continues and imports nothing into the table. How do I exit the code if the error occurs? I am new to VBA and even newer to error handling. Thanks for any help you can give me on this.