Automatically Create a Related Document Record in Microsoft Access
Question details
Create a related document record after an internship record is added, without duplicating data or creating placeholder documents.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- An Access form saves an internship and, when a document is ready, adds a corresponding record to a shared DOCUMENT table.
- Observed behavior
- The document record should reference the internship and document type through foreign keys, while Access generates its document number automatically.
Identify the internship primary key, the matching foreign key in DOCUMENT, and the document-type key. Confirm that NUM_DOC is an AutoNumber field and that a document should be created when the form saves the internship.
Add the Document Record After the Internship Is Saved
Use the form's After Insert event when the document is ready at the same time as the internship record.
The example assumes numeric fields named InternshipID and DocumentTypeID, a table named DOCUMENT, and a form control named cboDocumentType. Replace these names with those in your database.
If the document is not yet ready, do not create a placeholder. Use a later document-submission action instead.
In Access, open Database Tools > Relationships. Connect the internship primary key to DOCUMENT.InternshipID and the document-type primary key to DOCUMENT.DocumentTypeID. Enforce referential integrity where appropriate.
Open the internship form in Design View. In the Property Sheet, select the form, choose the Event tab, and set After Insert to [Event Procedure]. Click the build button to open the VBA editor.
Add code to the form's AfterInsert procedure. This example skips the insert if no document type is selected: Private Sub Form_AfterInsert() On Error GoTo InsertError If IsNull(Me.cboDocumentType) Then Exit Sub CurrentDb.Execute "INSERT INTO DOCUMENT (InternshipID, DocumentTypeID) VALUES (" & CLng(Me.InternshipID) & ", " & CLng(Me.cboDocumentType) & ")", dbFailOnError Exit Sub InsertError: MsgBox "The internship was saved, but the document record was not created. " & Err.Description End Sub
Save the form, open it in Form View, select a document type, and add an internship. Check the DOCUMENT table for a new record with the correct foreign keys and an automatically generated NUM_DOC.

Create the Document When It Is Submitted
Use a separate document-entry form when the document is not available when the internship is saved.
Work With Internship Documents in WPS Office
WPS Office is a free, lightweight alternative for editing and sharing office documents, with a familiar interface that makes migration easy. It does not replace Microsoft Access or run Access database forms.

Frequently Asked Questions
Why should I omit NUM_DOC from the insert statement?
NUM_DOC is an AutoNumber field, so Access generates its value when the DOCUMENT record is inserted. The INSERT statement only needs the required foreign keys and any other required fields.
What if the document is submitted after the internship ends?
Do not create a placeholder. Use a DOCUMENT entry form or a submission button later, and supply the saved internship's primary key as the document's foreign key.
How should I handle an internship with multiple students?
Use separate internship and student tables with a junction table containing their foreign keys. This represents the many-to-many relationship without duplicating student details in DOCUMENT.
Can the DOCUMENT table also contain books?
Yes. A shared DOCUMENT table can hold different document types. Use a document-type foreign key and define relationships and required fields appropriate to each type.
Does WPS Office run an Access After Insert event?
No. The VBA form event runs in Microsoft Access. WPS Office can be used for related Word, Excel, and PowerPoint documents, but it does not replace Access database forms.




