Topic 7: What is a Visual Basic module?

Go to Topic 8 * Access Database Skills Table of Contents * Technology Skills

Modules contain the Visual Basic programming code used in the database. Visual Basic provides 'event oriented' programming ... the programming code determines what happens when you click a command button, when you save a record, when you move the focus into or out of a field, or other events.

This is a short sample Visual Basic procedure that maximizes a form when it first opens.

Private Sub Form_Open(Cancel As Integer)
  On Error Resume Next
  DoCmd.Maximize
End Sub

This is a sample Visual Basic procedure that happens when the cursor leaves the City field in the sample database. The procedure fills in the fields called State and Zip based on the value of City ... using information from a table named ZipCodes.

Private Sub City_LostFocus()
  On Error Resume Next
  If Me!State > "" Then
  Else
    Me!State = DLookup("state", "Zipcodes", "city='" & cty & "'")
  End If
  If Me!Zip > "" Then
  Else
    Me!Zip = DLookup("zip", "Zipcodes", "City='" & city & "'")
    Me.Refresh
  End If
End Sub