Rig for Sale

Use one form for several data management tasks

(Access 97/2000/2002)

Using default settings, an Access form allows you to edit existing data, add new records, and delete records. You can modify how data is handled by changing the AllowEdits, AllowAdditions, AllowDeletions, and DataEntry properties. The first three properties are self-explanatory. When the DataEntry property is set to Yes, only new records may be added (note that AllowAdditions must also be set to Yes for DataEntry to be relevant). While these four properties let you customize a form to your needs, setting them as you design a form may not provide the best approach for determining how the form should behave.

For example, you may prefer that a form sometimes allows new entries, but that at other times it may only be used to edit existing data. Fortunately, it's easy to control how the form behaves if you open it with code. The VBA OpenForm method can be implemented using named arguments or the syntax:

DoCmd.OpenForm FormName, View, FilterName, WhereCondition, DataMode, WindowMode, OpenArgs

The argument we're interested in is DataMode, which accepts the following constants:

acFormAdd

- Limits you to working with new records

acFormEdit

- In addition to letting you add records, can change previously existing records

acFormReadOnly

- Lets you only view data, no changes are allowed

acFormPropertySettings

- A form's default behavior setting, causes Access to use the specified AllowEdits, AllowAdditions, AllowDeletions, and DataEntry property settings

For an example of how you can take advantage of the DataMode argument, let's say that you have a form named Customers. You then create another form with command buttons on it that control how Customers is opened. The first button, named cmdNewEntry opens the form solely for the purpose of adding new records. The second button, cmdEdits, lets you edit existing data or add new records. The last button, cmdReview, provides a read-only view of your data.

The code behind these buttons would resemble:

Private Sub cmdEdits_Click()

DoCmd.OpenForm FormName:="Customers", DataMode:=acFormEdit

End Sub

Private Sub cmdNewEntry_Click()

DoCmd.OpenForm FormName:="Customers", DataMode:=acFormAdd

End Sub

6Private Sub cmdReview_Click()

DoCmd.OpenForm FormName:="Customers", DataMode:=acFormReadOnly

End Sub

Created Date: 04/25/2003  Last Reviewed: 04/25/2003  Rev. Date: