Solved

check for error on query on form load in access

Posted on 2013-05-13
12
373 Views
Last Modified: 2013-05-17
Hi,
I have a form in access, On Open and On Load, I have an event that updates a table.

docmd.openquery "ST"

occasionally due a date field ST query may not update an throw an error.

I want to check if there is an error with the query, if error display a message and continue with loading the form. Right now, it just displays the message and doesn't open form in the event there is an error with the ST query.
thanks
S
0
Comment
Question by:Sivasan
[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
  • 6
  • 5
12 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39163172
Try this:

On Error Got to EH
docmd.openquery "ST"
'  
' The Rest of your COde goes here

Exit Sub

EH:
     if Err.number = {place the number of the error you are describing here} Then
             Resume Next
    else
          msgbox "Error " & err.number & ": " & err.description
end sub

Open in new window

0
 
LVL 75
ID: 39163220
On Error Resume Next

CurrentDB.Execute  "ST", dbFailOnError

If Err.Number>0 then
   MsgBox "Error: & " Err. Number & "  " & Err.Description
  'Do whatever because of error

End If

mx
0
 

Author Comment

by:Sivasan
ID: 39163268
Hi Mx,
Sorry , can you please explain further.When I try it, the error message pops even if no error with "ST" I may be placing the code in the wrong spot.

Please see my event below. Basically, if error with DoCmd.OpenQuery "ST"
I just want it not to run DoCmd.OpenQuery "ST"
and just run the following
DoCmd.OpenQuery "U"
DoCmd.OpenQuery "T"
msgbox " Incorrect date report"
and then Open form
in case no error with DoCmd.OpenQuery "ST"   then execute  DoCmd.OpenQuery "ST"
then execute
DoCmd.OpenQuery "U"
DoCmd.OpenQuery "T"
msgbox " Success"
then Open form

Please see below let me know the right way to place code.

DoCmd.OpenForm "All", acNormal

 DoCmd.SetWarnings False
DoCmd.OpenQuery "ST"
 On Error Resume Next

CurrentDb.Execute "ST2Input", dbFailOnError

If Err.Number > 0 Then
  DoCmd.OpenQuery "U"
DoCmd.OpenQuery "T"
MsgBox " my custom message here report wrong date"

else
DoCmd.OpenQuery "U"
DoCmd.OpenQuery "T"

MsgBox "My message Update Success"

End If
end sub
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 75
ID: 39163275
Get rid of ALL DoCmd.SetWarnings False / True ... bad idea as they mask out errors you WANT to know about.

They try what I posted , using the Execute Method,

mx
0
 

Author Comment

by:Sivasan
ID: 39163290
Execute Method? can you please explain?
thx
0
 
LVL 75
ID: 39163304
The Execute Method runs an Action query. And with the dbFailOnError parameter, it will raise any error.  Further, you do not get any of the annoying user prompts like you do with OpenQuery - which is why you had the DoCmd.SetWarnings False/True.

This is a **much safer** approach.  You can check VBA Help for full details, however it's pretty much just this simple.
0
 
LVL 75
ID: 39163311
And you can do this also:
On Error Goto ErrTrap

With CurrentDB
      .Execute  "ST", dbFailOnError
      .Execute  "T", dbFailOnError
      .Execute  "U", dbFailOnError
   ' and all your queries .....
End With

' more code

Done:
Exit Sub

ErrTrap:
   MsgBox "Error: & " Err. Number & "  " & Err.Description
  'Do whatever because of error
Resume Done

End Sub
0
 

Author Comment

by:Sivasan
ID: 39163418
Hi Mx,
Thank you for the detail explaination, but when I try your code above, EVEN when ST is good, it goes to ErrTrap and execute the code below it.
Basically if ST works then execute ST, U, T,  say message " success" open form.
if ST has error, then only execute U, T say message  " Problem with date" open form.
I did my code based on your post and even when ST is good, it still only executes U and T and Message " Problem with date"  and open form
not sure why.

On Error GoTo ErrTrap

With CurrentDb
    .Execute "ST", dbFailOnError
    .Execute "U", dbFailOnError
   .Execute "T", dbFailOnError
.End With

MsgBox "Success"


Done:
Exit Sub

ErrTrap:
  MsgBox "Please inform dept of Date not in "

'DoCmd.OpenQuery "U"
'DoCmd.OpenQuery "T"
Resume Done
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 39163616
Not sure what is happening. First, let's see what the error is you are getting:

ErrTrap:
   MsgBox "Error: & " Err. Number & "  " & Err.Description

Resume Done
0
 

Author Comment

by:Sivasan
ID: 39175325
Figured out. Thanks a lot MX
0
 

Author Closing Comment

by:Sivasan
ID: 39175327
Thanks a million for your help
0
 
LVL 75
ID: 39175392
What did you find ?
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

622 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