Command button and message box

Robert C

Registered User.
Local time
Today, 20:44
Joined
Mar 27, 2000
Messages
88
I have a command button which opens a report from a form. If a particular field on the form is not completed, I have a message box which pops up to tell the user that they can't open the report because they haven't filled in the particular field.

This all works OK but the report continues to open anyway, even after the OK button on the message box is clicked.

What code do I need to insert after the message box instruction and before the open report instruction to stop the report opening and return to the form.

Many thanks for any help.
 
Add the following line after your msgbox code but before the Open Report code

Exit Sub

This will cause the code to stop at that point.

HTH
RDH
 
RTH

Thanks for the reply, I've done what you suggested, but now, the report does not print even if the particular fields are completed. Could you advise me please.

Thanks again.
 
Robert, post the complete code you are using with this command button. I'm sure it's only a minor adustment needed.

RDH
 
RDH

Thanks again, heres the code so far:

Private Sub btnPrintOrder_Click()

If IsNull(Me.[ProjectID]) Then

MsgBox "Sorry, but you can't print this order until you've selected a Project from the list."

Me.[cboProjectName].SetFocus

End If


If IsNull(Me.[OrderedBy]) Then

MsgBox "Sorry, but you can't print this order until you've entered a name from the 'Ordered By' list."

Me.cboOrderedBy.SetFocus

End If

Exit Sub


On Error GoTo Err_btnPrintOrder_Click


Dim stDocName As String

stDocName = "rptPurchaseOrder"

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.OpenReport stDocName, acViewPreview, "qrySalesOrder"


Exit_btnPrintOrder_Click:
Exit Sub

Err_btnPrintOrder_Click:
MsgBox Err.Description
Resume Exit_btnPrintOrder_Click


End Sub
 
Sorry for the late reply, I got tied up at work.

In your code you are using 3 conditions. So I change your code around to better fit your needs (I hope). You will actually need 2 Exit subs in the code. I also used an If, ElseIf, Else statement to check the conditions. Make a copy of your db before you make these changes (just in case). You should be able to just copy the following sub, and paste it in place of your sub.

Private Sub btnPrintOrder_Click()
On Error GoTo Err_btnPrintOrder_Click
Dim stDocName As String

stDocName = "rptPurchaseOrder"

If IsNull(Me.[ProjectID]) Then
MsgBox "Sorry, but you can't print this order until you've selected a Project from the list."
Me.[cboProjectName].SetFocus
Exit Sub
ElseIf IsNull(Me.[OrderedBy]) Then
MsgBox "Sorry, but you can't print this order until you've entered a name from the 'Ordered By' list."
Me.cboOrderedBy.SetFocus
Exit Sub
Else
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.OpenReport stDocName, acViewPreview, "qrySalesOrder"
End If

Exit_btnPrintOrder_Click:
Exit Sub

Err_btnPrintOrder_Click:
MsgBox Err.Description
Resume Exit_btnPrintOrder_Click

End Sub

HTH
RDH
 
Thanks a million RDH. It all works a treat.

Rob
 
Robert, you are very welcome for the help, but please don't just accept the answer I gave just because it worked. Take a little time to evaluate the code and understand how and why it worked. Then it will become a lesson learned. This is how I learned what I know about Access. I have not had any formal training in Access.

Good luck with your project,

RDH
 

Users who are viewing this thread

Back
Top Bottom