Tutorial 11 Using and Writing Visual Basic for Applications Code

26
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code

description

Tutorial 11 Using and Writing Visual Basic for Applications Code. Objectives. Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event procedure Create a function in a standard module - PowerPoint PPT Presentation

Transcript of Tutorial 11 Using and Writing Visual Basic for Applications Code

Page 1: Tutorial 11 Using and Writing Visual Basic for Applications Code

®Microsoft Access 2010

Tutorial 11

Using and Writing Visual Basic for Applications Code

Page 2: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXP

New Perspectives on Microsoft Access 2010 2

Objectives• Learn about Function procedures (functions), Sub

procedures (subroutines), and modules• Review and modify an existing subroutine in an event

procedure• Create a function in a standard module• Test a procedure in the immediate window• Create event procedures• Compile and test functions, subroutines, and event

procedures• Hide text and change display colors

Page 3: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPVBA Code Window

New Perspectives on Microsoft Access 2010 3

Page 4: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPIntroduction to Visual Basic for Applications• Visual Basic for Applications (VBA) is the

programming language provided with Access and other Office programs

• VBA has a common syntax and a set of common features for all Microsoft Office programs– Event-driven language–Object-oriented language

New Perspectives on Microsoft Access 2010 4

Page 5: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPEvents

New Perspectives on Microsoft Access 2010 5

Page 6: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPProcedures• A Function procedure, or function, performs

operations, returns a value, accepts input values, and can be used in expressions (recall that an expression is a calculation resulting in a single value)

• A Sub procedure, or subroutine, performs operations and accepts input values, but does not return a value and cannot be used in expressions

New Perspectives on Microsoft Access 2010 6

Page 7: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPModules

New Perspectives on Microsoft Access 2010 7

Page 8: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPModules• Declarations section• Standard module• Public procedure• Class module• Local procedure• Private procedure

New Perspectives on Microsoft Access 2010 8

Page 9: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPUsing an Existing Procedure

New Perspectives on Microsoft Access 2010 9

Page 10: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPCreating a New Standard Module• Click the Create tab on the Ribbon• In the Macros & Code group on the Create tab,

click the Module button. A new Code window opens in the Visual Basic window

New Perspectives on Microsoft Access 2010 10

Page 11: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPCreating a Function• Each function begins with a Function

statement and ends with an End Function statement–Parameter–Argument

New Perspectives on Microsoft Access 2010 11

Page 12: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPTesting a Procedure in the Immediate Window• In the Code window, click View on the menu

bar, and then click Immediate Window to open the Immediate window

• Type a question mark (?), the procedure name, and the procedure’s arguments in parentheses. If the argument contains a string of characters, enclose the argument in quotation marks

• Press the Enter key and verify the displayed answer

New Perspectives on Microsoft Access 2010 12

Page 13: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPTesting a Procedure in the Immediate Window

New Perspectives on Microsoft Access 2010 13

Page 14: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPEvent Procedure

New Perspectives on Microsoft Access 2010 14

Page 15: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPCreating an Event Procedure

New Perspectives on Microsoft Access 2010 15

Page 16: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPDesigning the Event Procedure

New Perspectives on Microsoft Access 2010 16

Page 17: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPAdding an Event Procedure to a Form or Report• Open the form or report in Design view, select the

control whose event property you want to set, open the property sheet for the control, and then click the Event tab in the property sheet

• Click the event property box, click its Build button, click Code Builder in the Choose Builder dialog box, and then click the OK button

• Enter the subroutine statements in the Code window• Compile the procedure, fix any statement errors, and

then save the event procedure

New Perspectives on Microsoft Access 2010 17

Page 18: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPAdding an Event Procedure to a Form or Report

New Perspectives on Microsoft Access 2010 18

Page 19: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPCompiling Modules• The process of translating modules from VBA

to a form your computer understands is called compilation; you say that you compile the module when you translate it

• When you run a procedure for the first time, Access compiles it for you automatically and opens a dialog box only when it finds syntax errors in the procedure

New Perspectives on Microsoft Access 2010 19

Page 20: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPAdding a Second Procedure to a Class Module

New Perspectives on Microsoft Access 2010 20

Page 21: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPAdding a Second Procedure to a Class Module

New Perspectives on Microsoft Access 2010 21

Page 22: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPChanging the Case of a Field Value• StrConv function• wvProperCase constant• vbUpperCase constant• vbLowerCase constant

New Perspectives on Microsoft Access 2010 22

Page 23: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPHiding Controls and Changing a Control’s Color

New Perspectives on Microsoft Access 2010 23

Page 24: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPHiding Controls and Changing a Control’s Color

New Perspectives on Microsoft Access 2010 24

Page 25: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPHelpful Hints• None

New Perspectives on Microsoft Access 2010 25

Page 26: Tutorial 11 Using and Writing Visual Basic for Applications Code

XPXPXPAdditional Examples• None

New Perspectives on Microsoft Access 2010 26