Link to home
Start Free TrialLog in
Avatar of dougf1r
dougf1r

asked on

Import error warning does not pop-up when errors occur on VBA import

I am using the following code to import data through an MS Access 2010 form:

Private Sub ImportSurveyP_Click()
If IsNull(Me.txtFileName) Or Len(Me.txtFileName & "") = 0 Then
    MsgBox "Please select the Excel file"
    Me.ImportSurveyP.SetFocus
    Exit Sub
End If

DoCmd.TransferSpreadsheet acImport, 10, "data_Survey", Me.txtFileName, True

Me.txtFileName.SetFocus
Me.ImportSurveyP.Enabled = False

End Sub

Open in new window


If one of the fields of the import file contains a data type that does not match with the database specifications, the usual "Sheet1$_ImportErrors" table is generated to list which field(s) and record(s) were in error. However, Access does not show the typical warning message that the errors occurred and the error table was generated.

How can I get this warning message to appear when importing with VBA?

Note: Confirm Record changes, Document deletions, and Action queries are all checked in Options. Is there another Option setting I need to make?
Avatar of PatHartman
PatHartman
Flag of United States of America image

You don't get it because most people would want this to be a "silent" operation so it can run unattended.  After the import, check for the existence of the import errors file and pop up a message if you want.
I don't remember off-hand if the errors with TransferSpreadsheet are fully suppressed or not.  Add:

DoCmd.SetWarnings True

before the statement, and also add an error handler (on error goto ....) to the procedure.  If you still get nothing, then they are not exposed.

Jim.
Avatar of dougf1r
dougf1r

ASKER

Thanks Jim.

I put:
DoCmd.SetWarnings True
before the statement

Can you please provide an example of the error handler I should add?
At the top of the procedure, you would do:


On Error Goto Error_ImportSurveyP_Click

Then in the last part of the procedure, it would look like this:

DoCmd.TransferSpreadsheet acImport, 10, "data_Survey", Me.txtFileName, True

Me.txtFileName.SetFocus
Me.ImportSurveyP.Enabled = False

Exit_ImportSurveyP_Click:

Exit Sub

Error_ImportSurveyP_Click:
      MsgBox "Error " & err.num & " just occurred."
      Resume Exit_ImportSurveyP_Click

End Sub

  Now if any kind of trappable error occurs, you'll get the msgbox.   In place of that, you might do:

Error_ImportSurveyP_Click:
      If Err.Num = xxxx then
          ' This is an error we more or less expect and want to do something
      Else
          ' Unxepected and not sure how to handle, so just let the user know.
            MsgBox "Error " & err.num & " just occurred."
      End If
      Resume Exit_ImportSurveyP_Click

End Sub


 So you can respond to a specific error in a certain way and another way for other errors.  

  The other form of error handling is called "in-line" error handling.  This is where you do:

 On Error Resume Next
 Err = 0
 DoCmd.TransferSpreadsheet acImport, 10, "data_Survey", Me.txtFileName, True
 
 If Err<>0 then
    ' Some type of error occurred.
 End If


 The first form is more of  a generic handler for the entire procedure.   Doing it "in-line" lets you know that an error occurred with a specific operation (you actually can do that with the other, but you'd need to number your code lines and things are not quite as clear vs using in-line).

Jim.
Avatar of dougf1r

ASKER

I included:

On Error Goto Error_ImportSurveyP_Click

Open in new window


at the top, just below Line 1 (Private Sub ImportSurveyP_Click())

And also included:

Error_ImportSurveyP_Click:
      MsgBox "Error " & Err.num & " just occurred."
      Resume Exit_ImportSurveyP_Click

End Sub

Open in new window


At the bottom, just below Line 13 (End Sub)

But I am getting a Compile Error: Label not defined (error points to "On Error Goto...")

Ideas on how to remedy this?
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dougf1r

ASKER

Got it! Thanks Jim.