22
Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Embed Size (px)

Citation preview

Page 1: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Access Lesson 13Programming in Access

Microsoft Office 2010 Advanced

Cable / Morrison1

Page 2: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced2

Objectives

Create a new function. Test a new function. Add a control with defined names to a form. Create an If procedure. View the procedure results. Add an Else statement to a procedure. Test a revised procedure.

22

Page 3: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced3

Vocabulary

code comment function procedure public syntax

33

Page 4: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced4

Introduction

VBA is the programming language for Microsoft Office programs, including Access.

VBA has a set of common features for all Microsoft Office programs.

Page 5: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Creating a New Function

A function is an action that can be defined in VBA code where you use statements to describe the action you want the function to perform.

Code refers to the syntax, or wording, that is used in VBA.

Access already has built-in functions, such as Average, Min, and Max.

5

Page 6: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Creating a New Function (continued)

If you want to use a function that is not built-in, you will need to define a new function.

A variable name is a symbolic name that you assign to a value.

You will need to assign a data type to the variables.

6

Page 7: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Creating a New Function (continued)

A comment is a statement that does not perform an action, but helps in identifying the purpose of the code.

Defining a function in a module makes it accessible throughout the database, or public.

7

Page 8: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Creating a New Function (continued)

8

Completed YearDiff function

Page 9: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Testing a New Function

After creating a new function, it is smart to check it to make sure the code is correct.

Even the simplest functions can contain errors, such as spelling errors or logic errors.

To test a function, you use the Immediate window.

To test the function, type a question mark (?) before the function name and the variables.

9

Page 10: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Testing a New Function (continued)

10

Answer appears in Immediate window

Page 11: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Adding a Control with Defined Names to a Form

You can add controls to a form that display the results of a calculation.

To use a control in a procedure:– Add the control to the form– Assign specific names to the parts of the control

in the Property Sheet

11

Page 12: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Creating an If Procedure

A procedure is a group of statements written in VBA code that can include several functions.

Procedures include code that indicates where the procedure begins and additional code that tells where the procedure ends.

The If procedure begins with the code word If and ends with the code words End If.

12

Page 13: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Creating an If Procedure (continued)

VBA requires the use of specific syntax so that the procedure performs actions correctly.

If a procedure will be used in only one form, it will be entered as a private procedure.

A public procedure can be used in several objects.

13

Page 14: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Creating an If Procedure (continued)

14

Visual Basic window with procedure

Page 15: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Viewing the Procedure Results

After creating a procedure, you will need to view the results to be certain that the procedure is doing what you expect.

If the procedure returns incorrect results, the syntax may not be entered correctly or may include a misspelled word.

15

Page 16: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Adding an Else Statement to the Procedure

In an If procedure, the procedure may look at a field and check to see if it contains a value.– If it does contain a value, the procedure continues– If it doesn’t, the procedure ends

You may want the procedure to perform a specific action if the field does contain a value and another action if it doesn’t contain a value.

16

Page 17: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Adding an Else Statement to the Procedure (continued)

By placing an Else statement in the code, the procedure will perform one action or else it will perform another action.

17

Page 18: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Adding an Else Statement to the Procedure (continued)

18

Revised procedure with Else statement

Page 19: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Testing a Revised Procedure

If a procedure has changed, you need to check it again.

Review the results for several records to be sure that the procedure is working correctly.

19

Page 20: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Summary

In this lesson, you learned: If the function you want to use is not a built-in

function, you will need to create a new function.

After creating a new function, you will need to test the function to see if it produces the correct results.

20

Page 21: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Summary (continued)

You can add a control with defined names to a form.

An If procedure starts with the If syntax and ends with the End If syntax.

You should review the results of an If procedure to be certain that it is working correctly.

21

Page 22: Access Lesson 13 Programming in Access Microsoft Office 2010 Advanced Cable / Morrison 1

Ac

ces

s L

es

son

13

Cable / Morrison Microsoft Office 2010 Advanced

Summary (continued)

You can add an Else statement to an If procedure.

A revised procedure will need to be tested.

22