Contents: Modules and Procedures and Their Scope
|
Chapter 6: VBA Coding- Objects/Variables/Data Types VBA is the language used throughout the Office platform as well as other products, including Visio, AutoCAD. This chapter will prepare you for creating (not recording) your own macros, extending your ability to create truly useful applications with MS Word. The study of this language should begin with how data is manipulated and referred to. The first thing that you need to do before you work with VBA objects that are opened by an Office application, you should set a reference to the application that supports those objects by using the References Dialog Box. To open this dialog box, go to the VBE platform, then click on Toolsà References on the toolbars section on top. The References dialog box choices used in the MWEP is seen below:
You can use another application's objects without setting a reference in the References dialog box by using the CreateObject or GetObject function and declaring object variables as the generic Object type. If you use this technique, the objects in your code will be late-bound, and as a result your procedure will not run as fast. A VBA object represents an item that can be manipulated which has certain properties or attributes that describes it. These objects can perform actions, known as methods. A class is a template from which an object is created. Many different objects can be created from a single class and these objects are said to be instances of the class. An example of objects can be found in a typical patient's chart:
This information can be used inside tables, inside consult paragraphs, and in a whole host of other medical reports. With objects, properties and methods VBA can do almost anything. VBA has a few objects, augmented by the hundreds found inside MS Word itself. In the example above, Selection.TypeText Text:=" " object is provided by Word and represents the area of a document that has been highlighted, or selected. Word can not only set properties, but retrieve them. For example, the line below can yield what the highlighted area states. MsgBox Selection.Text The above line might pop up a phrase like- "This world is round!". Almost all of your VBA code will involve working with objects; it is important to understand a few general rules:
To do anything that is very useful in VBA, we need the ability to keep values around, not just in object properties, but also in some form of storage. This is where variables come in, a way to store data, and the next section will explain what they are and how to use them.THE MS OFFICE EMR PROJECT Variables are found in most programming languages, and serve as a place to store information which can then be used throughout your program to represent the values that have been assigned. The reason to use variables are several:
Examples of global variables that I've used in the MWEP include the following:
The first thing that you need to do is to declare your variables, either in the block, procedure, or module level. Variables should always be defined with the smallest scope possible. Global (Public) variables can increase the use of memory and make the logic of an application extremely difficult to understand. Global variables also make the reuse and maintenance of your code much more difficult. In a VBA application, global variables should be used only when there is no other convenient way to share data between forms. When global variables must be used, it is good practice to declare them all in a single module, grouped by function. In the MWEP I declare global module variables in the modGlobals module. Here is an example from the MWEP where variables are declared locally in both the block and in the procedure levels: The use of variables usually takes three steps:
It is preferable that you declare the type of variable that you are
dealing with, since your program will be optimized with the exact
amount of memory allocated and will generally run faster than if you
leave it undeclared. When you declare a variable, remember that:
Here is an example of early binding of an object type variable: Dim wdApp As Word.Application
Naming Conventions For VariablesVariable Data Types names should also 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.
There are 3 main types of scopes possible with variables:
A Review of the MWEP modGlobals Variable Declarations The modGlobals module lists a full paragraph of public variable declarations. Notice how this Class Module sets these variables beginning with the word “Public”, making their scope as wide as possible. The first line sets the most important constant, which is the local backend table location where the local parameters of the front-end are kept, including certain passwords and settings. The default location of the back-end tables is stored as one of these settings that are changed depending on which button you choose when the splash screen is shown. Option Explicit Public Const conDBpath As String = "C:\ZMDSBE.mdb" Public conDBpath2 As String, yy As String, tt As String, uu As String, vv As String, ww As String Public XX As String, xxx As String, zz As String, zzz As String, yyy As String, strTitle As String Public Const conDebug As Boolean = True Public lun As String, ptn As String, attgname1 As String, acctno As Long Public street1 As String, age1 As Long, sex11 As String Public citystatezip1 As String, NoOfRecords As Long, fvst1 As Date, MyTime, MySecond Public Phones1 As String, ptaddress1 As String, racee As String, tt11 As String Public db As DAO.Database, rs As DAO.Recordset, SQLStmt As String Public xlapp As Object, schdateD As Date, act, aa As String, age2 As String Public dd As Date, dd2 As Date, ii As Long, I As Long Public ftit As String, worddoc1 As String, pdfdoc1 As String, szDialogTitle As String Public lngDBTimesOpened As Long, pty As Property, lngProfileTimesOpened As Long
![]() When you choose any one of this form’s buttons, you can either keep the current location of the back-end patient files or link to a different back-end location. The constant variable that cannot be changed is the conDBpath; the variable denoting the actual “working” back-end variable is the conDBpath2, set as a string type in line 2 of the code in the modGlobals declaration paragraph, and assigned a value by the splash screen form with code such as seen below: conDBpath2 = "Z:\ZMDSBE.mdb" At various points throughout the program, code checks to see if this assignment has been done, and if it hasn’t (i.e. it’s “nul and l”), then it sets it to the default local folder: If IsNull(conDBpath2) Or Len(conDBpath2) < 2 Then conDBpath2 = "C:\ZMDSBE.mdb" End If Other global variable setting lines of interest that are constantly used in the MWEP are:
Public lun As String, ptn As String, attgname1 As String
Public dd As Date, dd2 As Date, ii As Long, I As Long
Public db As DAO.Database, rs As DAO.Recordset, SQLStmt As String
Public street1 As String, age1 As Long, sex11 As String
Public yy As String, tt As String, uu As String, vv As String, ww As String Public XX As String, xxx As String, zz As String, zzz As String, yyy As String, strTitle As String Applying this Tutorial to Your EMR ProjectAt this point, you should:
Bibliograp hy Microsoft Office 2000/Visual Basic Programmer's Guide http://msdn.microsoft.com/en-us/library/aa164510(office.10).aspx#
Special Procedure Types
Bibliography
Microsoft Office 2000/Visual Basic Programmer's Guide http://msdn.microsoft.com/en-us/library/aa164510(office.10).aspx#
|
||||||||||||||||||||||||||||