470,563 Members | 2,004 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

'Spruce Up' Your Message Boxes!

8,800 Expert 8TB
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.
Expand|Select|Wrap|Line Numbers
  1. 'Code for 2 Paragraphs with OK, Cancel Buttons and an Information Icon, Default Button = 1 [OK]
  2. '(notice the double "@@")
  3.     'vbOKCancel = 1
  4.     'vbInformation = 64
  5.     'TOTAL Constant Value = 65
  6. If Eval("Msgbox('Paragraph 1/Line 1 - this Line will be in BOLD!@Paragraph 2/Line 2 - Click ""OK"" " & _
  7.         "to confirm your Delete or ""Cancel"" to UNDO your deletion.@@',65, 'Message Box Title')") = vbOK Then
  8.   MsgBox "You chose OK!"
  9. Else
  10.   MsgBox "You Canceled the previous Operation!"
  11. End If
  13. 'Code for 3 Paragraphs with Abort, Retry, Ignore Buttons and an Exclamation Icon
  14. 'Default Button = 2 [Retry], (each Paragraph separated by "@")
  15.     'vbAbortRetryIgnore = 2
  16.     'vbExclamation = 48
  17.     'vbDefaultButton2 = 256
  18.     'TOTAL Constant Value = 306
  19. Select Case Eval("Msgbox('Paragraph 1/Line 1 - this Line will be in BOLD!@Paragraph 2/Line 2 - Normal Text.@" & _
  20.                  "Paragraph 3/Line 3 - Normal Text.@',306, " & _
  21.                  "'Message Box Title')")
  22.   Case vbAbort
  23.     MsgBox "You Aborted the previous Operation!"
  24.   Case vbRetry
  25.     MsgBox "Way to go! Let's give it another try!"
  26.   Case vbIgnore
  27.     MsgBox "You chose to Ignore the previous Operation!"
  28. End Select
Apr 29 '08 #1
8 11623
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.
Expand|Select|Wrap|Line Numbers
  1. Public Function BoldMessageBox(Caption As String, BoldPrompt As String, Optional FirstLine As String, Optional SecondLine As String, Optional Buttons As VbMsgBoxStyle) As VbMsgBoxResult
  2. Dim s As String
  3. If IsMissing(Buttons) Then Buttons = vbOKOnly
  4. If IsMissing(FirstLine) Then FirstLine = ""
  5. If IsMissing(SecondLine) Then SecondLine = ""
  7. s = "Msgbox('" & BoldPrompt & "@" & FirstLine & "@" & SecondLine & "@'," & Buttons & ",'" & Caption & "')"
  8. BoldMessageBox = Eval(s)
  9. End Function

Expand|Select|Wrap|Line Numbers
  1. If BoldMessageBox("Test", "Bold", "nonBold", "", vbAbortRetryIgnore) = vbAbort Then
  2.   BoldMessageBox "Test", "Aborted"
  3. End If
Aug 9 '09 #2
8,800 Expert 8TB
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.
Aug 9 '09 #3
2,322 Expert Mod 2GB
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.
Sep 12 '12 #4
2,322 Expert Mod 2GB
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?
Sep 13 '12 #5
6 Nibble
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).
Dec 5 '12 #6
32,295 Expert Mod 16PB
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 :
Expand|Select|Wrap|Line Numbers
  1. Public Function BoldMessageBox(Caption As String, BoldPrompt As String, _
  2.                                Optional FirstLine As String = "", _
  3.                                Optional SecondLine As String = "", _
  4.                                Optional Buttons As VbMsgBoxStyle = vbOKOnly) As VbMsgBoxResult
  5.     Dim s As String
  7.     s = "Msgbox('" & BoldPrompt & "@" & FirstLine & "@" & SecondLine & "@'," & Buttons & ",'" & Caption & "')"
  8.     BoldMessageBox = Eval(s)
  9. End Function
Dec 9 '12 #7
2,322 Expert Mod 2GB
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.
Dec 11 '12 #8
32,295 Expert Mod 16PB
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.
Dec 11 '12 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Andre Nogueira | last post: by
3 posts views Thread by Brian Mitchell | last post: by
12 posts views Thread by jburkle | last post: by
5 posts views Thread by HowardChr | last post: by
2 posts views Thread by =?Utf-8?B?TmF0aGFuIFdpZWdtYW4=?= | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.