turn off error msg

Jason Dinnell

New member
Local time
Today, 04:32
Joined
Feb 8, 2009
Messages
7
Is there code that turns off the error msg when you cancel a view report or send object command button.

“Run time error 2501:”
The OpenReport action was canceled


Thanks
 
You need to use error handling to take care of that. In the event that calls the report, add an error handler:

Near the top of the procedure:
Code:
On Error GoTo err_handler

at the end of the procedure:

Code:
Exit_ProcedureNameHere:
    Exit Sub

err_handler:
   If Err.Number <> 2501 Then
      MsgBox Err.Description, vbExclamation, "Error: " & Err.Number
   End If
   Resume Exit_ProcedureNameHere
End Sub
 
Thanks for the Help..

when i added the code i get a compile error "Lable not defined"
 
Here is the code. i have tried using the same error handling that access puts on my other command buttons but nothing works. I pulled the code for printing one record at a time off the net. I am sure the solution is simple but this is my first time with any programming language.

Code:
Private Sub Print_Watchbill_Click()
On Error GoTo err_handler
'Print current record
'using watchbill section 2.
If IsNull(Me!Date) Then
MsgBox "Please select a valid Date", _
vbOKOnly, "Error"
Exit Sub
End If
DoCmd.OpenReport "Watchbill Section 2", , , _
"Date = # " & Me!Date & " # "
End Sub
err_handler:
   If Err.Number <> 2501 Then
      MsgBox Err.Description, vbExclamation, "Error: " & Err.Number
   End If
   Resume Exit_ProcedureNameHere
End Sub
 
1. It would appear that you are using an Access Reserved Word for a field name (DATE). You should change that as it can cause you no end of grief if you don't.

2. If you keep it, you need to surround it with square brackets so Access knows you aren't trying to refer to the FUNCTION which exists as DATE but instead looking for the FIELD named DATE.

3. You didn't put the code in like I suggested. You only used part of it.

Code:
Private Sub Print_Watchbill_Click()
On Error GoTo err_handler
'Print current record
'using watchbill section 2.
If IsNull(Me![color=red][[/color]Date[color=red]][/color]) Then
MsgBox "Please select a valid Date", _
vbOKOnly, "Error"
Exit Sub
End If
DoCmd.OpenReport "Watchbill Section 2", , , _
"[color=red][[/color]Date[color=red]][/color]= # " & Me![color=red][[/color]Date[color=red]][/color] & " # "

[color=red]Exit_ProcedureNameHere:
Exit Sub[/color]

err_handler:
   If Err.Number <> 2501 Then
      MsgBox Err.Description, vbExclamation, "Error: " & Err.Number
   End If
   Resume Exit_ProcedureNameHere
End Sub
 
Thanks a lot. It works but I still do not understand exactly why.

I did try the code as written several times but I never saved it so when I replied I accidentally left out those two lines.

Something I noticed is that if I copy and past the new lines of code into my original or just type them line by line I still get the compile error. However if I delete the entire print event and copy and past everything from your last post (not just the error handling code) it works fine. But I don’t see anything different between what you have in your post and what I copied over. Thanks for the heads up on the reserved word. I didn’t even realize that could be an issue. I guess i need to be more creative in my field names.

Anyway, I appreciate all your help.
Thanks
Jason
 
After some research on error handling and error trapping things are making more sense, but I was wondering if using “Return Next” wouldn’t be better in the above case. Return next would ignore the error and start at the next line which in this case exits the procedure. The error handler basically does the same thing with more code. Or am I not understanding the difference.

Code:
Private Sub Print_Watchbill_Click()
On Error Resume Next
If IsNull(Me![wbDate Selection]) Then
MsgBox "Please select a valid Date", _
vbOKOnly, "User Error"
Exit Sub
End If
 
    DoCmd.OpenReport "Watchbill Section 2 Selection", acNormal
Exit_Print_Watchbill_Click:
    Exit Sub
 
End Sub
 

Users who are viewing this thread

Back
Top Bottom