Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 704
  • Last Modified:

Error handling stop routine on error

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)
    
    With fdg
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems
            strSelectedFile = vrtSelectedItem
        Next vrtSelectedItem
        Else
        End If
        Dim StrSQL As String
        StrSQL = "Delete * from Scrap;"
        DoCmd.SetWarnings False
        DoCmd.RunSQL StrSQL
        DoCmd.SetWarnings True
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Scrap", strSelectedFile, True, , False
        

        
        End With
        Set fd = Nothing
Exit_Procedure:
     Exit Sub
   
        
Error_Handler:
DisplayUnexpectedError Err.Number, Err.Description
        
End Sub

Open in new window



And the error handling code:
 
Public Sub DisplayUnexpectedError(ErrorNumber As String, ErrorDescription As String)
Select Case Err.Number
       Case 2522
          MsgBox "You did not select an Excel file"
       End Select
End Sub

Open in new window


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.
0
G Scott
Asked:
G Scott
  • 3
  • 2
1 Solution
 
Rey Obrero (Capricorn1)Commented:
       
        End With
        Set fd = Nothing
Exit_Procedure:
     Exit Sub
   
       
Error_Handler:
DisplayUnexpectedError Err.Number, Err.Description
       
'Add this line

Resume Exit_Procedure

End Sub
0
 
Rey Obrero (Capricorn1)Commented:

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)
    
    With fdg
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewDetails
        If .Show = -1 Then
            For Each vrtSelectedItem In .SelectedItems
            strSelectedFile = vrtSelectedItem
        Next vrtSelectedItem
        Else
        End If
        Dim StrSQL As String
        StrSQL = "Delete * from Scrap;"
        DoCmd.SetWarnings False
        DoCmd.RunSQL StrSQL
        DoCmd.SetWarnings True
        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "Scrap", strSelectedFile, True, , False
        

        
        End With
        Set fd = Nothing
Exit_Procedure:
     Exit Sub
   
        
Error_Handler:
DisplayUnexpectedError Err.Number, Err.Description


'Add this line

Resume Exit_Procedure


        
End Sub

Open in new window

0
 
G ScottAuthor Commented:
Tried that and it still cleared out the table...

I also tried

If NOT ISNULL(strSelectedFile) Then
   ...Import Spreadsheet....
Else
   msgbox "Pick an Excel File"
End If

But that didn't work either.


If NOT ISNULL(strSelectedFile) Then
   Import Spreadsheet
Else
   msgbox
End If

Open in new window

0
 
Rey Obrero (Capricorn1)Commented:
try this


If  len(trim(strSelectedFile) & "") >0 Then
   Import Spreadsheet
Else
   msgbox
End If
0
 
G ScottAuthor Commented:
That did it. Thanks for saving me again cap!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now