Ch07 Access

1,345 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,345
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
114
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Ch07 Access

  1. 1. Enhancing User Interaction Through Programming<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />1<br />Chapter 7<br />“I like thinking big. If you’re going to be thinking anything, youmight as well think big.”—Donald Trump<br />
  2. 2. Chapter Introduction<br />Visual basic for applications (VBA)<br />Fully customize and automate a database<br />Perform more complex validity<br />Use functions and actions not available with macros<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />2<br />
  3. 3. Tools Covered in This Chapter<br />Assignment statement <br />Breakpoints<br />Code window <br />DateDiff function <br />DateSerial function<br />Debug<br />DoCmd statement<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />3<br />
  4. 4. Tools Covered in This Chapter (continued)<br />If statement<br />Immediate window<br />Northwind sample database<br />Variables<br />Visual Basic editor<br />Watches window<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />4<br />
  5. 5. Level 1 Objectives:Writing Visual Basic for Applications Code<br />Design create and test a subroutine in a standard module<br />Design create, and test an event procedure<br />Analyze decisions using procedures<br />Design create, and test a function in a standard module<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />5<br />
  6. 6. Understanding Visual Basic for Applications<br />Programming language for Microsoft Office programs <br />Including Access<br />Common syntax and set of common features for all Microsoft Office programs<br />Features unique for each Microsoft Office program<br />Use programming language<br />Write set of instructions to direct computer to perform specific operations in specific order<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />6<br />
  7. 7. Understanding Visual Basic for Applications (continued)<br />Coding<br />Statement<br />Event-driven language<br />Object-oriented language<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />7<br />
  8. 8. Event-Driven Programming<br />Event <br />State condition or occurrence that Access recognizes<br />Has associated event property<br />Appear in property sheet for forms reports, and controls<br />Create group of statements using VBA code <br />Set event property value to name of that group of statements<br />Event procedure<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />8<br />
  9. 9. Frequently Used Access Events<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />9<br />
  10. 10. Common Events for Selected Controls<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />10<br />
  11. 11. Coding VBA<br />Types of procedures <br />Function procedures<br />Performs operations <br />Returns value <br />Accepts input values <br />Can be used in expressions<br />Sub procedures<br />Performs operations and accepts input values <br />Does not return value <br />Cannot be used in expressions<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />11<br />
  12. 12. Coding VBA (continued)<br />Module<br />Group of related procedures<br />Starts with declarations section<br />One or more procedures<br />Basic types of modules <br />Standard modules<br />Database object stored in memory<br />Use from anywhere in database<br />Class modules<br />Usually associated with particular form or report<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />12<br />
  13. 13. Coding VBA (continued)<br />Public procedure<br />More than one object can use<br />Local procedure or a private procedure <br />Only the form or report for which class module created can use event procedure<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />13<br />
  14. 14. Creating a Subroutine in a Standard Module<br />Replace macros with VBA procedures<br />Some tasks cannot be performed with VBA<br />AutoKeys<br />AutoExec<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />14<br />
  15. 15. Creating a New Standard Module<br />Click Create tab<br />Click Macro button arrow, and then click Module<br />Begin new procedure in module<br />Click Insert menu<br />Click procedure<br />Type name<br />Select options<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />15<br />
  16. 16. Creating a New Standard Module (continued)<br />Visual Basic Editor<br />Create and modify VBA code<br />Visual Basic window <br />Program window that opens within VBE<br />Code window <br />Window to create modify and display specific VBA procedures<br />Can have as many code windows open as modules in database<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />16<br />
  17. 17. Creating a New Standard Module (continued)<br />Sub statement <br />Ends with end sub statement<br />Includes <br />Scope of procedure<br />Name of procedure <br />Opening and closing parenthesis<br />Option compare statement <br />Designates technique access uses to compare and sort text data<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />17<br />
  18. 18. Creating a Subroutine<br />Type statements in procedure between Sub and End Sub statements<br />DoCmd statement<br />Executes action in procedure<br />Access object<br />Method <br />Action that operates on specific objects or controls<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />18<br />
  19. 19. Creating a Subroutine (continued)<br />Comment <br />Include anywhere in VBA procedure <br />Describe what procedure or statement does<br />Make it easier for programmers to identify purpose of statements<br />Begin comment with <br />Word Rem (for “remark”) <br />Or single quotation mark (')<br />Appear green in code window<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />19<br />
  20. 20. Creating a Subroutine (continued)<br />Line label<br />Statement that serves as starting point for block of statements in procedure<br />Begins at start of line and ends with colon<br />Underscore character at end of line<br />Statement continues to next line<br />Run procedure<br />Click Run Sub/UserForm button<br />Saves module<br />Click Save button<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />20<br />
  21. 21. Creating an Event Procedure<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />21<br />
  22. 22. Designing an Event Procedure<br />Associate procedure with form’s On Current event property<br />Triggered when form’s Current event occurs<br />Occurs when focus moves to record <br />Add event procedure<br />Open property sheet for form<br />Click On Current text box<br />Click list arrow<br />Click [event procedure]<br />Click Build button to right of On Current text box<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />22<br />
  23. 23. Making Decisions Using the If Statement<br />Decision-making statement <br />Executes group of statements based on outcome of condition<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />23<br />
  24. 24. Making Decisions Using the If Statement (continued)<br />If statement<br />Tests condition follows one of two paths depending on outcome of condition<br />General form<br />If condition Then<br />True-statement group<br />[Else<br />False-statement group]<br />End If<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />24<br />
  25. 25. Making Decisions Using the If Statement (continued)<br />Assignment statement <br />Assigns value of expression to control or property<br />Example<br />EstSalary.ForeColor = vbGreen<br />ForeColor property<br />Color constants<br />Predefined VBA names <br />Have values that represent system color value<br />IsNull functions returns<br />True value when field or control null<br />False when not<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />25<br />
  26. 26. Comparison Operators and How to Use Them in Conditions<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />26<br />
  27. 27. Testing an Event Procedure<br />Switch to form view<br />Navigate through records <br />Make sure results correct in different situations<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />27<br />
  28. 28. Creating a Function<br />Similar calculation requests<br />Create function to perform calculation <br />Function <br />Begins with Function statement <br />Ends with End Function statement<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />28<br />
  29. 29. Variables<br />Named location in computer memory <br />Can contain value<br />Use to store and retrieve data<br />Reference using variable name assigned to location<br />Holds only one value at a time<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />29<br />
  30. 30. Variables (continued)<br />Naming rules<br />Must begin with letter<br />Cannot exceed 255 characters<br />Include letters numbers and underscore character <br />Cannot use space, punctuation, or special characters<br />Cannot be same as keywords or reserved words<br />Each variable has data type<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />30<br />
  31. 31. Common VBA Data Types<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />31<br />
  32. 32. Using Functions<br />Similar to way built-in functions used<br />Call a function<br />Function name followed by argument(s)<br />Commas separate multiple arguments<br />DateDiff function<br />Calculates number of time intervals between two dates<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />32<br />
  33. 33. Testing a Function in the Immediate Window<br />When statement entered<br />Editor checks statement to make sure syntax correct<br />Logic error <br />Occurs when procedure produces incorrect results<br />Immediate window<br />Test VBA procedures without changing data in database<br />Enter different values to test procedure<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />33<br />
  34. 34. Testing a Function in the Immediate Window (continued)<br />Immediate window<br />Type keyword “print” or question mark (?) <br />Followed by procedure name and argument values to test in parentheses and separated by commas<br />DateSerial function <br />Returns date value for specified year, month, and day<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />34<br />
  35. 35. Level 1 Summary<br />VBA<br />Programming language for Microsoft Office programs<br />Create procedures in module<br />Event-driven programming<br />Program statement execution triggered by events<br />If statement<br />Used to make decisions<br />Function<br />Returns a value<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />35<br />
  36. 36. Level 2 Objectives:Using Custom Functions and Verifying Data<br />Design create and test a custom function in an event procedure<br />Use ElseIf and If statements<br />Verify data using event procedures<br />Use the Case control structure<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />36<br />
  37. 37. Enhancing an Event Procedure to Use a Custom Function<br />Add and change statements in a procedure<br />Open object that contains event procedure in Design view<br />Click View Code button on Design Ribbon<br />Add new procedure module or class module<br />Click Insert on menu bar <br />Click Procedure, Module, or Class Module<br />Edit existing statements<br />Scroll code window to desired statement <br />Make necessary changes<br />Press Save button on Visual Basic toolbar<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />37<br />
  38. 38. Using an ElseIf Statement in an If Statement<br />Date function<br />Returns current computer system date<br />ElseIf statement <br />Equivalent to Else clause followed by If statement<br />Choice of which If statement version used <br />Matter of personal preference<br />Arranging order of condition testing<br />Order of condition testing critical<br />Place conditions in order from least inclusive to most inclusive<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />38<br />
  39. 39. Comparing an ElseIf Clause to an Else…if Clause<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />39<br />
  40. 40. Using an ElseIf Statement in an if Statement (continued)<br />Dim statement<br />Declare variables and associated data types in procedure<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />40<br />
  41. 41. Verifying Data with VBA<br />Before Update event <br />Occurs before changed data in control or record updated in database<br />Use to verify entered data<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />41<br />
  42. 42. Designing the Field Validation Procedure to Verify Zip Codes<br />Cancel As Integer <br />Defines Cancel as parameter with Integer data type<br />For some events access executes attached event procedure before performing default behavior<br />Can cancel default behavior in event procedure or macro<br />Using Cancel parameter<br />Left function <br />Returns string containing specified number of characters from left side of specified string<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />42<br />
  43. 43. Designing the Field Validation Procedure to Verify Zip Codes (continued)<br />Val function <br />Returns numbers contained in specified string as numeric value<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />43<br />
  44. 44. Using the Case Control Structure<br />Control structure <br />Set of VBA statements work together as a unit<br />Case control structure<br />Evaluates expression<br />Performs one of several alternative sets of statements <br />Based on resulting value (or condition) of evaluated expression<br />Use Case Else statement as last Case statement<br />Include false-statement group<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />44<br />
  45. 45. Using the Case Control Structure (continued)<br />CancelEvent action <br />Cancels event that caused procedure or macro containing action to execute<br />Me keyword <br />Refers to current object<br />Undo <br />Method that clears all changes made to current record<br />SetFocus <br />Moves focus to specified object or control<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />45<br />
  46. 46. Testing the Field Validation Procedure to Verify Zip Codes<br />Switch to form view to test validation procedure<br />Using event procedure for control or form<br />Before Update event for each control on form<br />Need two or more fields as part of validation process<br />Use form’s Before Update event<br />Disadvantage<br />All changes made to record undone<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />46<br />
  47. 47. Designing the Field Validation Procedure to Verify Phone Area Codes<br />Message can suggest potential error<br />Must accept any phone area code entry<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />47<br />
  48. 48. Level 2 Summary<br />ElseIf statement <br />Verify data using VBA<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />48<br />
  49. 49. Level 3 Objectives: Testing and Exploring VBA<br />Troubleshoot problems in VBA procedures<br />Compile modules<br />Develop sources for learning VBA<br />Explore completed modules in the Northwind sample database<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />49<br />
  50. 50. Troubleshooting VBA Procedures<br />Syntax error <br />Occurs when VBA statement violates language rules for statement such as <br />Misspelling <br />Incorrect sequence of keywords<br />Missing parenthesis<br />Detected immediately when statement completed<br />Error message opens and explains error<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />50<br />
  51. 51. Syntax Error Message<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />51<br />
  52. 52. Troubleshooting VBA Procedures (continued)<br />Compilation error <br />Occurs when procedures translated into form computer can understand<br />Compilation<br />Process of translating modules from VBA to form computer understands<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />52<br />
  53. 53. Troubleshooting VBA Procedures (continued)<br />Prevent compilation errors at run time<br />Use Compile command on Debug menu <br />Compile as last step before saving module<br />Execution error or run-time error <br />Occurs when procedure executes and stops because it tries to perform operation that is impossible to perform<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />53<br />
  54. 54. Using the Debugger to Correct Errors<br />Breakpoint <br />Run subroutine or function up until line on which breakpoint set <br />Procedure halts execution at point and displays module screen<br />Good way to isolate place at which procedure stops producing anticipated result<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />54<br />
  55. 55. Using the Debugger to Correct Errors (continued)<br />Use mouse pointer to get more information about procedure<br />Pointing to variable name displays ScreenTip with value contained in variable at point that procedure halted<br />Execute procedure one statement at a time<br />Try and identify cause of error <br />Click Debug on menu bar<br />Click Step Into<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />55<br />
  56. 56. Using the Debugger to Correct Errors (continued)<br />Step Into<br />Procedure executes next statement and stops<br />Step Out<br />Executes current subroutine or function and then halts<br />Reset button<br />Stops debugger so you can fix problem<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />56<br />
  57. 57. Identifying Errors Using the Watches Window<br />Watches window <br />Shows current value of specified variable<br />Add Watch dialog box<br />Enter variable name to watch<br />“<Out of context>” in value column <br />Appears for watch expression <br />When access not executing procedure that contains watch expression<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />57<br />
  58. 58. Add Watch Dialog Box<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />58<br />
  59. 59. Building Procedures Slowly to Isolate Problems<br />Prevent problems in procedures <br />Build them slowly using small groups of statements<br />Prove them to be correct as you go<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />59<br />
  60. 60. Using Help to Learn More About Programming and VBA<br />Access Help contains valuable information about VBA<br />Reading individual topics gives excellent background<br />Search results list<br />Sample code for situations<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />60<br />
  61. 61. Visual Basic Help Table Of Contents<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />61<br />
  62. 62. Level 3 Summary<br />Errors<br />Syntax <br />Compilation<br />Execution or run-time<br />Debugger<br />Use to find and correct errors<br />Help<br />Learn more about VBA<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />62<br />
  63. 63. Chapter Summary<br />VBA<br />Programming language for Microsoft Office applications<br />Decision structures<br />If<br />ElseIf<br />Case<br />Develop subroutines<br />Use debugger to find errors<br />Succeeding in Business with Microsoft Office Access 2007: A Problem-Solving Approach <br />63<br />

×