Solved

Error handling stop routine on error

Posted on 2010-08-20
5
686 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 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
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 119

Accepted Solution

by:
Rey Obrero 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now