Solved

MS Access VBA Sub with update query not presenting message

Posted on 2010-09-09
7
402 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
ID: 33642734
You might have warnings turned off. In vba just before query do:

Docmd.setwarnings true
0
 
LVL 75
ID: 33642757
"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
ID: 33642770
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:Greg_Beam
ID: 33642885
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
ID: 33642921
"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
ID: 33647236
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
ID: 33648572
You did not add the dbFailOnError option as I showed

qdf.Execute  

s/b

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

mx
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
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…
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…
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…

867 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

21 Experts available now in Live!

Get 1:1 Help Now