Custom Msgbox inquiry (1 Viewer)

kirkm

Registered User.
Local time
Today, 19:21
Joined
Oct 30, 2008
Messages
1,324
I wonder if anyone is familiar with the custom MsgBox here


It works brilliantly, with one limitation. You can have 3 buttons but the third one always returns Cancel. The same as closing the MsgBox.
I've spent hours trying to figure out how to distinguish the 3rd button from "X" Close but failed.
Is it possible anyhow ?
 

Ranman256

Well-known member
Local time
Today, 02:21
Joined
Apr 9, 2015
Messages
3,936
vRet = MsgBox("message", vbYesNoCancel, "title")
 

kirkm

Registered User.
Local time
Today, 19:21
Joined
Oct 30, 2008
Messages
1,324
I don't think you understood my question.
 

moke123

AWF VIP
Local time
Today, 02:21
Joined
Jan 11, 2013
Messages
2,562
Here's a working demo.

Whats the code your calling it with?
Code:
Me.txtNum = fCustomMsgBox(3, "One,Two,Three", "Return a numerical value", "Numbers", False, vbExclamation)
To get the text, use true as your argument and it returns the label value , Use false and it returns the constants for the buttons (listed in the module)
 

Attachments

  • CustomNativeMessageBox.accdb
    436 KB · Views: 24

Sun_Force

Active member
Local time
Today, 15:21
Joined
Aug 29, 2020
Messages
315
@moke123
The question was why if one click the x button of the message box, it returns the value of the third button?
If I click x on "How many Scoops?", it returns 3
if I click x on "Select a Flavor", it returns Strawberry.
and so on.

It's from OP's Post:
how to distinguish the 3rd button from "X" Close
 

moke123

AWF VIP
Local time
Today, 02:21
Joined
Jan 11, 2013
Messages
2,562
The simple answer is closing the messagebox with the x is interpreted as cancel.
The message box returns the constants of the native messagebox. For three buttons the underlying messagebox is vbYesNoCancel. 1 for yes, 2 for no, and 3 for cancel. Notice that a 2 button messagebox based on a vbyesno has no cancel choice and the x is greyed out.

you can change the code to use the vbAbortRetryIgnore option which has a greyed out X.

here's what you need to change
Code:
    Case 3    ' Three Buttons

'        MsgBoxCustom_Set vbYes, varBL(0)
'        MsgBoxCustom_Set vbNo, varBL(1)
'        MsgBoxCustom_Set vbCancel, varBL(2)
'        MsgBoxCustom ans, vPrompt, vbYesNoCancel + Icon, vTitle

' changed the constants for the buttons

        MsgBoxCustom_Set vbAbort, varBL(0)
        MsgBoxCustom_Set vbRetry, varBL(1)
        MsgBoxCustom_Set vbIgnore, varBL(2)
        MsgBoxCustom ans, vPrompt, vbAbortRetryIgnore + Icon, vTitle

        Select Case ans

        Case 6 'yes

            If ReturnCaption = False Then
                fCustomMsgBox = 1
            Else
                fCustomMsgBox = CStr(varBL(0))
            End If

        Case 7 'no

            If ReturnCaption = False Then
                fCustomMsgBox = 2
            Else
                fCustomMsgBox = varBL(1)
            End If

        Case 2 'cancel

            If ReturnCaption = False Then
                fCustomMsgBox = 3
            Else
                fCustomMsgBox = varBL(2)
            End If
         
'Added case selections for vbAbortRetryIgnore

          Case 3 'Abort

            If ReturnCaption = False Then
                fCustomMsgBox = 1
            Else
                fCustomMsgBox = varBL(0)
            End If
         
           Case 4 'retry

            If ReturnCaption = False Then
                fCustomMsgBox = 2
            Else
                fCustomMsgBox = varBL(1)
            End If
         
           Case 5 'ignore

            If ReturnCaption = False Then
                fCustomMsgBox = 3
            Else
                fCustomMsgBox = varBL(2)
            End If
       

        End Select

    End Select

End Function

EDIT: I have to look this over for a bit to see why some values are returning the wrong selection.
EDIT_EDIT: Found it. a copy and paste error. I fixed the above code.
 
Last edited:

moke123

AWF VIP
Local time
Today, 02:21
Joined
Jan 11, 2013
Messages
2,562
I couldn't think of any reason to keep the messagebox with the X, so I updated the demo to have the disabled X.
 

Attachments

  • CustomNativeMessageBox_V2.zip
    38.3 KB · Views: 18
Last edited:

kirkm

