Solved

MS Access VBA Sub with update query not presenting message

Posted on 2010-09-09
7
399 Views
Last Modified: 2013-11-29
Hi,
My MS Access app has a VBA sub that has runs an update query.  The Sub is run as a result of clicking on a button on a form.  When the query is run directly, it presents the message, if applicable, "Microsoft Office Access can't update all of the records in the update query".  But when the query is run via clicking on the button, the message is not displayed.

I added On Error GoTo Err_PresentMessage at the top of the code and Err_PresentMessage:      
    MsgBox Err.Number & Err.Description
    Exit Sub
at the bottom, but the message still does not appear.

I would like to present all messages to the user and eventually intercept them with more detail about how to correct, etc.  How can I get the messages to appear?
Thanks,
Greg



0
Comment
Question by:Greg_Beam
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:gozoliet
Comment Utility
You might have warnings turned off. In vba just before query do:

Docmd.setwarnings true
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
"But when the query is run via clicking on the button, the message is not displayed."

That would normally only happen if ... you have a DoCmd.SetWarnings False command.

You will need this approach:

Public Function SomeFunction()

On Error Goto SomeFunction_Error

  '  whatever code
   
     CurrentDb.Execute "YourActionQueryName", dbFailOnError   ' ****

  '  more code  

SomeFunction_Exit:
Err.Clear
Exit Function

SomeFunction_Error:
    Select Case Err.Number
         Case <SomeErrorYouExpect1>   ' actual error number
                Mgsbox "SomeMessage1"
         Case <SomeErrorYouExpect2>  ' actual error number
                Mgsbox "SomeMessage2"      
           ' and so on for each expect error where you want a nice messge
         Case Else
                Msgbox "Unexpected Error occurred ...."
    End Select
    Goto SomeFunction_Exit

End Function    
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
Unfortunately, Docmd.setwarnings true (false)  is really not a good idea because it will often mask errors that you WANT to know about ... and may give the false illusion that the action query worked correctly.  

Everyone should get away from use Docmd.setwarnings.  The Execute Method of the CurrentDb object is much safer and more flexible, yet avoids the usually annoying 'warning' messages like "You are about to append N records ...", etc.

mx
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Greg_Beam
Comment Utility
It seems there are errors and warnings, which are handled differently, it that true?

So the On Error would trap errors, but since the message I am trying to present is a warning, it is not trapped by On Error?

I explicity set Docmd.setwarnings true just before the On Error, and still it does not work.

I am setting a QueryDef to the query name and Opening the RecordSet.  Does this prevent the warnings, annoying and otherwise?  If I continue to use it, can I turn warnings on?

Greg
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
"it that true?"
Yes.

"So the On Error would trap errors, but since the message I am trying to present is a warning, it is not trapped by On Error?"

It's a warning of sorts in the DoCmd.SetWarnings paragigm.  It's an Error in the Execute method ... which is what you what, because you want to trap that ... and display a nice 'what to do' message.

As I suggested, completely forget about using DoCmd ... and start using the Execute method for these operations ... and you will be way ahead of the game.

Check the VBA Help file for all the info on the Execute method ... which can be used with QueryDefs (and more) also ... and you will see all the other options.

mx
0
 

Author Comment

by:Greg_Beam
Comment Utility
Here is the code.  I am doing all of the suggestions but do not get the warnings on the "Import update 00 Consolidated Mapping" update query, which uses Execute.

Option Compare Database

Private Sub Import_non_MAPS_based_spreadsheet_Click()

DoCmd.SetWarnings True

On Error GoTo Err_PresentMessage

Dim Db As DAO.Database
Dim qdf As DAO.queryDef
Dim rs As Recordset

Set Db = CurrentDb

Me.[Edit Description] = "check Batch Name"
Set qdf = Db.QueryDefs("Import check 01 Batch Name")
Set rs = qdf.OpenRecordset
If Not rs.EOF Then
    Me.[Spreadsheet Errors].SourceObject = "Query.Import check 01 Batch Name"
    Exit Sub
End If

Set qdf = Db.QueryDefs("Import update 00 Consolidated Mapping")
qdf.Execute

Set qdf = Nothing
Set Db = Nothing
                   
Exit Sub

Err_PresentMessage:                    
    MsgBox Err.Number & Err.Description
    Exit Sub
   
End Sub
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
Comment Utility
You did not add the dbFailOnError option as I showed

qdf.Execute  

s/b

qdf.Execute, dbFailOnError  '  *** You need this

mx
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

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…
When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

772 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

10 Experts available now in Live!

Get 1:1 Help Now