Chapter 4: VBE- the UserForm Interface

The Visual Basic Editor (VBE) platform can be accessed by:

  • Click on the toolbar click button Viewà Toolbarsà Visual Basic (make sure that it has a check mark).
  • Now click on the VBE toolbar button:

The VBE gives you a tree-structured view of all the application with its associated objects. You can expand these and collapse branches of the views to get more or less detail. The VBE window displays UserForms, modules or other separators. If you want to select a UserForm or module, simply double click on the project explorer window for a more detailed look.

A UserForm is a custom dialog, a pop-up window that collects and processes input from the user. It looks and acts just like any of the familiar built-in dialogs, such as the ones you see when you click File à Print or Format à Font, except that you design it yourself and tailor its elements to suit the needs of the job it performs through the use of programming language- VBA.

Userforms  and the VBA language are also seen in other MS Office programs, including Excel and PowerPoint. Not only do these programs use many of the same fundamental principles, some of the best references that have solved problems for me in the Word platform have come from Excel internet and book sources.

Userforms can employ any number of available graphical "controls" — command buttons, text boxes, check boxes, list boxes, option buttons ("radio buttons"), scroll bars, and multi-page structures. Learning about all of them, and how to write the VBA code that lives inside them is critical to unleashing the power of Word as an EMR. Making a simple UserForm, though, is not difficult.

The Toolbox Objects

            When you click on the Toolbar’s View à Toolbox the following form appears with available controls:

 

        Each of these controls have specific properties as demonstrated in the “Properties Window”, including size, color, font, and other elements that could be manipulated. Passing your pointer over the specific properties gives a brief description on what can be altered.

Label

The label control allows the placement of static (not editable) text on your form. These controls are useful as captions for controls.

Text Box

This is the standard control to allow the user to select and enter text. It can be single or multiple lines and has built-in support for cut, copy, and paste commands.

Combo Box

This is a drop-down list, allowing the user to select a value from a provided list, or to type a value directly in.

List Box

This control displays a list of items, with a scroll bar if necessary, allowing the user to select one or more items.

Check Box

This control displays a small square that the user can check or uncheck by clicking on it. Multiple check boxes are used when you want the user to be able to select any number of items in a group.

Option Button

Also known as a radio button and functions much like the check box. Groups of this control allow only one item to be selected at a time.

Command Button

Also known as the click button, this control is the regular Windows button; it has a caption and can be clicked on by the end user.

Toggle Button

Toggle buttons can be used to make choices. Unlike a regular command button, the toggle button is changed from a “down” state to an “up” state or back again when clicked. This functionality is similar to the check box.

Frame

This control is for appearance purposes only, drawing a 3D-styled box on your UserForm for the purpose of grouping controls.

Tab Strip

This control simulates the row of tabs commonly found in Windows option dialogs. You write code to respond to changes in which tab is selected, but it does not automatic display and hiding of portions of your UserForm.

Multi-Page

This control is similar to the previous one, tab strip, but it actually supports multiple pages of controls, and will show and hide those pages automatically as the user switches tabs.

Scroll Bar

Functioning just like a regular Windows scroll bar, this control can be used for anything where you want the user to set a value within a range.

Spin Button

This control is usually used to increment or decrement a numeric value.

Image

This allows the display of a picture file on your userform. The picture file can be selected from bitmaps, gifs, jpegs, metafiles, or icon files.

Naming Conventions For Objects

        Object names should follow a certain standard, as displayed below, with a 3 letter code followed by a capital letter and then the rest of the object name.

  •    cbo = ComboBox           

  •    chk = Checkbox                                            

  •    cmd = Command Button  (examples: cmdProceed, cmdCancel)

  •    dir = Directory ListBox

  •    dlg = Dialog

  •    drv = Drive ListBox                                         

  •    fil = File ListBox                                             

  •    hsb = Horizontal Scrollbar                            

  •    img = Image

  •    lbl = Label  (examples: lblFirstDescription, lblHeading)

  •    lst = ListBox                     

  •    mnu = Menu

  •    net = Inet (Internet) dialog bo                      

  •    opt = Option Button        

  •    pic = PictureBox

  •    sld = Slider                       

  •    tmr = Timer                       

  •    txt = TextBox  (examples: txtSocialSecurityNumber, txtUserInput_1)

   LABELS: generally are in all caps

Creating Your First UserForm

