Technology & Information Management Instructor: Michael Kremer, Ph.D. Database Program: Microsoft Access Series DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)
AGENDA 3. Executing VBA 4. Declaring Variables
Executing VBA 3.
3.1 CONTROL STRUCTURES 31 VBA statements are processed from left to right and top to bottom. No GoTo statement! Most commonly used control structures: Decision structures Loop structures Decision structures: Define groups of statements that may or may not be executed, depending of the value of an expression. Loop structures: Define groups of statements that executes repeatedly
3.2 DECISION STRUCTURES 32 If Statement An expression is evaluated, if True then statements within the If block are processed. Simple If End If Enhanced structure: If..Else End If. If expression evaluates to False, then Else block is processed. Select Case End Case is another decision structure. Basic If statement (single line):
3.2 DECISION STRUCTURES Multiple-Line If structure: Start IF 33 If condition True Statements Use indentation to improve readability. False End IF
3.2 DECISION STRUCTURES 34 Condition is usually a comparison, such as A < 10. However, it can contain an expression that evaluates to a numeric expression. 0 is considered false and -1 is true. A Yes/No field is a perfect candidate.
3.2 DECISION STRUCTURES 35 Else ElseIf
3.2 DECISION STRUCTURES 36
3.2 DECISION STRUCTURES 37 Select Case statement
3.2 DECISION STRUCTURES 38 SELECT CASE Statement Only one test expression at the top, then compare to multiple values.
3.2 DECISION STRUCTURES 39 Use To keyword for ranges, commas to separate multiple values. When using comparison operators, use the Is keyword. Select Case statement can be replaced with traditional If statement. However, Select Case is more efficient and readable. As with If statement, only one block is processed. Select Case statement works only with one test expression.
3.2 DECISION STRUCTURES 40
3.3 LOOP STRUCTURES 41 Loop structures are designed to execute code repetitively. Two Loop structures: Do Loop For Next Do Loop structure uses conditional processing to determine whether a loop is to be processed. For Next loop repeats a specific number of times. Start Loop Condition False True Statements End Loop
3.3 LOOP STRUCTURES 42 Do Loop
3.3 LOOP STRUCTURES Another variation is shown below: 43 At least one loop is executed since the condition is placed at the bottom.
3.3 LOOP STRUCTURES 44 Looping while a conditions remains true:
3.3 LOOP STRUCTURES 45 For Next Loop No testing of conditions necessary, very efficient. Nesting of loops very common.
3.3 LOOP STRUCTURES 46 This example will append the alphabet into a string and display it in a message box.
3.4 EXITING A CONTROL STRUCTURE 47 Sometimes it is desirable to exit a loop structure when you are done with a certain task. Syntax: Exit Do Exit For Exit Sub Exit Function Generally, you have to test for some condition using an If statement inside your loop.
3.4 EXITING A CONTROL STRUCTURE 48
3.5 MESSAGE BOX FUNCTION/STATEMENT 49 Message box statement: Msgbox prompt, buttons, title Prompt: Message to be displayed Buttons: 3 different parts: Buttons, Icon Style, Default button Title: Title of message box window
3.5 MESSAGE BOX FUNCTION/STATEMENT 50 Message box function returns a value which button the user pressed. Only difference in syntax is to enclose the arguments in parentheses.
3.6 ARRAYS 51 It is common to store records in one variable. An index is used to tell the different records apart. First Name Last Name Record 1 1,1 1,2 Record 2 2,1 2,2 Record 3 3,1 3,2 Record 4 4,1 4,2 Record 5 5,1 5,2 Record 6 6,1 6,2 Record 7 7,1 7,2 Record 8 8,1 8,2 Record 9 9,1 9,2 Record 10 10,1 10,2
3.6 ARRAYS 52 Arrays have upper and lower bounds to indicate the range of index numbers. Once an array is declared, use LBound and UBound functions to retrieve the upper and lower bounds.
3.6 ARRAYS 53
3.6 ARRAYS 54
3.7 NULLS IN VBA 55 What is Null? Currently unknown or missing data Not applicable data
3.7 NULLS IN VBA 56 In VBA, only one data type can hold Nulls, that is the Variant data type. Comparison expressions using Nulls. Do not use = Null to test for Nulls!
Declaring Variables.
4.1 OVERVIEW OF VARIABLES 57 Variables hold temporarily data while the application is running. Also used for storing references to other objects for quick access. Variables are declared for four reasons: To assign a variable name To designate the variable s scope (visibility) To designate the lifetime To specify a data type The Dim statement:
4.2 THE DIM STATEMENT 58 Most commonly used data types: String for text values. Date for date/time values. Single/Double/Integer/Long Integer for number values. Boolean for yes/no type of data. Variant for any kind of data. Object for object references, such as a form or a report.
4.3 IMPLICIT/EXPLICIT VARIABLE DECLARATION If no data type is specified, the Variant data type is used. Variant is the least stringent because it can hold all types of data. Once a variable is declared, a default value is assigned: String = (zero-length string) Number = 0 Date/time = 12:00:00 AM Variant = Empty Object = Nothing Variables in code can be used without being declared. Not recommended! 59
4.3 IMPLICIT/EXPLICIT VARIABLE DECLARATION 60 Option Explicit statement means that every variable must be declared. Sometimes, especially during development, you may want to turn this off..
4.3 IMPLICIT/EXPLICIT VARIABLE DECLARATION 61 Once you turn on Option Explicit, the following compile error is displayed:
4.3 IMPLICIT/EXPLICIT VARIABLE DECLARATION 62 The Option Explicit statement must be included in all modules in order to be effective for the entire application. Turn on Require Variable Declaration in Tools, Options. Does not change existing modules! Must manually include Option Explicit
4.4 SCOPE OF VARIABLES 63 Depending on how a variable is declared, it may be only accessible in the procedure it is declared in. Or only in the module the procedure belongs to. Or in the entire database application. Three different scopes: Procedure-level Private-module level Public-module level Using Dim in a procedure creates Procedure-level scope. Also declaring an argument in in the argument list of a procedure is a Procedure-level scope.
4.4 SCOPE OF VARIABLES 64 When a variable is declared in the declaration section of a module using the Dim or Private statement, the scope is Private-module level. Module can be a class or standard module. When a variable is declared in the declaration section of a module using the Public statement, its scope is Public-module level. Procedure-level variable is destroyed Private Module-level once the procedure Public Module-level is finished. Procedure-level There may be more than one variable strtemp.
4.4 SCOPE OF VARIABLES 65
4.4 SCOPE OF VARIABLES 66
4.4 SCOPE OF VARIABLES 67
4.4 SCOPE OF VARIABLES 68 Database Module 1 Public gstrtemp As String Procedure Dim strtemp As String Procedure-level Module 2 Option Compare Database Option Explicit Dim mstrtemp As String Procedure Public Module-level Private Module-level
4.5 LIFETIME OF VARIABLES 69 Values of module-level variables (Public and Private) are preserved as long as the database remains open. Procedure-level variable exists only as long as the procedure is running. There is no such statement to kill a variable in VBA, therefore use module-level variables sparingly. One lifesaver for procedure-level variables: Static Such a variable preserves its value in memory, but it is only accessible in the procedure it was declared. An entire procedure can be declared using the Static keyword: All variables become static:
4.5 LIFETIME OF VARIABLES 70