View Full Version : Error handling problem


ScrmingWhisprs
05-07-2007, 12:16 PM
I have the following code in the OnClick event of a button that opens a report. I'm not that fluent with error handling code, so i'm not sure if I have all of this in the right place:Private Sub cmdViewReport_Click()
On Error GoTo Err_Report_Click

DoCmd.OpenReport "On Search", acViewPreview

Err_Report_Click:
If Err.Number = 2501 Then
Resume Next
Else

MsgBox Err.Description
Resume Exit_Report_Click
End If

If Me.Frame43.Value = 3 Then
If IsNull(txtAfter) Then
DoCmd.Close acReport, "On Search", acSaveNo
MsgBox "You must enter an After date.", vbExclamation, "Report Error"
End If

If IsNull(txtBefore) Then
DoCmd.Close acReport, "On Search", acSaveNo
MsgBox "You must enter a Before date.", vbExclamation, "Report Error"
End If

End If


End Sub
When I click the command button I get a Compile error: Label not defined. Message box. The line Resume Exit_Report_Click is highlighted in the code.

What am I doing wrong?

ScrmingWhisprs

boblarson
05-07-2007, 12:19 PM
Change the areas listed in Bold to what I've shown in bold and move it down to the bottom:

I have the following code in the OnClick event of a button that opens a report. I'm not that fluent with error handling code, so i'm not sure if I have all of this in the right place:Private Sub cmdViewReport_Click()
On Error GoTo Err_cmdViewReport_Click

DoCmd.OpenReport "On Search", acViewPreview



If Me.Frame43.Value = 3 Then
If IsNull(txtAfter) Then
DoCmd.Close acReport, "On Search", acSaveNo
MsgBox "You must enter an After date.", vbExclamation, "Report Error"
End If

If IsNull(txtBefore) Then
DoCmd.Close acReport, "On Search", acSaveNo
MsgBox "You must enter a Before date.", vbExclamation, "Report Error"
End If

End If
Exit Sub
Err_cmdViewReport_Click:
If Err.Number = 2501 Then
Resume Next
Else

MsgBox Err.Description
Resume Exit_Report_Click
End If
End Sub

Rich
05-07-2007, 12:31 PM
Shouldn't it be more like

On Error GoTo Err_Preview_Click

'Check to see that ending date is later than beginning date.
If IsDate(BeginningDate) And IsDate(EndingDate) Then
If EndingDate < BeginningDate Then
MsgBox "The ending date must be later than the beginning date."
SetDate.Caption = "Set Ending Date"
SelectDate.SetFocus
Exit Sub
End If
Else
MsgBox "Please use a valid date for the beginning date and the ending date values."
Exit Sub
End If

strDocName = "YourReport"
DoCmd.OpenReport strDocName, acViewPreview


Exit_Preview_Click:
Exit Sub

Err_Preview_Click:
If Err = ConErrRptCanceled Then
Resume Exit_Preview_Click
Else
MsgBox Err.Description
Resume Exit_Preview_Click
End If
It seems pointless to open the Report first
End Sub