Solved

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

Posted on 2014-11-11
7
312 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 35

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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
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…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

840 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