Solved

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

Posted on 2014-11-11
7
298 Views
Last Modified: 2014-11-14
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?
0
Comment
Question by:dougf1r
  • 3
  • 3
7 Comments
 
LVL 34

Expert Comment

by:PatHartman
ID: 40436239
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.
0
 
LVL 57
ID: 40437210
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.
0
 
LVL 1

Author Comment

by:dougf1r
ID: 40440480
Thanks Jim.

I put:
DoCmd.SetWarnings True
before the statement

Can you please provide an example of the error handler I should add?
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 57
ID: 40440629
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.
0
 
LVL 1

Author Comment

by:dougf1r
ID: 40440698
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?
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 40443510
You can't have two end subs. .it needs to look like this:

Private Sub ImportSurveyP_Click()

On Error Goto Error_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

Exit_ImportSurveyP_Click:

     Exit Sub

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

End Sub
0
 
LVL 1

Author Comment

by:dougf1r
ID: 40443531
Got it! Thanks Jim.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

744 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

11 Experts available now in Live!

Get 1:1 Help Now