Access VBA programming TUTOR: Andy Sekiewicz MOODLE: http://moodle.city.ac.uk/ WEB: www.staff.city.ac.uk/~csathfc/acvba
The DoCmd object The DoCmd object is used to code a lot of the bread and butter operations of Access e.g., DoCmd.OpenForm "frmcustomers" OBJECT METHOD ARGUMENT The OpenForm part of the statement is a method of the object (one of many). A dot separates the object and the method. The first argument required by the method is the name of the form to open, supplied in the form of a string, which means the name is enclosed by double quotes.
Filtername argument The Filtername argument allows you to limit the records opened in the form to the records defined by a saved query e.g., DoCmd.OpenForm "frmcustomers",,"qryregionalcustomers"
WhereCondition argument The syntax for the WhereCondition argument is: a string expression that's a valid SQL WHERE clause without the word WHERE therefore, the string expression we need is "City <> 'London' "
Sub Rectangle() Variables a basic program Dim height as Single, width as Single, area as Single height = 5 width = 4 area = width * height MsgBox "The area is " & area End Sub The concatenation operator is & (Shift+7).
Getting input from the user This code asks the user for their height and stores the input in a variable Sub AskHeight() Dim height as Single height = InputBox("Please enter your height in metres") End Sub
Msgbox A MsgBox is used in a program to communicate to the user e.g., MsgBox "100 row(s) copied",, "City University" A MsgBox can also be used by the user of the program to choose a course of action e.g., var = MsgBox("Do you want to continue?", vbyesnocancel) NOTE THE COMPULSORY USE OF BRACKETS IN THIS USAGE An If Then ElseIf statement would be needed to usefully employ the MsgBox
If Then statement If <condition> Then End If line(s) of code to be executed if condition is True You can specify an alternative course of action using Else If <condition> Then line(s) of code to be executed if condition is True Else line(s) of code to be executed if condition is False End If
If Then ElseIf You can specify multiple alternative courses of action using ElseIf If <condition1> Then statement(s) to be executed if condition1 is True ElseIf <condition2> Then statement(s) to be executed if condition1 is False and condition2 is true ElseIf <condition3> Then Else End If statement(s) to be executed if condition1 and condition2 are false and condition3 is True, and so on. optionally, statement(s) to be executed if none of the above conditions are True
Functions The purpose of a function is to return a value. The first line of a function, the function declaration, defines how the function operates. The example shows a function that will return a real number, given a integer. Function Discount (q as Integer) As Single FUNCTION ARGUMENT DATA TYPE DATA TYPE OF NAME OF ARGUMENT RETURN VALUE Specifically, if you supply an order quantity, the function will return a percentage to be used as a discount value. A function can be used directly in a query, on a form or report, or can be called by a VBA subprocedure
Example Function Function Discount (q as Integer) as Single 'calculates a discount based on the quantity ordered If q >= 20 Then Discount = 0.2 ElseIf q >= 10 Then Discount = 0.1 Else Discount = 0 End If End Function
Objects VBA provides Objects for you to manipulate. Any Object has three things associated with it. An Object has: PROPERTIES Eg. Forms!frmCustomers.CustomerID.Forecolor = vbred METHODS Eg. DoCmd.OpenReport Employee Rates EVENTS Eg. The Click event of a button. A program can be made to run only when a button is clicked.
Collections Some objects are collection objects e.g., Forms Some objects are singular e.g., Form Application Forms Collection Form A collection object can be said to consist of the singular object that bears the same name e.g., The collection of Forms consists of individual Form objects A singular object can be said to contain other objects e.g., The Form object contains a collection of Controls Controls Collection Control
Forms Collection example Application Forms Collection frmcustomers Form frmsalesorders Form Form Controls Collection Controls Collection CustomerID Control CustomerName Control Control OrderNo Control CustomerID Control Control The illustration depicts a situation where two forms are open, frmcustomers and frmsalesorders
Referring to a Control on a Form The unnecessarily long way Application.Forms!frmCustomers.Controls!CustomerID can be shortened to Forms!frmCustomers!CustomerID Forms!frmCustomers.CustomerID However, if you are writing code on the module for the form that contains the control then you can refer to the control directly, e.g., CustomerID better still, you can use the Me property of the form e.g., Me!CustomerID The two examples below are equivalent assuming the code is on the module belonging to frmcustomers Forms!frmCustomers.CustomerID.Forecolor = vbred Me!CustomerID.Forecolor = vbred or
.....