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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Message Box with Yes/No Option 3

Status
Not open for further replies.

waldo7474

Technical User
Dec 30, 2002
38
US
Hi All!!!

How do I create a Message Box that allows me to have a yes or no response from the user. "MsgBox" only allows me to have an ok response. I would like a Yes No Response and then I need to know how to have the actions after the Yes or No has been clicked. I think this is probably straight forward, but I am new to VBA and trying to get the hang of it. Your responses will be greatly appreciated.

Thanks in advance for all your help!!
 
Slightly modified from the example in the help file:

Function YesNoBox(ByVal boxtitle As String, ByVal Msg As String)
Dim Style, Response, MyString


'Msg = Msg + Chr$(10) + " Are you sure you want to do this ?" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.

' context.
' Display message.
Response = MsgBox(Msg, Style, boxtitle)

If Response = vbYes Then ' User chose Yes.
YesNoBox = True
Else ' User chose No.
YesNoBox = False
End If

End Function

 
I need to add a couple of things:
The function above returns true or false, so to answer your question on how to handle the response would be:

If YesNoBox("WARNING:","Are you sure you want to press that button?") then
'Do this
else
'Do that
Endif
Next the line:
Style = vbYesNo + vbCritical + vbDefaultButton2
Can be modified to fit your situation. This one displays a Stop sign. See the help file under Msgbox for more options on styling the box

The comment:
'Msg = Msg + Chr$(10) + " Are you sure you want to do this ?" ' Define message.
is just an old comment I left in to remind myself I could use Chr$(10) in the msg variable to separate a message on different lines. It is pretty obsolete. Nowadays it would be vbCRLf, for example:

Dim msg as string

msg="Please check the following:"+vbCrlf+"Yadda"+vbCrkf+"Yadda Yadda"+vbcrlf+"Proceed with processing")


If Yesnobox("WARNING:", msg) then
etc








 
Hi all!!

I thought that I had it. I manipulated the example in the help section and used some of vbajock. The problem that I have now is both buttons do the same thing. I have changed things around many times but nothing seems to work. Here is the code that I am using right now.
Code:
    Dim Response As String
    MsgBox "WARNING:" + vbCrLf + "You are attempting to change the Master File.  Make sure" + vbCrLf + " you have proper authorization to do this.  Do you wish to continue?", vbCritical + vbYesNo, "Prime Systems, Inc."
    If Response <> No Then
        Me!FrmSubRevAdd.Visible = False
        Me!PartNumber.Locked = True
        Me!Family.Locked = False
        Me!SheetSize1Cbo.Locked = False
        Me!SheetSize2Cbo.Locked = False
        Me!SheetSize3Cbo.Locked = False
        Me!Description.Locked = False
        Me!FirstUsedOn.Locked = False
        Me!RevNumber.Locked = True
        Me!DateEntered.Locked = False
            If Not IsNull(Me!OldPartNumber) Then
            Me!OldPartNumber.Visible = True
            End If
            If Not IsNull(Me!WrongPartNumber) Then
            Me!WrongPartNumber.Visible = True
            End If
            If Not IsNull(Me!OldDescription) Then
            Me!OldDescription.Visible = True
            End If
        Me!PartNumber.SetFocus
    End If
    If Response = No Then
        Me!SaveRecord.SetFocus
    End If
Any help would be greatly appreciated. The way that I have the code right now. It sets the focus to the save record, but it is also unlocking all the fields. It seems to try to be doing both of the ifs.

I had a response = yes, and a response <> no. This for some reason is not the same thing. I am pulling my hair out with this thing. Any help would be great!!

Thanks in advance!!
 
Use vbYes and vbNo, as that's what the msgbox is going to return.

Also, to make things more clear, I often do this (with either vbYes or VbNo, depending on what I'm checking for):

if vbYes = Msgbox(&quot;Yadda&quot;) then
'Do Some stuff
else
'Do some other stuff
end if

Jeremy =============
Jeremy Wallace
AlphaBet City Dataworks

Take a look at the Developers' section of the site for some helpful fundamentals.
 
Thanks this worked like champ. I tried the vbYes thing before but it told me I was using &quot;unlike&quot; terms or something. But this works. Thanks for the time everyone who responded. I appreciate the guidance vbajock. I had problems with the whole &quot;Function&quot; thing. It wouldn't let me run anything like that. Maybe I don't have a certain reference table set up. Thanks JeremyNYC to you goes the star.

Thanks again everybody!!
 
I also use a similar format to JeremyNYC. I find it easier to figure out what I'm doing with it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top