DI Management Home > Microsoft Access Classes > Linking to the database

Linking to the database


<< Back to Classes index page

How do we link class modules with the tables in our database? Let's set up an elementary example where we read customer details from a table into a customer object.

  1. Create a new table tblCustomers with the following fields:
     
    Field NameData TypeSetting
    CustomerIDAutoNumberPrimary Index
    CustomerNameText 
    AddressText 
    TelephoneText 
    DiscountRateNumberDouble
    TermsNumberInteger

    The DiscountRate is the standard discount we might give each particular customer and Terms is the number of days credit we give them.
     

  2. Enter some dummy data to create a couple of records. Make sure you don't leave any empty fields for the moment.
     
    tblCustomers: Sample Data
    CustomerID CustomerName Address Telephone DiscountRate Terms
    1 Fred 123 Main Street (02) 1234 1234 2.50% 30
    2 Sue 234 Any Road (02) 2345 5666 5.00% 60

     
  3. Create a new class module clsCustomer and add the code (clsCustomer.txt).
  4. In the Debug Window, type the following commands (the responses are shown in italics):
    Set oCust = New clsCustomer
    ? oCust.Load(1)
    True
    ? oCust.customername
    Fred
    ? oCust.terms
     30 
    ? oCust.Load(2)
    True
    ? oCust.customername 
    Sue
    ? oCust.discountrate
     0.05 
    ? oCust.telephone
    (02) 2345 5666
    

What has this done?

These commands have in turn:

What does this mean?

In the next lesson, we'll show how to save your object's data back to the database.

Things to note in the class module code

<< Back  |  Next >>

Contact

Any comments, feedback, questions: please send us a message.

This page first published circa 2000. Last updated 11 June 2014