Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

MsgBox with Options Menu

Introduction.

This article is a follow-up to my earlier post on using the MsgBox With the Office Assistant.
In this installment, we’ll explore how to leverage the Office Assistant to display a balloon-style menu with multiple options for the user to choose from.

When the user clicks on one of the presented options, our VBA code detects the selected value and executes the corresponding programmed action. This technique can be a fun and interactive way to enhance user engagement in your MS Access applications.

Below is a sample screenshot of the Balloon Menu displayed through the Office Assistant:

Preparing for a Demo.

Design a simple form with a Text Box and a Command Button, as shown in the illustration above, to run the demo program.

  1. Rename the Text Box

    • Click on the Text Box and open the Property Sheet (View → Properties).

    • Change the Name property value to OptTxt.

    • This control will be used only for displaying the selected option text during the demo.

  2. Rename the Command Button

    • Click on the Command Button and open its Property Sheet.

    • Change the Name property value to cmdOpt.

    • Change the Caption property value to Get Option.

  3. Add the VBA Code

    • While the sample form is still in Design View, open the form’s VBA module (View → Code).

    • Copy and paste the code provided below into the form’s module.

    • Save the form with the name OptionCheck, or choose any other name you prefer.

Private Sub cmdOpt_Click()
Dim OptionArray(1 To 5) As String
Dim i As Integer, msg As String, title As String

OptionArray(1) = "Display Report Source Data"
OptionArray(2) = "Print Preview"
OptionArray(3) = "Print"
OptionArray(4) = "CANCEL"

msg = "Please Select an Option"title = "
Report Options"

i = MsgGetOpt(msg, title, OptionArray())

If i > 0 Then
    Me![optTxt] = i & " : " & OptionArray(i)
End If

Select Case i
    Case 1
      'DoCmd.OpenForm "DataForm", acNormal
       msg = "Display Report Source Data"
    Case 2
       'DoCmd.OpenReport "myReport", acViewPreview
        msg = "Print Preview"
    Case 3
       'DoCmd.OpenReport "myReport", acViewNormal
        msg = "Print"
    Case 4
        msg = "CANCEL"
End Select

    MsgOK "selected: " & msg

End Sub

The VBA Code

Let’s break down what happens in the above routine.

First, we define a String array variable  OptionArray with a maximum of five elements, and then load it with values in four of those elements. The main program MsgGetOpt() (code provided below) supports a maximum of five options. Even if you assign more than five elements to OptionArray the statement:

vba
k = IIf(k > 5, 5, k)

will limit the number of options to five.

Next, we initialize the variables msg and Title With the option text and the dialog box title. We then call the MsgGetOpt() program, passing these variables, along with the OptionArrayby reference. This ensures that the option values are sent directly into the routine for display.

If you have already completed the MsgBox with Office Assistant and Command Button Animation examples from earlier articles, you may proceed directly to step 3. However, make sure that you copy and paste these VBA routines into the same project where the main programs were placed earlier. If required, you can copy the main program code again from the following pages.

Setup instructions:

  1. Copy the main programs from the page MsgBox with Office Assistant into a Global Module in your project, then save the module.

  2. Link the Microsoft Office Library to your project. The procedure for doing this is described on the Command Button Animation page, which also lists other essential library references.

  3. Copy and paste the following MsgGetOpt() code into a VBA Global Module, and save it.

Public Function MsgGetOpt(ByVal strText As String, ByVal strTitle As String, ByRef MaxArray5obj) As Integer
Dim intVal As Integer, X As Integer, Bal As Balloon, k As Integer
On Error GoTo MsgGetOpt_Err

k = UBound(MaxArray5obj)
k = IIf(k > 5, 5, k)

With Assistant
  If .On = False Then
    .On = True    
   '.FileName = "OFFCAT.acs"
    .Animation = msoAnimationBeginSpeaking
    .AssistWithHelp = True
    .GuessHelp = True
    .FeatureTips = False
    .Visible = True
  End If
End With