To create your first basic UserForm that houses a macro which performs an activity in Word,  follow these steps:

  1. Create a new  template by clicking on File à New à then click on “On my computer…” (Office 2003; in earlier Word versions you’ll be sent to the next step automatically). Now click on the “General” tab of the popup window à in the lower right hand corner, click on Template à click "OK”.

 

  1. Now add your text, graphics, bookmarks, and other elements. For this exercise, add a bookmark that will be used to add text  to your template. Go to the place on your template where you wish to add your bookmark, then go to your toolbar on top, and select Insert à Bookmark, then when you get the popup window, add a Bookmark name. For this example we’ll use “testbmk”. Now click on “Add” to deploy it to your template.

  2. Now open up the VBE platform. Go to your toolbars and select Tools à Macro à Visual Basic Editor.

  3. Now insert a new UserForm by right clicking on either your project name or on the “Word Objects” line, then select Insert à UserForm.

  4. After you make your new UserForm, you will end up with an open blank UserForm. In other situations, just click on the UserForm to activate it. It’ll be automatically named something like “UserForm1”.

  5. You’ll see on your platform the Toolbox popup control full of objects that can be placed on your UserForm. For this example, place a textbox and a command button on  your new UserForm by  clicking on the objects individually, then clicking on the UserForm where you wish for them to be placed. You can later left click and drag them to a new spot if needed. If you don’t see the Toolbox popup control, go to your toolbar and select View à Toolbox. Each object will be given an automated name which can be changed later on. Other controls that could be used are the label, list box, combobox, checkbox, option button, and image controls. For now, the two objects that have been placed have been named Textbox1 and Commandbutton1. 

  6. Now double-click on the CommandButton1 to view the empty coding area. Add the following code:

Sub CommandButton1_Click()

With ActiveDocument

    .Bookmarks("testbmk").Range.InsertBefore TextBox1

End With

UserForm1.Hide

End Sub

 

 

  1. Close the Visual Basic Editor     
  2. Now select ToolsàMacro à Macros, then with the Template selected in the “Macros in:” box, type the name “autonew” in the “Macro name:” box and click the Create button. Enter the following command in the module screen:

UserForm1.Show

  1. Exit from the Visual Basic Editor and save your template.

      When you create a new document from the template, the "autonew" macro opens the user form for the user to input data, then when the command button is clicked, the code associated with that button inserts each piece of information into the respective bookmark. Alternatively, you can place a new toolbar item to allow for UserForm access at anytime.

      The end-user now only has to add text to the textbox, then deploy the text onto the bookmark that you previously had placed onto your template. There's lots more that you can do with UserForms. If you get stuck, post a question to the microsoft.public.word.vba.userforms newsgroup and someone will spring to your rescue.

VBA vs. Visual Basic

            In the next few chapters we'll be discussing topics dealing with the underlying coding of  your project. It's important to understand the difference between VB and VBA. Visual Basic (VB) is the programming language of choice for programmers that work in the Windows OS environment. It is a programming language that can be compiled to create standalone exe's and ActiveX dll's. VBA (VBA) is a subset of VB that is used as a macro language for MS Office (as well as some other programs like AutoCad). VBA is not capable of creating a standalone executable, but rather is ran within the host environment of the parent program (Excel, Word, Access, etc.)    

            The VB programming language has been chosen by the many due to its ease of use. With little extra work, a VB programmer can write Windows programs without having to relearn how to program in a different language syntax, for example Visual C++. Visual Basic programs are free standing Windows programs which can be compiled and  exported to other Windows platform. VB programs run in their own memory space inside MS Windows. They can even run on RISC platforms running on Windows without changing any of the existing programs.

            VB also allows programmers to make Custom Controls to be used in other Visual Basic program, as well as Delphi and Visual C++. Using VB Enterprise, programmers can create n-tiered systems. This allows programmers to create programs that communicate with each other across a network to share information and resources as well as work in unison. This is useful in splitting the client and server processing thus speeding data access for users.

            VBA provides a complete integrated development environment (IDE) that features the same elements familiar to developers using Microsoft Visual Basic, including a Project Window, a Properties Window, and debugging tools. VBA is finding its way into more and more applications, including the popular MS Office System. Since VBA is actually a part of the application the overhead is lower which makes accessing and manipulating the host application faster. It also gives users greater ability to manipulate the host application objects.

Applying this Tutorial to Your EMR Project

          At this point, begin creating your UserForms, pasting the various objects that you will need to most rapidly fill in the patient chart. Don’t worry about the underlying code just yet. Typical UserForms include:

  • Patient general information
  • Patient SOAP notes (for patient health problems, including medications)
  • Scheduler form
  • General note creation form

As you develop your EMR project

, you will find that you will need many more UserForms and subforms.