Registered User.
Local time
Today, 19:21
Joined
Oct 30, 2008
Messages
1,324
If you disable the X you force the user to select one of the 3 buttons.
If X is enabled Closing it returns the same value as the 3rd button.
Ideally you want to be able choose any of the buttons, or Close it selecting nothing.
 

Gasman

Enthusiastic Amateur
Local time
Today, 07:21
Joined
Sep 21, 2011
Messages
9,062
If you disable the X you force the user to select one of the 3 buttons.
If X is enabled Closing it returns the same value as the 3rd button.
Ideally you want to be able choose any of the buttons, or Close it selecting nothing.
Still one click away? and forcing the user to make a choice out of the only choices allowed/available is not such a bad thing, surely?
 

kirkm

Registered User.
Local time
Today, 19:21
Joined
Oct 30, 2008
Messages
1,324
Well, no if that's all you want.
But I want to know if it's been closed or not. As it is it's no use, but worth asking here just in case somebody knew.
 

isladogs

CID VIP
Local time
Today, 07:21
Joined
Jan 14, 2017
Messages
15,408
Alternatively just create your own customised message form. This example shows some advantages of doing so..

1634284056252.png
 

sxschech

Registered User.
Local time
Yesterday, 23:21
Joined
Mar 2, 2010
Messages
718
This is probably not as polished as Colin's. Here is a stripped down demo of what I've been using.
  • It can do up to 4 buttons, and as Colin mentioned, can be modified for more
  • It can use a keyword or keywords to define a default set focus button to highlight (this part is commented out in the demo and was altered so would need to be edited for the current need)
  • You can hide buttons that aren't needed by using NA and can choose which ones to display such as 1st and 3rd if you want space between them
  • It can also be used like a standard message box with only the ok button
  • It can self close after a default set amount of time (used when only informing and doesn't require user interaction)
  • It can self close after a set amount of time as defined by the user/developer/coder
  • Currently only has two icons - Question and Information. Would need to alter code to display other message icons
Beside the form, only other thing needed is to copy the global variable into a standard module. This is what is stored so that can be used in downstream code with a value of 1 through 4. Code uses openargs and pipe as a delimiter.

Here are some use examples.

Example 1: (User clicks OK button)
Code:
DoCmd.OpenForm "frmMessageTimer", , , , , acWindowNormal, "Put this in correct thread for a Response To|Information|RESPONSE TO|NoTimer"


Example 2: (Form closes at default time amount)
Code:
DoCmd.OpenForm "frmMessageTimer", , , , , acWindowNormal, "For same day requests, please inform Doc Control in the body of the message.|Information|Same Day Request|Timer"


Example 3: (Form closes after user defined time amount)
Code:
DoCmd.OpenForm "frmMessageTimer", , , , , acWindowNormal, "Please check to be sure that this number is next in sequence.  If the number already exists, need to determine if there is an issue such as someone inadvertantly taking an RFI number rather than RSR or GNC.|Information|Verify RFI Sequence Number|Timer|15000"


Example 4: (Use All 4 buttons with custom captions)
Code:
'By definition buttonchoice is treated as NoTimer since requires user to make decision
'so is placed in the NoTimer slot if buttons are needed


    DoCmd.OpenForm "frmMessageTimer", , , , , acDialog, "Please review the Word doc and make any other relevant changes " & _
                    "if needed BEFORE clicking OK." & vbCrLf & vbCrLf & _
                    "Click Don't Save to cancel without saving for cases where RFI needs clarification in order to proceed." & vbCrLf & vbCrLf & _
                    "Click Reject to cancel without saving and send email back to originator noting the issues to be corrected." & _
                    "|Question|Review Document|ButtonChoice|B1:Looks OK|B2:Don't Save|B3:Reject|B4:Cancel"
    yesno = gblButtonChoice
    
    If yesno = 2 Or yesno = 4 Then
        'May add this in future if have time to build a list of issues, but may
        'be TMI
        '20190123
        GoTo Error_Handler_Exit
     ElseIf yesno = 3 Then
        hasIssues = True
        GoTo Error_Handler_Exit
    End If

Example 5: Display two buttons, but not consecutively (1st and 3rd).
Can choose any combination such as:
1st and 2nd
1st and 4th
2nd and 3rd
2nd and 4th
Code:
DoCmd.OpenForm "frmMessageTimer", , , , , acDialog, "Please choose a button to continue.|Question|Review Document|ButtonChoice|B1:Looks OK|B2:NA|B3:Cancel|B4:NA"
 

Attachments

  • CustomMsgBoxDemo.accdb
    512 KB · Views: 19

Sun_Force

Active member
Local time
Today, 15:21
Joined
Aug 29, 2020
Messages
315
and forcing the user to make a choice out of the only choices allowed/available?

User should be able to cancel what he's doing anytime and at any stage of a running code. You can't force him to continue when he need to quit running the rest of code.
If windows allows only 3 buttons, a cancel button is necessary. That's why that X button is sitting there. Isn't it?

@moke123 's sample file above is a good example. The code asks for three flavor and the message box has three button for each flavor. What if the user doesn't like any of them and need to cancel ordering? Do you expect him to finish the order and pay for something he doesn't like?
That's when the X is necessary for a choice out of the only choices allowed.
 

kirkm

Registered User.
Local time
Today, 19:21
Joined
Oct 30, 2008
Messages
1,324
Thank you Sun_Force that's what I was getting at right from Msg #1 and though it would be blindingly obvious.
 

moke123

AWF VIP
Local time
Today, 02:21
Joined
Jan 11, 2013
Messages
2,562
That's when the X is necessary for a choice out of the only choices allowed.
The solution there is that you make Cancel one of the choices in the command buttons. Personally, I dont think I would ever include X-ing out of something as a choice.

Thank you Sun_Force that's what I was getting at right from Msg #1 and though it would be blindingly obvious.
It works brilliantly, with one limitation. You can have 3 buttons but the third one always returns Cancel. The same as closing the MsgBox.
I've spent hours trying to figure out how to distinguish the 3rd button from "X" Close but failed.
Is it possible anyhow ?
A couple simple experiments shows that the X functions as designed. Dont forget that the procedure your referencing is using the native message box and is only changing the button labels. The buttons themselves still return their numeric value regardless of the button caption.
You'll also notice that not all messagebox options have the X enabled. For instance a yes/no messagebox disables the X. An ok/cancel messagebox allows the X but choosing the X returns the cancel value (2).

The button constants are vbOK = 1, vbCancel = 2, vbAbort = 3, vbRetry = 4, vbIgnore = 5, vbYes = 6, vbNo = 7
If you run this code from a command button you'll see that the X is functioning as designed.
Click the X where available.
Code:
Dim y
y = MsgBox("x", vbAbortRetryIgnore)
MsgBox "value = " & y
y = MsgBox("x", vbOKCancel)
MsgBox "value = " & y
y = MsgBox("x", vbOKOnly)
MsgBox "value = " & y
y = MsgBox("x", vbRetryCancel)
MsgBox "value = " & y
y = MsgBox("x", vbYesNo)
MsgBox "value = " & y
y = MsgBox("x", vbYesNoCancel)
MsgBox "value = " & y
 

kirkm

Registered User.
Local time
Today, 19:21
Joined
Oct 30, 2008
Messages
1,324
Yes, but that's not what was asked. I wanted 3 choices plus Close and was that possible. Implying some modification may have achieved that.
If you only want 2 or 1 button it's perfect.
 

isladogs

CID VIP
Local time
Today, 07:21
Joined
Jan 14, 2017
Messages
15,408
The CustomMsgBox doesn't add any extra functionality to a standard message box other than allow you to change the button labels.

If you run this simple code on a standard message box you will see that clicking Cancel or the X do EXACTLY the same thing.
Code:
Sub MsgBoxTest()

Dim strRet As String
strRet = MsgBox("3 button test", vbYesNoCancel, "Test")

    If strRet = vbYes Then
        MsgBox "Yes"
    ElseIf strRet = vbNo Then
        MsgBox "No"
    Else
        MsgBox "Cancel"
    End If
End Sub

As stated earlier, if you want more than that, you need to create your own customised message form
 
Last edited:

moke123

AWF VIP
Local time
Today, 02:21
Joined
Jan 11, 2013
Messages
2,562
Is it possible anyhow ?
The answer is no.

Implying some modification may have achieved that.
When I write code I try to tightly control the actions available to the user. You specifically referenced J. Woolley's procedure with which I'm very familiar with. In researching your question, I noticed what I would consider a flaw in the original authors design. He used vbYesNoCancel which has an enabled X. I changed it to vbAbortRetryIgnore which has the X disabled. I wouldn't expect a user to use the X for anything when I'm specifically giving them choices. IMO, it's bad design. If I want cancel as an option, I give it to them.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 02:21
Joined
Feb 19, 2002
Messages
33,213
User should be able to cancel what he's doing anytime and at any stage of a running code. You can't force him to continue when he need to quit running the rest of code.
I once had a programmer who brought down thousands of users by locking the entire CICS network because he never gave the user a graceful way out. His program went into a tight loop and when it had sucked up all available memory, the system crashed. The testing team revised their procedures as a result of this debacle.

For the form itself, the user can always use Esc, Esc. as long as you don't block it but for a dialog box, they may need a no action, action.
 

Users who are viewing this thread

Top Bottom