Message Box (1 Viewer)

kitty77

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

Thanks...
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 23:43
Joined
Jul 9, 2003
Messages
16,287
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:-

 

oleronesoftwares

Passionate Learner
Local time
Today, 15:43
Joined
Sep 22, 2014
Messages
1,159
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
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:43
Joined
May 21, 2018
Messages
8,554
@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
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 23:43
Joined
Sep 12, 2006
Messages
15,660
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.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 23:43
Joined
Jul 9, 2003
Messages
16,287
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.
 

isladogs

MVP / VIP
Local time
Today, 23:43
Joined
Jan 14, 2017
Messages
18,246
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
 

Cotswold

Active member
Local time
Today, 23:43
Joined
Dec 31, 2020
Messages
528
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
 

isladogs

MVP / VIP
Local time
Today, 23:43
Joined
Jan 14, 2017
Messages
18,246
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
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 23:43
Joined
Jul 9, 2003
Messages
16,287
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_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:43
Joined
Feb 28, 2001
Messages
27,223
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.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 23:43
Joined
Jul 9, 2003
Messages
16,287
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.....
 

sonic8

AWF VIP
Local time
Tomorrow, 00:43
Joined
Oct 27, 2015
Messages
998
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.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:43
Joined
Feb 28, 2001
Messages
27,223
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.
 

Cotswold

Active member
Local time
Today, 23:43
Joined
Dec 31, 2020
Messages
528
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.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:43
Joined
May 21, 2018
Messages
8,554
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:

June7

AWF VIP
Local time
Today, 14:43
Joined
Mar 9, 2014
Messages
5,488
x = MsgBox("message here", vbCritical + vbDefaultButton1 + vbYesNo, "Test")
 

Harry Taylor

Registered User.
Local time
Today, 23:43
Joined
Jul 10, 2012
Messages
73
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

Top Bottom