Help with Error Problem

E

erick-flores

Hello there, I am trying to display an error everytime a cmd button is
hit and a report is not selected. Below is the code I am using, it
works prefect, but when the actual report is selected and displayed it
also pop up a windows displaying the error again. So it looks like the
error is displaying everytime I either have selected a report or have
not selected a report. Can someone help me please, thanks

Private Sub PBPreviewDiag_Click()

On Error GoTo Handler
Dim stDocName As String

stDocName = Forms![Select Reports]!LBDiagReports.Column(2)

DoCmd.OpenReport stDocName, acPreview

Handler:
MsgBox "Select a report"

End Sub
 
K

Keith Wilby

erick-flores said:
Hello there, I am trying to display an error everytime a cmd button is
hit and a report is not selected. Below is the code I am using, it
works prefect, but when the actual report is selected and displayed it
also pop up a windows displaying the error again. So it looks like the
error is displaying everytime I either have selected a report or have
not selected a report. Can someone help me please, thanks

Private Sub PBPreviewDiag_Click()

On Error GoTo Handler
Dim stDocName As String

stDocName = Forms![Select Reports]!LBDiagReports.Column(2)

DoCmd.OpenReport stDocName, acPreview

Handler:
MsgBox "Select a report"

End Sub

You need "Exit Sub" before your error handler.

Regards,
Keith.
www.keithwilby.com
 
A

Allen Browne

You forgot to Exit Sub above the error handler. Consequently, you see the
error message, even when there is no error.

Try something like this:

Private Sub PBPreviewDiag_Click()
On Error GoTo Err_Handler
Dim stDocName As String

stDocName = Forms![Select Reports]!LBDiagReports.Column(2)
DoCmd.OpenReport stDocName, acPreview

Exit_Hander:
Exit Sub

Err_Handler:
MsgBox "Select a report"
Resume Exit_Handler
End Sub

More info:
Error Handling in VBA
at:
http://allenbrowne.com/ser-23a.html
 
E

erick-flores

Thank you very much, works perfect now :D

Allen said:
You forgot to Exit Sub above the error handler. Consequently, you see the
error message, even when there is no error.

Try something like this:

Private Sub PBPreviewDiag_Click()
On Error GoTo Err_Handler
Dim stDocName As String

stDocName = Forms![Select Reports]!LBDiagReports.Column(2)
DoCmd.OpenReport stDocName, acPreview

Exit_Hander:
Exit Sub

Err_Handler:
MsgBox "Select a report"
Resume Exit_Handler
End Sub

More info:
Error Handling in VBA
at:
http://allenbrowne.com/ser-23a.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

erick-flores said:
Hello there, I am trying to display an error everytime a cmd button is
hit and a report is not selected. Below is the code I am using, it
works prefect, but when the actual report is selected and displayed it
also pop up a windows displaying the error again. So it looks like the
error is displaying everytime I either have selected a report or have
not selected a report. Can someone help me please, thanks

Private Sub PBPreviewDiag_Click()

On Error GoTo Handler
Dim stDocName As String

stDocName = Forms![Select Reports]!LBDiagReports.Column(2)

DoCmd.OpenReport stDocName, acPreview

Handler:
MsgBox "Select a report"

End Sub
 
Top