Ch07 Access

Click here to load reader

  • date post

    14-Jan-2015
  • Category

    Documents

  • view

    1.086
  • download

    0

Embed Size (px)

description

 

Transcript of Ch07 Access

  • 1. Enhancing User Interaction Through Programming
    Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
    1
    Chapter 7
    I like thinking big. If youre going to be thinking anything, youmight as well think big.Donald Trump

2. Chapter Introduction
Visual basic for applications (VBA)
Fully customize and automate a database
Perform more complex validity
Use functions and actions not available with macros
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
2
3. Tools Covered in This Chapter
Assignment statement
Breakpoints
Code window
DateDiff function
DateSerial function
Debug
DoCmd statement
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
3
4. Tools Covered in This Chapter (continued)
If statement
Immediate window
Northwind sample database
Variables
Visual Basic editor
Watches window
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
4
5. Level 1 Objectives:Writing Visual Basic for Applications Code
Design create and test a subroutine in a standard module
Design create, and test an event procedure
Analyze decisions using procedures
Design create, and test a function in a standard module
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
5
6. Understanding Visual Basic for Applications
Programming language for Microsoft Office programs
Including Access
Common syntax and set of common features for all Microsoft Office programs
Features unique for each Microsoft Office program
Use programming language
Write set of instructions to direct computer to perform specific operations in specific order
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
6
7. Understanding Visual Basic for Applications (continued)
Coding
Statement
Event-driven language
Object-oriented language
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
7
8. Event-Driven Programming
Event
State condition or occurrence that Access recognizes
Has associated event property
Appear in property sheet for forms reports, and controls
Create group of statements using VBA code
Set event property value to name of that group of statements
Event procedure
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
8
9. Frequently Used Access Events
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
9
10. Common Events for Selected Controls
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
10
11. Coding VBA
Types of procedures
Function procedures
Performs operations
Returns value
Accepts input values
Can be used in expressions
Sub procedures
Performs operations and accepts input values
Does not return value
Cannot be used in expressions
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
11
12. Coding VBA (continued)
Module
Group of related procedures
Starts with declarations section
One or more procedures
Basic types of modules
Standard modules
Database object stored in memory
Use from anywhere in database
Class modules
Usually associated with particular form or report
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
12
13. Coding VBA (continued)
Public procedure
More than one object can use
Local procedure or a private procedure
Only the form or report for which class module created can use event procedure
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
13
14. Creating a Subroutine in a Standard Module
Replace macros with VBA procedures
Some tasks cannot be performed with VBA
AutoKeys
AutoExec
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
14
15. Creating a New Standard Module
Click Create tab
Click Macro button arrow, and then click Module
Begin new procedure in module
Click Insert menu
Click procedure
Type name
Select options
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
15
16. Creating a New Standard Module (continued)
Visual Basic Editor
Create and modify VBA code
Visual Basic window
Program window that opens within VBE
Code window
Window to create modify and display specific VBA procedures
Can have as many code windows open as modules in database
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
16
17. Creating a New Standard Module (continued)
Sub statement
Ends with end sub statement
Includes
Scope of procedure
Name of procedure
Opening and closing parenthesis
Option compare statement
Designates technique access uses to compare and sort text data
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
17
18. Creating a Subroutine
Type statements in procedure between Sub and End Sub statements
DoCmd statement
Executes action in procedure
Access object
Method
Action that operates on specific objects or controls
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
18
19. Creating a Subroutine (continued)
Comment
Include anywhere in VBA procedure
Describe what procedure or statement does
Make it easier for programmers to identify purpose of statements
Begin comment with
Word Rem (for remark)
Or single quotation mark (')
Appear green in code window
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
19
20. Creating a Subroutine (continued)
Line label
Statement that serves as starting point for block of statements in procedure
Begins at start of line and ends with colon
Underscore character at end of line
Statement continues to next line
Run procedure
Click Run Sub/UserForm button
Saves module
Click Save button
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
20
21. Creating an Event Procedure
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
21
22. Designing an Event Procedure
Associate procedure with forms On Current event property
Triggered when forms Current event occurs
Occurs when focus moves to record
Add event procedure
Open property sheet for form
Click On Current text box
Click list arrow
Click [event procedure]
Click Build button to right of On Current text box
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
22
23. Making Decisions Using the If Statement
Decision-making statement
Executes group of statements based on outcome of condition
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
23
24. Making Decisions Using the If Statement (continued)
If statement
Tests condition follows one of two paths depending on outcome of condition
General form
If condition Then
True-statement group
[Else
False-statement group]
End If
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
24
25. Making Decisions Using the If Statement (continued)
Assignment statement
Assigns value of expression to control or property
Example
EstSalary.ForeColor = vbGreen
ForeColor property
Color constants
Predefined VBA names
Have values that represent system color value
IsNull functions returns
True value when field or control null
False when not
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
25
26. Comparison Operators and How to Use Them in Conditions
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
26
27. Testing an Event Procedure
Switch to form view
Navigate through records
Make sure results correct in different situations
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
27
28. Creating a Function
Similar calculation requests
Create function to perform calculation
Function
Begins with Function statement
Ends with End Function statement
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
28
29. Variables
Named location in computer memory
Can contain value
Use to store and retrieve data
Reference using variable name assigned to location
Holds only one value at a time
Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach
29
30. Variables (continued)
Naming rules
Must begin with letter
Cannot exceed 255 characters
Include letters numbers and underscore character
Cannot use space, punctuation, or special characters
Cannot be same as keywords or reserved words
Each variable has data type
Succeeding in Business with Microsoft Office Access 2007: A