O'Reilly logo

Access™ 2007 VBA Programmer's Reference by Armen Stein, Geoffrey Griffith, Rob Cooper, Teresa Hennig

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required