VBA & Macro Problems

How to Add a Related Subform Record When Saving a Main Record in Access

Ayan MasoodAyan Masood Nov 2, 2026 869 views

Question details

The user needs to automatically create a related child record in a status table when a new main record is saved in a Microsoft Access form.

How to Add a Related Subform Record When a New Main Record Is Saved
Product
Microsoft Access
Device & OS
not provided
Scenario
Developing a database form with a one-to-many relationship where closing the form or saving the main record should automatically generate a corresponding tracking record in a linked subform.
Observed behavior
The main record is successfully saved to the primary table when the form closes, but no corresponding child record is inserted into the related status table.
Before you start

Ensure that your primary table and status table share a properly configured foreign key relationship (e.g., RevID) and that referential integrity is strictly enforced in your Database Tools relationship window.

Solution 1Recommended

Use the Form's After Insert Event via VBA

This is the most reliable method because the After Insert event triggers immediately after the main record is saved, ensuring the primary key (RevID) has been fully generated.

By binding an SQL INSERT statement to the After Insert event of the main form, you guarantee that a primary key has been successfully committed to the database. This prevents foreign key errors when creating the related subform record.

1
Open the Form in Design View

Right-click your main Access form in the Navigation Pane and select Design View.

2
Access the Form Properties

Double-click the form selector box in the top-left corner to open the Property Sheet. Navigate to the Event tab.

3
Create the After Insert Code

Locate the 'On After Insert' property, click the ellipsis (...) button, and choose Code Builder.

4
Execute the Append Query

In the VBA editor, type the execution code using CurrentDb.Execute to run an INSERT INTO SQL statement. For example: CurrentDb.Execute "INSERT INTO tblStatus (RevID) VALUES (" & Me.RevID & ")", dbFailOnError.

5
Requery the Subform

Add a line of code below your SQL execution to refresh the subform data so the new record becomes visible instantly: Me.YourSubformControlName.Requery.

Use the Form's After Insert Event via VBA
Primary Key Timing: Using the After Insert event ensures you never attempt to save a child record before the parent record's ID physically exists in the database.
Free Microsoft Office alternative

Looking for a Lightweight and Versatile Office Suite?

While Microsoft Access is a robust database tool, many day-to-day data management and documentation tasks can be handled more efficiently with a unified office suite. WPS Office is a highly compatible, free alternative for Word, Excel, and PowerPoint tasks.

  1. 1. Download the Installer: Visit the official WPS Office website and download the free installation package for your operating system.
  2. 2. Run the Setup: Open the downloaded executable and follow the quick on-screen instructions to install the suite.
  3. 3. Manage Your Data: Open WPS Spreadsheet to import, filter, and organize your tabular data or CSV exports effortlessly.
Seamless compatibility with Microsoft Excel (.xlsx) and CSV formats for flat-file data managementFree, lightweight, and consumes fewer system resourcesFamiliar tabbed user interface, allowing you to manage multiple documents in one windowCross-platform support across Windows, Mac, Linux, iOS, and Android
microsoft office alternative - wps office

Frequently Asked Questions

Why isn't my subform updating automatically after inserting a new main record?

Even if the related record is successfully appended to the underlying table via VBA, the subform control on your screen will not reflect the changes until it is explicitly requeried. Add 'Me.YourSubformName.Requery' to the end of your VBA script.

Can I use Access Data Macros instead of VBA for this?

Yes. If you are using Access 2010 or newer, you can open the main table (tblMain) in design view and create an 'After Insert' Data Macro. Use the 'Create a Record' macro block targeting tblStatus to set the RevID automatically without using form-level VBA.

How do I handle required fields validation before the main record saves?

You should use the form's 'Before Update' event to validate data. If required fields are missing, you can display a message box and set 'Cancel = True'. This stops the record from saving, which in turn prevents the After Insert event from trying to create an orphaned subform record.