Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbYesNo MsgBox Functionality

Status
Not open for further replies.

Cyberjake

Technical User
Joined
Jun 5, 2003
Messages
14
Location
US
I have selected to use the vbYesNo MsgBox in my coding. My dilemma comes when the message box appears. I click on Yes and it runs the remaining code; I click on No and it still runs the code instead of ending the sub routine. How do I make the Yes and No buttons on the MsgBox work so that when I click on No, it will end the sub routine?

Code:
 Dim strMsg As String, stDocName As String, stLinkCriteria As String
    strMsg = "There is no Telephone Number assigned to the Consignee. Do you want to add a Telephone Number?"
    stDocName = "frmCustomerInfo"
    stLinkCriteria = "[CustomerNumber]=" & Me![Consignee]
    
    If CallForAppointment = True Then
        If IsNull(Forms!frmQuoteOrderEntry!Customer_subform2!Telephone) Then
            MsgBox strMsg, vbYesNo, "Missing Consignee Telephone"
            DoCmd.OpenForm stDocName, , , stLinkCriteria
            Exit Sub
        Else
            PurchaseOrder.SetFocus
        End If
    Else
        PurchaseOrder.SetFocus
    End If
 
I've modified (and simplified) your code a little:
Code:
Dim strMsg As String
strMsg = "There is no Telephone Number assigned to the Consignee. Do you want to add a Telephone Number?"
    
If CallForAppointment = True Then
    If IsNull(Forms!frmQuoteOrderEntry!Customer_subform2!Telephone) Then
        If MsgBox(strMsg, vbYesNo, "Missing Consignee Telephone") = vbYes Then DoCmd.OpenForm "frmCustomerInfo", , , "[CustomerNumber]=" & Me![Consignee]
    Else
        PurchaseOrder.SetFocus
    End If
Else
    PurchaseOrder.SetFocus
End If
Hope this helps.

[pc2]
 
Thanks, that worked out great. For the longest time I have avoided using the MsgBoxes because I could not get them to function properly. You have enlightened me on how to use them. Thanks again!!
 
If I care about the value returned by a msgbox statement, I set it up like this:

If vbYes = MsgBox("Do you wanna?", vbYesNo) Then
'some stuff
else
'some other stuff
end if

That way, the answer I'm responding to is at the front of the line, and I can read it more easily. It doesn't make a bit of difference to Access, but I find it makes my code a lot easier for me to read.

Jeremy

==
Jeremy Wallace
AlphaBet City Dataworks
Affordable Development, Professionally Done

Please post in the appropriate forum with a descriptive subject; code and SQL, if referenced; and expected results. See thread181-473997 for more pointers.
 
I personally prefer this approach for readability:

Dim intResponse as integer

intResponse = msgbox("The Text", vbYesNo)
if intResponse = vbYes then
'Do something
else
'Do something else
end if

I know it adds an extra variable, but I find it easier to read, and maintain. Furthermore, at times I have custome messageboxes, and I find that the above format is easier to use when yanking values from these.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top