Contents: Word’s Built-In Dialog Boxes- MsgBox ( ) Function
|
Chapter 9: VBA- Dialog Boxes and API Calls One time-saving feature of VBA is its ability to create forms and dialog boxes to gather data for your programs. There are also built-in dialog boxes in both Word and in Windows which can extend your EMR's capabilities tremendously.Word’s Built-In Dialog Boxes- MsgBox ( ) FunctionThe objective of MsgBox is to produce a pop-up message box and prompt the user to click on a command button before he /she can continues. This message box format is as follows: MsgBox(Prompt, Style Value, Title). Here is a code sample: intReturn = MsgBox("Do you wish to delete this record?", vbQuestion + vbYesNo, "Add Appointments Form") The first argument, Prompt, will display the message in the message box. The Style Value will determine what type of command buttons appear on the message box, please refer Table 10.1 for types of command button displayed. The Title argument will display the title of the message board. Here is a table delineating the various style values:
You can use a named constant in place of integers for the second argument to make the programs more readable. For example, you can use either “0” or “vbOKOnly”. In the sample shown on the preceding page, intReturn is variable that holds values that are returned by the MsgBox ( ) function. Here is a code sample: intReturn = MsgBox("Diagnosis " & Chr(34) & strAuth & Chr(34) & " is not in the system." & " Do you want to add this diagnosis?", vbQuestion + vbYesNo, "Add Diagnosis Form") If intReturn = vbYes Then DoCmd.OpenForm FormName:="DxAddP", DataMode:=acAdd, WindowMode:=acDialog, OpenArgs:=strAuth Response = acDataErrAdded Exit Sub End If Response = acDataErrDisplay Here is how this sample looks like:
Word’s Built-In Dialog Boxes- The InputBox( ) Function An InputBox( ) function will display a message box where the user can enter a value or a message in the form of text. The format is myMessage=InputBox(Prompt, Title, default_text, x-position, y-position). Here is a code sample: ' Initialize string. strMsg = "The [LUNAME] (i.e. lookup name field) is empty." & Chr(13) & "Press OK to enter a new value." ' Prompt user for input. strInput = InputBox("Enter in a new value for the empty [LUNAME] field." & Chr(13) & "The [LUNAME] consists of 1st 4 letters of the patient " & Chr(13) & "last name plus the 1st 4 letters of the patient first" & Chr(13) & "name and the numbers representing the patient" & Chr(13) & " date of birth. Example: the LookUp Name for this " & Chr(13) & "patient would normally beà " & Left([LNAME], 4) & Left([FNAME], 4) & MONTH([DOB]) & Day([DOB]) & YEAR([DOB])) ' Determine if user chose "Cancel". If strInput <> "" Then ' Test value of user input. 'strmsg = Left([LNAME], 4) & Left([FNAME], 4) & MONTH([DOB]) & Day([DOB]) & YEAR([DOB]) Do While Len(strInput) < 2 If MsgBox(strMsg, vbOKCancel, "Error!") = vbOK Then strInput = InputBox("Enter in a new value for the [LUNAME].") Else Exit Sub End If Loop ' Display user's correct input. MsgBox "You entered this value for the [LUNAME]: " & strInput & "." [LUNAME] = strInput Forms![*Genl Info & Insurance]![Combo211] = Forms![*Genl Info & Insurance]![LUNAME] Else Exit Sub End If The strMsg variable is a variant data type but typically it is declared as string, which accept the message input by the users. The arguments are explained as follows:
Here is how this sample looks like:
In the MWEP only one of the dialogs from the list above was used. The following sets the printer (in the current version of MWEP this has been hidden). This functionality was added into Office 2003: With
Dialogs(wdDialogFilePrintSetup)
Accessing the Windows API
in VBA
Microsoft bundled many more DLLs which are accessible through the
Windows Operating System, however, they are separated into numerous
other system DLL files, which is out of the scope of this
discussion. Because of the multi-threaded mature of the Windows
operating system, several programs can access the Windows API
simultaneously. The following API call was placed in the splash screen to bring up the windows szDialogTitle which allows the user to search for the needed back-end folder. Notice that it passes on the value "C:\" to the variable szDialogTitle: (borrowed from the MVP site http://www.hammerdata.com/Newsgrp/api/api0002.htm) Call AutoMacros.BrowseFolder(szDialogTitle = "C:\") The following declaration needs to be made to establish the BrowseFolder function or class (in the AutoMacros module): Private Type BROWSEINFO Now here is the actual dialog box code (also in the AutoMacros module): Public Function
BrowseFolder(szDialogTitle As String) As String
|