By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,707 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

'Spruce Up' Your Message Boxes!

ADezii
Expert 5K+
P: 8,669
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
  12.  
  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
Share this Article
Share on Google+
8 Comments


P: 1
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 = ""
  6.  
  7. s = "Msgbox('" & BoldPrompt & "@" & FirstLine & "@" & SecondLine & "@'," & Buttons & ",'" & Caption & "')"
  8. BoldMessageBox = Eval(s)
  9. End Function
  10.  
usage:

Expand|Select|Wrap|Line Numbers
  1. If BoldMessageBox("Test", "Bold", "nonBold", "", vbAbortRetryIgnore) = vbAbort Then
  2.   BoldMessageBox "Test", "Aborted"
  3. End If
  4.  
Aug 9 '09 #2

ADezii
Expert 5K+
P: 8,669
@Johar
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

TheSmileyCoder
Expert Mod 100+
P: 2,321
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

TheSmileyCoder
Expert Mod 100+
P: 2,321
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

P: 5
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

NeoPa
Expert Mod 15k+
P: 31,660
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
  6.  
  7.     s = "Msgbox('" & BoldPrompt & "@" & FirstLine & "@" & SecondLine & "@'," & Buttons & ",'" & Caption & "')"
  8.     BoldMessageBox = Eval(s)
  9. End Function
Dec 9 '12 #7

TheSmileyCoder
Expert Mod 100+
P: 2,321
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

NeoPa
Expert Mod 15k+
P: 31,660
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