Solved

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

Posted on 2014-11-11
7
322 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
[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
  • 3
7 Comments
 
LVL 37

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

738 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