'Spruce Up' Your Message Boxes!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    'Spruce Up' Your Message Boxes!

    Have you ever wondered how Microsoft Access displays those Custom Message Boxes with the first line in BOLD, and the second and/or second and third lines in Normal Font Weight? The answer lies in the unique ability to break up your Message Text into either two or three Paragraphs using the "@" symbol, then evaluating the entire expression using the Eval() Function.

    The "@" symbol inserted into your Message Text will break the Message into Paragraphs, with the Text before the first "@" shown in BOLD. Subsequent Paragraphs (you are limited to three) must be followed by the "@" symbol. If you only want to break for two Paragraphs, you must use two "@" symbols at the end of the second Paragraph. Simply download the Attachment to actually see how this is accomplished. The Attached Code can also be used as a Template for your future 'Spruced Up' Message Boxes!

    Special Considerations:
    1. You cannot use Variables in your Message Boxes with this Method.
    2. You cannot use the VB Intrinsic Constants such as vbOKCancel, these Constants must be given as specific numbers which you can readily reference in the Help Files or Object Browser.

    [CODE=vb]
    'Code for 2 Paragraphs with OK, Cancel Buttons and an Information Icon, Default Button = 1 [OK]
    '(notice the double "@@")
    'vbOKCancel = 1
    'vbInformation = 64
    'TOTAL Constant Value = 65
    If Eval("Msgbox('P aragraph 1/Line 1 - this Line will be in BOLD!@Paragraph 2/Line 2 - Click ""OK"" " & _
    "to confirm your Delete or ""Cancel"" to UNDO your deletion.@@',65 , 'Message Box Title')") = vbOK Then
    MsgBox "You chose OK!"
    Else
    MsgBox "You Canceled the previous Operation!"
    End If

    'Code for 3 Paragraphs with Abort, Retry, Ignore Buttons and an Exclamation Icon
    'Default Button = 2 [Retry], (each Paragraph separated by "@")
    'vbAbortRetryIg nore = 2
    'vbExclamation = 48
    'vbDefaultButto n2 = 256
    'TOTAL Constant Value = 306
    Select Case Eval("Msgbox('P aragraph 1/Line 1 - this Line will be in BOLD!@Paragraph 2/Line 2 - Normal Text.@" & _
    "Paragraph 3/Line 3 - Normal Text.@',306, " & _
    "'Message Box Title')")
    Case vbAbort
    MsgBox "You Aborted the previous Operation!"
    Case vbRetry
    MsgBox "Way to go! Let's give it another try!"
    Case vbIgnore
    MsgBox "You chose to Ignore the previous Operation!"
    End Select[/CODE]
  • Johar
    New Member
    • Aug 2009
    • 1

    #2
    Replacement of standard message box

    Thank you for this insightful article. Based on your information I wrote a quick function, that almost replaces the normal messagebox (works in Access 2007 - older versions may not be able to understand the optional-parameter; just remove the optional keywords and the IfMissing-codelines.

    Here is the code. Simply put it in a standard code module and enjoy.
    Code:
    Public Function BoldMessageBox(Caption As String, BoldPrompt As String, Optional FirstLine As String, Optional SecondLine As String, Optional Buttons As VbMsgBoxStyle) As VbMsgBoxResult
    Dim s As String
    If IsMissing(Buttons) Then Buttons = vbOKOnly
    If IsMissing(FirstLine) Then FirstLine = ""
    If IsMissing(SecondLine) Then SecondLine = ""
    
    s = "Msgbox('" & BoldPrompt & "@" & FirstLine & "@" & SecondLine & "@'," & Buttons & ",'" & Caption & "')"
    BoldMessageBox = Eval(s)
    End Function
    usage:

    Code:
    If BoldMessageBox("Test", "Bold", "nonBold", "", vbAbortRetryIgnore) = vbAbort Then
      BoldMessageBox "Test", "Aborted"
    End If

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by Johar
      Thank you for this insightful article. Based on your information I wrote a quick function, that almost replaces the normal messagebox (works in Access 2007 - older versions may not be able to understand the optional-parameter; just remove the optional keywords and the IfMissing-codelines.

      Here is the code. Simply put it in a standard code module and enjoy.
      Code:
      Public Function BoldMessageBox(Caption As String, BoldPrompt As String, Optional FirstLine As String, Optional SecondLine As String, Optional Buttons As VbMsgBoxStyle) As VbMsgBoxResult
      Dim s As String
      If IsMissing(Buttons) Then Buttons = vbOKOnly
      If IsMissing(FirstLine) Then FirstLine = ""
      If IsMissing(SecondLine) Then SecondLine = ""
      
      s = "Msgbox('" & BoldPrompt & "@" & FirstLine & "@" & SecondLine & "@'," & Buttons & ",'" & Caption & "')"
      BoldMessageBox = Eval(s)
      End Function
      usage:

      Code:
      If BoldMessageBox("Test", "Bold", "nonBold", "", vbAbortRetryIgnore) = vbAbort Then
        BoldMessageBox "Test", "Aborted"
      End If
      Nice job, Johar. I particularly like that way you encapsulated this functionality within a Function Call, and allowed for Optional Arguments. Thanks for enhancing the usefulness of this Thread - I will actually use this approach more often now, since you have made it easier to implement.

      Comment

      • TheSmileyCoder
        Recognized Expert Moderator Top Contributor
        • Dec 2009
        • 2322

        #4
        I can't believe I haven't stumbled across this information earlier. Very nice to know that I don't have to create a custom form to get a bigger degree of variety in my msgbox's.

        Comment

        • TheSmileyCoder
          Recognized Expert Moderator Top Contributor
          • Dec 2009
          • 2322

          #5
          Can anyone explain why exactly it is that the simple messagebox gets its functionality expanded by being put into a eval function call?

          Are there other functions that you could eval to get more value?

          Comment

          • Hoopla3000
            New Member
            • Oct 2012
            • 7

            #6
            Be careful when using the formatted message box. If the content you want to display contains the '@' symbol, it will mess up the formatting. Maybe you could adjust your function so if the passed parameters contain @, it reverts to the standard message box (without the eval() function).

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Re: Code in Post #2.
              I'm not sure how this was missed, probably because the effect is nil anyway, but IsMissing() will always return False when passed a string variable. Only Variants can store the Missing flag. For non-Variant variables the syntax to use would be :
              Code:
              Public Function BoldMessageBox(Caption As String, BoldPrompt As String, _
                                             Optional FirstLine As String = "", _
                                             Optional SecondLine As String = "", _
                                             Optional Buttons As VbMsgBoxStyle = vbOKOnly) As VbMsgBoxResult
                  Dim s As String
              
                  s = "Msgbox('" & BoldPrompt & "@" & FirstLine & "@" & SecondLine & "@'," & Buttons & ",'" & Caption & "')"
                  BoldMessageBox = Eval(s)
              End Function

              Comment

              • TheSmileyCoder
                Recognized Expert Moderator Top Contributor
                • Dec 2009
                • 2322

                #8
                Technically there is no reason to set the default value of optional string arguments to an empty string, since they are allready an empty string. Unlike a variant variable, string variables (and number variables) cannot be assigned a null value, it will throw an error.

                That doesn't mean it can't make sense to write it as NeoPa has done, sometimes it can help to illustrate the intent of the code to write it as NeoPa has.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  Absolutely. Spot on.

                  Particularly in the forums, I find it makes sense to be explicit about such things as it's easier to follow, even if, on the surface, it seems redundant.

                  Comment

                  Working...