Connecting Tech Pros Worldwide Forums | Help | Site Map

'Spruce Up' Your Message Boxes!

ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,224
#1   Apr 29 '08
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



Newbie
 
Join Date: Aug 2009
Posts: 1
#2   Aug 9 '09

re: 'Spruce Up' Your Message Boxes!


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.  
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,224
#3   Aug 9 '09

re: 'Spruce Up' Your Message Boxes!


Quote:

Originally Posted by Johar View Post

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.  

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.
Reply