Professional Documents
Culture Documents
Table of Contents
VISUAL BASIC EDITOR.....................................................................................................................................................................2 MESSAGE BOXES ...............................................................................................................................................................................3 MESSAGE BOX EXAMPLE ......................................................................................................................................................................3 THE OBJECT MODEL ........................................................................................................................................................................3 REFERRING TO OBJECTS CONTAINED IN AN A PPLICATION OBJECT MODEL ...........................................................................................4 S REFERRING TO OBJECTS IN VBA CODE .................................................................................................................................................4 Referring to an Object Properties and Methods in VBA Code.........................................................................................................4 s The Object Browser .........................................................................................................................................................................4 Getting Help in the Object Browser ..................................................................................................................................................5 PROCEDURE LEVEL VARIABLES...................................................................................................................................................5 RULES FOR NAMING VARIABLES: ...........................................................................................................................................................5 THE OPTION EXPLICIT STATEMENT .......................................................................................................................................................5 OBJECT VARIABLES .........................................................................................................................................................................6 SELECTING THE APPROPRIATE DATA TYPE AND NAME FOR AN OBJECT VARIABLE..................................................................................6 USING THE SET STATEMENT .................................................................................................................................................................6 OBJECT VARIABLE EXAMPLE ................................................................................................................................................................6 STRING VARIABLES..........................................................................................................................................................................7 USING AN ASSIGNMENT STATEMENT TO ASSIGN A VALUE TO A STRING VARIABLE .................................................................................7 USING THE INPUTBOX F UNCTION ..........................................................................................................................................................7 CONCATENATING STRINGS ...................................................................................................................................................................8 USING THE VAL F UNCTION ...................................................................................................................................................................8 STRING VARIABLE EXAMPLE ................................................................................................................................................................9 DATE VARIABLES..............................................................................................................................................................................9 USING AN ASSIGNMENT STATEMENT TO ASSIGN A VALUE TO A DATE VARIABLE ...................................................................................9 USING VBA DATE, TIME, AND NOW F UNCTIONS ..............................................................................................................................10 S USING DATES AND TIMES IN C ALCULATIONS .......................................................................................................................................10 CONVERTING STRINGS TO DATES ........................................................................................................................................................10 DATE VARIABLE EXAMPLE .................................................................................................................................................................11 INTERNET SITES..............................................................................................................................................................................11 GLOSSARY.........................................................................................................................................................................................12
Page 1
Figure 1
__F_ Code window __E_ Code window Close button s __B_ Dot member selection operator __G_ Full Module View button __K_ Method icon __A_ Object list box
__C_ Procedure list box __H_ Procedure View button __J_ Project Explorer window __I_ Properties window __L_ Property icon __D_ Visual Basic Editor Close button s
Page 2
MESSAGE BOXES
The syntax for the MsgBox command allows you to include five items called arguments. These named arguments are Prompt, Buttons, Title, HelpFile, and Context. Only the Prompt argument, which specifies the message you want the dialog box to display, is required. The other four arguments are optional, which is why they are surrounded by brackets ([]).
Page 3
Page 4
Offered by the Center for Teaching and Learning @ UIS Getting Help in the Object Browser View an object Help s screen by clicking the Help button at the top of the Object Browser window. Select Application, then Name then Click Help.
Page 5
OBJECT VARIABLES
Selecting the Appropriate Data Type and Name for an Object Variable
The data type selected for an object variable will depend on the type of object to which the object variable points. In most cases, the data type has the same name as its corresponding object. You can use the Visual Basic Object Browser to view a complete listing of objects and their data types.
Page 6
Call procedure from the Report Open Event 1. In Project Window, select Report_StudentReport 2. Select Open event 3. type: SelectFieldOrder 4. comment out the doCmd line which opens the report in the SelectFieldOrder function 5. Open the report
STRING VARIABLES
String variables store names of people and things. When the Dim statement is used to reserve a String variable in memory, VBA initializes the variable to a zero-length string, or empty string. An empty string is two quotation marks with nothing between them.
Page 7
Offered by the Center for Teaching and Learning @ UIS written & taught by Sandra Ohler InputBox(Prompt:= prompt [,Title:=title] [, Default:=defaultValue]). prompt is the message displayed inside the dialog box, title is the text (name) displayed in the dialog box title bar s defaultValue is the value that appears in the input area when the dialog box first opens. Example: strName = InputBox(Prompt:= What is your name? Title:= , Name Information ) Notes: Italicized items to the right of the := operator are called arguments, and the bold items to the left of the := operator are the names of the arguments. The items in square brackets ([]) are optional arguments, meaning that they are not required parts of the syntax. If a Title argument is not supplied to the InputBox function, the name of the host application (for example, Microsoft Access) appears in the dialog box title bar. If no Default argument is included, the input area will be blank when s the dialog box first opens. In VBA, a function is a set of instructions that performs a task, like a procedure. A function, however, returns a value after the task is performed. For example, the InputBox function prompts the user to enter a value from the keyboard. It then returns this value to the calling program. The value returned by the InputBox is always a string literal constant, so typically it is assigned to a String variable.
Concatenating Strings
Linking strings together is called concatenating. Strings are concatenated in VBA by use of the ampersand (&).
Page 8
DATE VARIABLES
Date Variables store dates or time. Date variables are automatically initialized to the number 0. Dim variablename as Date Ex: Dim dtmBirth as Date
Page 9
Note: The vbNewLine constant causes the insertion point to move to the next line in the message box.
TimeValue function takes a string expression and returns the time equivalent dtmVariable = TimeValue(Time:=stringExpression
Page 10
INTERNET SITES
http://www.zdnet.com/zdhelp/filters/subfilter/0,7212,6003254,00.html http://visualbasic.about.com/compute/visualbasic/ http://www.vbatutor.com/ http://www.codd.com/vbonline/
Page 11
GLOSSARY
Alt F11 Opens the Visual Basic Editor Arguments Specific information needed by a procedure to perform its task - also called parameters Assignment statements are used to assign values to numeric, Date, Boolean and String variables. The syntax of an assignment statement is variablename = value Class is the formal definition of an object, including the properties that control the object appearance and the methods and s events that control its behavior. Code window VBA instructions are entered as procedure code to perform some task in the Code window. The Code window title bar shows the name of the current open file and the current object. Collection A group of one or more individual objects referred to as a single unit. Treating a group of objects as a collection allows you to conveniently refer to all of the objects at the same time. Comments serve as reminders to the programmer about the purpose of the procedure and are explanatory details for anyone reading the code. To create a comment in VBA, type an apostrophe ( before the text that is to be treated ) as a comment. Constant A named item that retains a constant value throughout the execution of a program. You can use constants anywhere in your code in place of actual values. Date Function Returns the date maintained by the computer's internal clock. Date Variables Contain a date or time Dim statement Used to reserve a procedure-level variable. The syntax for the Dim statement is Dim variablename As datatype dot member selection The period between the object and its property (or method) is called the dot member selection operator. It indicates that the property is a member of the object chosen. empty string two quotation marks with nothing between them, also called a zero length or null string event procedures procedures which run in response to some action you perform on an object Format Function controls the appearance of dates and times. Function is a set of instructions that performs a task, like a procedure. A function, however, returns a value after the task is Function procedures are procedures that may return a value Input Box Function One way of getting information from the user at the keyboard. The Input Box function returns a string. Keywords Words that have special meanings to a programming language like VBA and can not be used as variable names line continuation in VBA a space followed by an underscore allows you to continue a line of code on a second line to improve readability macros procedures which can be created, saved, and run directly within the package where they will be used Main window In Visual Basic Editor, it is displayed at the top of the screen and contains the title bar, the menu bar, and the Standard toolbar. The title bar will include the currently open filename when the Visual Basic Editor window is open on the screen. Page 12