Solved

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

Posted on 2014-11-11
7
304 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

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!

Question has a verified solution.

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

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

948 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