Integrated Business Applications with Databases (D3) Jenny Pedler
Session 1: VBA Coding Basics Components of the VBA development environment Variable types and naming Sub and Function procedures Private and Public procedures Debugging your code Using the VBA Help facility
Add more complex logic to event handlers More actions available than for macros Integrate with other Office applications through automation Executes faster than equivalent macro action Can trap errors and provide custom error- handling Easier to read and print out than macros Advantages of using VBA
VBA is… A programming language A subset of VB (Visual Basic) Object-oriented? –Encapsulation –Encapsulation –Inheritance –Inheritance –Object-enabled Event-driven
VBA IDE Project Explorer Property window Code window Procedure list Object list Immediate window Locals window Watch window View selection buttons
Modules Form/Report Modules –Belong to specific form or report –Procedures are generally private Standard Modules –Modules pane of main database window –General-purpose code –Can be used by any form/report in database
Procedures Sub –Performs an action but does not return a value Function –Returns a value –May also perform an action
Variables Use Dim statement to declare variables Use As to define data type Use naming convention E.g. –Dim strName as String –Dim datToday as Date
VBA Data Types Boolean Logical variables - values are True or False Integer These are short (16 bit) integers. Long These are long (32 bit) integers. Currency Used for quantities which consist of a whole number combined with a fraction with a fixed number of decimal places. As the name implies, they are useful for dealing with monetary values. Single These are floating point values, providing around 6 significant figures of accuracy. Double These are floating point values, providing around 14 significant figures of accuracy. Date A date variable is composed of two floating pointing values, one containing a date and the other containing the time of day. String Variable length character strings. Variant A variant variable can contain any of the above data types. Variant data variables change their type dynamically according to what is stored in them. Note that variables are assumed to be Variant unless specifically declared to be something else.
Event handlers Event handler –VBA code (or macro) to be executed when a specific event happens to a specific object Event property –Mechanism to allow us to attach event handler to specific event for specific object –Event tab on objects property sheet lists events that an object can respond to
DoCmd object Runs Access actions from VBA –Macro – actions –VBA – methods DoCmd methods are VBA equivalent of macro actions
If [condition] Then statements to be executed End If If [condition] Then statements to be executed Else default statements to be executed End If If [condition] Then statements to be executed Else If [condition] Then alternative statements to be executed Else default statements to be executed End If Conditional Execution: If
Select Case statement Syntax Select Case testexpression [Case expressionlist-n [statements-n]]... [Case Else [elsestatements]] End Select