Solved

Error handling stop routine on error

Posted on 2010-08-20
5
696 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

632 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