A Tutorial for Access 2007 VBA
Microsoft Access 2007 is a desktop database application. Microsoft includes Visual Basic for Applications (VBA) as a way for you to extend the functionality of Access 2007 using Visual Basic. For example, Access does not provide a built-in function to calculate age, but with VBA you could create a function to do this. In the Visual Basic Editor, you can create modules and classes. A module is a container for VBA code containing declarations and procedures. There are two types of modules. A standard module is not associated with a particular object while a class module is.
Instructions
-
-
1
Click "Start," "All Programs," "Microsoft Office" and "Microsoft Access 2007."
-
2
Open an existing database using "File," "Open."
-
-
3
Click "Visual Basic" on the "Database Tools" tab of the ribbon.
-
4
Click "Module" from the "Insert" menu. Select "Procedure" from the "Insert" menu. Enter a name for the procedure. Select a type, such as "Function," and a scope and then click "OK."
-
5
Enter the code for your procedure. For example:
Public Function Age(varBirthDate As Variant) As Integer
Dim varAge As Variant
If IsNull(varBirthDate) Then Age = 0: Exit Function
varAge = DateDiff("yyyy", varBirthDate, Now)
If Date < DateSerial(Year(Now), Month(varBirthDate), Day(varBirthDate)) Then
varAge = varAge - 1
End If
Age = CInt(varAge)
End Function
-
6
Click "Immediate Window" from "View." Type "? Age("1/2/1956")" and press "Enter" to test your code. You should get "54" if the current date is in 2010.
-
7
Click the floppy disk icon on the toolbar to save your work.
-
1
References
Resources
- Photo Credit calculating image by timur1970 from Fotolia.com