Solved

Error handling stop routine on error

Posted on 2010-08-20
5
689 Views
Last Modified: 2013-11-27
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
Comment
Question by:G Scott
  • 3
  • 2
5 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 33488283
       
        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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 33488343

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
 
LVL 1

Author Comment

by:G Scott
ID: 33488358
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
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 33488403
try this


If  len(trim(strSelectedFile) & "") >0 Then
   Import Spreadsheet
Else
   msgbox
End If
0
 
LVL 1

Author Closing Comment

by:G Scott
ID: 33488458
That did it. Thanks for saving me again cap!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question