How to Insert Error Handling Code

  • Thread starter Thread starter Nona
  • Start date Start date
N

Nona

I want to have an error code that will prevent users from getting an error
message for Error #2501. I get this error when making a selection from a
combo box if for some reason the action is not completed.

Is there a simple code I can copy and use for this? And where do I put the
code? on the "On Error" properties of the form that contains the combo box?
(The "On Error" property is not listed as an option among properties of the
Combo box.)

Thanks!



Nona
 
Hi
To trap the erorr you need to add a small section to your code like this
On Error GoTo Err_Handler
'some other code here'
etc
etc
Exit Sub
Err_Handler:
If Err.Number = 2501 Then
Resume Next
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_Point
End If
End Sub


Have a look at Alan Brown's details n this page
http://allenbrowne.com/ser-23a.html

good luck
 
Hi Nona,
I get this error when making a selection from a
combo box if for some reason the action is not completed.

One cause of Error 2501 is if the report includes a NoData event procedure,
used to cancle opening of the report in the event that the underlying
recordsource does not include any records. This is useful especially in cases
where reports would otherwise display errors.

Have a look at the code in the this sample, specifically the form named
"fdlgAskForDatesAndCity2", for the cmdOK_Click event procedure:

http://www.accessmvp.com/TWickerath/downloads/customdialogbox.zip

Here, you can see that I used a Select Case structure in the error handler
"ProcError":

ExitProc:
Exit Sub
ProcError:
Select Case Err.Number
Case 2501 ' No data or user cancels, so ignore this error.
Case 2302, 3010 ' Excel file is already open
MsgBox "You must close the file '" & strFile _
& "' before attempting to export the data.", _
vbCritical, "Cannot Overwrite Open File..."
Case Else
MsgBox Err.Number & ": " & Err.Description, vbCritical, _
"Error in cmdOK_Click Event Procedure..."
End Select
Resume ExitProc
End Sub


The NoData event procedure can be found in the class module associated with
the report named "rptOrders".

Private Sub Report_NoData(Cancel As Integer)
On Error GoTo ProcError

MsgBox "No records match the search criteria for this report.", _
vbInformation, "Project Database"
Cancel = True

ExitProc:
Exit Sub
ProcError:
MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
"Error in NoData event procedure..."
Resume ExitProc
End Sub

You might want to see this article as well:

Add NoData Event to Access Reports
http://www.fmsinc.com/free/NewTips/Access/accesstip45.asp


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________
 
Wow! This is great information. Thank you1 It will take me awhile to study
it, then make the changes on my database, but I think it will work. I had
been confused because when I got the ms error, then clicked on debug, it sent
me to the code for the dblclick. I tried to insert the error code there, but
it wouldn't take it. I hope I can make this work now. Many thanks. I really
appreciate your time and expertise.
 
Thanks, Wayne, for your help and for sending the code. I hope to be able to
make this work!
 
Thanks so much for all the information. After reading through your papers, I
realized that you diagnosed the problem correctly as being related to the No
Data property. The problem doesn’t occur within the report itself – it
happens when the user clicks on the desired report from a list box. When I
click on “Debug†in the error message, it takes me to the Print Preview code.
However I still get the ms error message as before. Here’s my code:

Private Sub Command4_DblClick(Cancel As Integer)
'Open the selected report in the Print Preview window
DoCmd.OpenReport [ReportListControl], acViewPreview

On Error GoTo ProcError

MsgBox "No records match the search criteria for this report.", _
vbInformation, "Project Database"
Cancel = True

ExitProc:
Exit Sub
ProcError:
MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
"Error in NoData event procedure..."
Resume ExitProc
End Sub

What am I doing wrong?
 
Hi Nona,

You have put the code that should go in the report's NoData event procedure
into the double-click event procedure for the command button currently named
"Command4". I have a couple of suggestions:

1.) Rename this command button to give it a more meaningful name. This way,
when you see it's name in VBA code, you will know the function it is intended
to do. Use something like cmdPreviewReport

2.) Use the Click event procedure instead of the double-click event procedure.

3.) Move the On Error statement *above* the line of code that attempts to
open the report. This way, if there is an error opening the report, your
error-handling code will be properly invoked.

Private Sub cmdPreviewReport_Click(Cancel As Integer)
On Error GoTo ProcError

'Open the selected report in the Print Preview window
DoCmd.OpenReport [ReportListControl], acViewPreview


ExitProc:
Exit Sub
ProcError:
Select Case Err.Number
Case 2501 ' No data or user cancels, so ignore this error.
Case Else
MsgBox Err.Number & ": " & Err.Description, vbCritical, _
"Error in cmdPreviewReport_Click Event Procedure..."
End Select
Resume ExitProc
End Sub


4.) Move the code intended for the NoData event procedure to the class
module associated with your report. See the code associated with the report
in my sample database.


Tom Wickerath
Microsoft Access MVP
http://www.accessmvp.com/TWickerath/
__________________________________________

:

Thanks so much for all the information. After reading through your papers, I
realized that you diagnosed the problem correctly as being related to the No
Data property. The problem doesn’t occur within the report itself – it
happens when the user clicks on the desired report from a list box. When I
click on “Debug†in the error message, it takes me to the Print Preview code.
However I still get the ms error message as before. Here’s my code:

Private Sub Command4_DblClick(Cancel As Integer)
'Open the selected report in the Print Preview window
DoCmd.OpenReport [ReportListControl], acViewPreview

On Error GoTo ProcError

MsgBox "No records match the search criteria for this report.", _
vbInformation, "Project Database"
Cancel = True

ExitProc:
Exit Sub
ProcError:
MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
"Error in NoData event procedure..."
Resume ExitProc
End Sub

What am I doing wrong?
 
Back
Top