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

Saving to the database


<< Back to Classes index page

Introduction

This lesson explains how to use class modules to capture data from an unbound form and save it to a database table.

The example shows how to handle a simple Order object by creating a new class module clsOrder, a table tblOrders in the database, and a form frmOrderCreate that will use the object to create a new record in the database.

Download the zipped file classes.mdb for the full example database.

Instructions

  1. Create a new table tblOrders with the following fields:
     
    Field NameData TypeProperties
    OrderIDAutoNumberPrimary Index
    CustomerIDNumberLong integer
    ItemText 
    QtyNumberLong integer
    UnitPriceCurrency 
  2. Create a one-to-many relationship between tblCustomers.CustomerID and tblOrders.CustomerID with enforced referential integrity.

    One-to-many relationship

  3. Create a new class module clsOrder and enter the code (clsOrder.txt).
  4. Create a new unbound form frmOrderCreate with the following controls:
     
    Control NameTypeProperties
    cboCustomerCombo BoxBound to tblCustomers.CustomerID
    txtItemText Box 
    txtQtyText Box 
    txtUnitPriceText Box 
    txtValueText BoxDisabled; =[txtQty]*[txtUnitPrice]
    cmdCloseCommand button(see below)
    cmdOKCommand button(see below)
  5. Add the code (frmOrderCreate.txt) to the form.
  6. Open the form and fill out with some sample data:

    Form: frmOrderCreate

  7. Click OK. This should create a new record in the tblOrders table of the database.

What we've done

<< Back  |  Next >>

Contact

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

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