Message Box

kitty77

Registered User.
Local time
Today, 00:51
Joined
May 27, 2019
Messages
715
I'm looking for a message box that if you select yes, something happens or no something else happens.
Any good examples?

Thanks...
 
I'm looking for a message box that if you select yes, something happens or no something else happens.
Any good examples?

This Blog on my website maybe of interest:-

 
Code:
Private Sub cmdMessageBox_Click()
    MsgBox "Your logon credentials have been checked " & _
           "and your application has been approved: Congratulations!" & _
           vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           VbMsgBoxStyle.vbYesNo Or VbMsgBoxStyle.vbQuestion
End Sub

msgbox4.gif
 
@oleronesoftwares . You need some code to actually make a decision on yes or no.
if you select yes, something happens or no something else happens.
Code:
Private Sub msgDemo()
    Dim rtn As Long
    rtn = MsgBox("Your logon credentials have been checked and your application has been approved: Congratulations!" & vbCrLf & "Before leaving, would you like " & _
           "to take our survey survey now?", _
           VbMsgBoxStyle.vbYesNo Or VbMsgBoxStyle.vbQuestion)
    If rtn = vbYes Then
      'do code here for yes
      Debug.Print vbYes
    Else
      'do some code here for NO
      Debug.Print vbNo
    End If
End Sub
 
Note that you can choose to pre=select whichever option you want - generally the non-destructive answer will be best, so users won't inadvertently err just pressing enter. eg the follow makes NO the default action.

Code:
if msgbox ("pressing yes will catastrophically delete all of your data. " & vbcrlf & vbcrlf & _
            "Do you want to continue? ", vbQuestion + vbYesNo + vbdefaultbutton2,"Confirm Action")= vbno then
            exit sub
end if

As @MajP noted, you can use or or + to show multiple buttons is fine.

The main bubble options are vbQuestion, vbInformation, vbExclamation and vbCritical. You can only have one of these.
 
I'm pretty sure that Colin @isladogs has an example on his website, but due to recent structural changes to his website, I'm not sure how to get to it. I've posted his name here and that should alert him to this thread and hopefully he will provide you with some excellent information.
 
I'm alerted .... ;) ..and have even managed a URL unfurl successfully following Tony's excellent guidance last week
Have a look at the various examples in my example database:

Tony - the new website is in my signature line and its URL is https://www.isladogs.co.uk
 
On a more general note regarding Message Box buttons. You do not need to use the vbConstants, you can use numbers.
Which can be easier and of course shortens the MsgBox() line maybe also your code..

BUTTON TYPE : Simply add together to create button reqd.
0 = Ok only
1 = Ok & Cancel
2 = Abort, Retry, & Ignore
3 = Yes, No, Cancel
4 = Yes & No
5 = Retry & Cancel Icon to show
0 = Display no icon
16 = Critical <STOP>
32 = Warning Query <?>
48 = Warning Message <!>
64 = Information Message <!>

DEFAULT BUTTON
0 = First Button is default
256 = Second Button is Default
512 = Third Button is Default

BUTTON RETURN VALUES
1 = OK
2 = Cancel
3 = Abort
4 = Retry
5 = Ignore
6 = Yes
7 = No
 
After 25 years working with Access, I still find it hard to remember those values- easier for me to just enter what I need using the intellisense provided.

Interestingly someone at another forum asked how you could create a message box with no default button to force users to think & respond appropriately. ANSWER = YOU CAN'T ... at least not with a standard message box
 
I have a hazy recollection of seeing a Blog which explained a method where you could add up the numbers listed above. The new "single" number controlled the setup/display of your message box.

The name of this technique escapes me for the moment!!!!
 
The name of this technique escapes me for the moment!!!!

I think the word you want is "Obfuscation" - actions to hide what it is that you are actually doing. Using the names for those enumerated flags is how you know that the message box is doing. Using the raw numbers obscures that. It is one of the rare times when I think that something that makes you type MORE is preferable to something that lets you type LESS.
 
Obfuscation

I am anti obfuscation, as in my experience it's usually me that has to go back and try and work out what I was trying to hide! It's just a pointless exercise in making things difficult for yourself!

However the word or idea I was looking for, to give you a better idea of what I was thinking has something to do with binary numbers. EDIT:- I think.....
 
The name of this technique escapes me for the moment!!!!
It's commonly called addition. - The opposite of subtraction. ;-)
However, in this specific context the term obfuscation is even more correct.
It is one of the rare times when I think that something that makes you type MORE is preferable to something that lets you type LESS.
These times might become less rare, if you consider that code is read much more often than written. A 10/1 ratio is often assumed.
 
All I know is that I often put code aside while doing something else, which "snowballs" from minor icy roads to avalanche category. So several months later I have to come back and fix something or upgrade it or SOMETHING, and then I have ask myself , "Self,.... WTF were you even THINKING when you wrote this pile of dyspeptic dragon droppings.?" Which is where using named symbolic values helps you get back up to speed.
 
I appear to be on my own on this one! As I have used the numbers in Windows since the late 1990s, they are actually consistent throughout all of my code and not at all confusing to me. They also appear to be consistent with Microsoft as an alternative to Constants, and in some other languages I use or have used. The Constant names are however, not always the same, although clearly similar and understandable. The Title, Message and Type do however shift about language to language. Whatever suits really, not a problem to me.
 
The name of this technique escapes me for the moment!!!!
I am no expert on this, but AFAIK it is called a bitmask and not obfuscation.

This is an example.
Say you want vbretrycancel, vbquestion, and vbdefaultbutton2
then you have these values where column 2 is decimal, column 3 is binary notation
vbRetryCancel
5​
00000101
vbQuestion
32​
00100000
vbDefaultButton2
256​
10000000
if you add these up you get 293 which is 10100101

To determine what is included you can do an AND 293 to the original value and if the result is the same as the input then that feature is included. You can see that 5 and 293 returns 5, 32 and 293 returns 32, 256 and 293 returns 256.
5​
00000101
and 29310100101
5​
00000101
32​
00100000
and 29310100101
32​
00100000
256​
10000000
and 29310100101
256​
10000000
Recommend you OR them and not add them because some of the features include other features and when you double count you will not get what you expect.
 
Last edited:
x = MsgBox("message here", vbCritical + vbDefaultButton1 + vbYesNo, "Test")
 
Note that you can choose to pre=select whichever option you want - generally the non-destructive answer will be best, so users won't inadvertently err just pressing enter. eg the follow makes NO the default action.

Code:
if msgbox ("pressing yes will catastrophically delete all of your data. " & vbcrlf & vbcrlf & _
            "Do you want to continue? ", vbQuestion + vbYesNo + vbdefaultbutton2,"Confirm Action")= vbno then
            exit sub
end if

As @MajP noted, you can use or or + to show multiple buttons is fine.

The main bubble options are vbQuestion, vbInformation, vbExclamation and vbCritical. You can only have one of these.

Fantastic !!!
Thank you
 

Users who are viewing this thread

Back
Top Bottom