Set Bal = Assistant.NewBalloon
With Bal
   .Animation = msoAnimationWritingNotingSomething
   .Icon = msoIconAlert
   .Heading = strTitle
   .Text = strText
   .BalloonType = msoBalloonTypeButtons
   For X = 1 To k
    If Len(MaxArray5obj(X)) > 0 Then
     .labels(X).Text = MaxArray5obj(X)
    End If
   Next
   .Button = msoButtonSetNone
    intVal = .Show
End With

Assistant.Visible = False
MsgGetOpt = intVal

Set Bal = Nothing

MsgGetOpt_Exit:
Exit Function

MsgGetOpt_Err:
Err.Clear
Resume MsgGetOpt_Exit
End Function

Understanding the MsgGetOpt() Routine.

The MsgGetOpt() Routine is the core program that displays a balloon-style menu using the Office Assistant and allows the user to select from up to five options.

Here’s how it works:

  1. Limit on Options – Although you can pass more than five values in the OptionArray The routine automatically restricts the total to five using the line:

    vba
    k = IIf(k > 5, 5, k)

    This ensures the display remains neat and fits within the Office Assistant’s balloon window.

  2. Balloon Setup – The routine uses the msg  Title values you pass to it to create the balloon message and set the window’s title bar text.

  3. Loading the Options – Each element of the OptionArray (up to five) is assigned to the balloon’s menu items, making them clickable choices for the user.

  4. Displaying the Menu – The Office Assistant appears with the configured balloon, presenting the options to the user in a visually friendly way.

  5. Capturing the User’s Choice – When the user clicks one of the options, the routine detects the selection and returns its value, so your program can take the appropriate action.

By organizing this functionality into a reusable MsgGetOpt() routine, you can call it from any form or module simply by passing the required message text and option values.

To see it in action, open the Demo Form named OptionCheck and click its Command Button. The Office Assistant-based message box will appear, displaying the available options, just like the sample image shown at the top of this page.

When you click one of the options:

  1. A standard MsgBox will appear, showing the text of the option you selected.

  2. The same selected option text, along with its corresponding sequence number, will also be displayed in the form’s TextBox.

  3. Internally, the user’s selection is returned into the variable i inside the cmdOpt_Click() subroutine.

A Select Case ... End Select structure then evaluates the value  i to determine which option was chosen and executes the appropriate block of code for that selection.

This approach makes the process interactive, user-friendly, and easy to adapt for any set of options you want to present.

Next, we will look into the CheckBoxes with the Office Assistant.

Download the Demo Database.


Share:

No comments:

Post a Comment

Comments subject to moderation before publishing.

PRESENTATION: ACCESS USER GROUPS (EUROPE)

Translate

PageRank

Post Feed


Search

Popular Posts

Blog Archive

Powered by Blogger.

Labels

Forms Functions How Tos MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Class Module External Links Queries Array msaccess reports Accesstips WithEvents msaccess tips Downloads Objects Menus and Toolbars Collection Object MsaccessLinks Process Controls Art Work Property msaccess How Tos Combo Boxes Dictionary Object ListView Control Query VBA msaccessQuery Calculation Event Graph Charts ImageList Control List Boxes TreeView Control Command Buttons Controls Data Emails and Alerts Form Custom Functions Custom Wizards DOS Commands Data Type Key Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Command Button Report msaccess menus msaccessprocess security advanced Access Security Add Auto-Number Field Type Form Instances ImageList Item Macros Menus Nodes RaiseEvent Recordset Top Values Variables Wrapper Classes msaccess email progressmeter Access2007 Copy Excel Export Expression Fields Join Methods Microsoft Numbering System Records Security Split SubForm Table Tables Time Difference Utility WScript Workgroup database function msaccess wizards tutorial Access Emails and Alerts Access Fields Access How Tos Access Mail Merge Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Common Controls Conditional Formatting Data Filtering Database Records Defining Pages Desktop Shortcuts Diagram Disk Dynamic Lookup Error Handler External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Message Box Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif ms-access msaccess msaccess alerts pdf files reference restore switch text toolbar updating upload vba code