M.6. Preload Records

Sometimes, you'll have a problem when you open a detail form for a new record. If there is a subform with child records on the detail form, users will encounter an error if they try to create a child record before entering any data for the parent (master) record, as shown in Figure M-10.

Figure M.10. Figure m-10

The error occurs because the parent key still contains a Null, and the child record has a required foreign key for the parent Business record. Even if the foreign parent key in the child record isn't set to Required, another problem occurs: The child record will have a Null foreign parent key, making it an orphan record with no parent.

One way to prevent this error is to create an empty record before opening the Detail form. That way the parent record already has a primary key and can accept related child records. During this preloading operation, you can specify a default name for the new record, such as <New Business>, giving your user a clear indicator that she has just created a new record and allowing her to add child records immediately. Figure M-11 shows the new record.

Figure M.11. Figure m-11

The code to preload the record is in the index form frmBusinesses (refer to Figure M-5), in the Click event of the New button:

Private Sub cmdNew_Click() ...

Get Access™ 2007 VBA Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.