Compare Dates Between Related Tables in Microsoft Access
Question details
Compare dates entered on a form with dates in a related table and prevent entries that violate the intended rule.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A main table contains a final date, and a related child table can contain several dates for each main record.
- Observed behavior
- The user wants Access to check the relevant related dates and reject an invalid entry before it is saved.
Identify the primary key, child-table foreign key, and date field names, and decide which date comparisons should be allowed. The example below assumes a numeric key and a rule that no child date may be later than the parent’s final date.
Validate Parent and Child Dates in Access Forms
Use form Before Update events to check the applicable related dates before a record is saved.
This example uses ParentTable, ChildTable, ParentID, FinalDate, and ChildDate as placeholder names. Replace them with your table, field, and form control names. Equality is allowed; change the comparison operators if your rule differs.
The child-form check uses DLookup to retrieve the parent date. The parent-form check uses DMax to find the latest saved child date, so it can reject a final date that is too early.
Open the child entry form in Design View. In the form’s Property Sheet, select the Event tab, set Before Update to Event Procedure, and click the builder button.
Add this code to the child form’s Before Update event, replacing the example names: Private Sub Form_BeforeUpdate(Cancel As Integer) Dim parentDate As Variant If IsNull(Me!ParentID) Or IsNull(Me!ChildDate) Then MsgBox "Select a parent and enter a child date." Cancel = True Exit Sub End If parentDate = DLookup("FinalDate", "ParentTable", "ParentID=" & Me!ParentID) If IsNull(parentDate) Then MsgBox "The parent record must have a final date." Cancel = True ElseIf Me!ChildDate > parentDate Then MsgBox "The child date cannot be later than the final date." Cancel = True End If End Sub
Open the parent form’s Before Update event in the same way. Add this code to prevent an existing final date from being changed to a date earlier than a saved child date: Private Sub Form_BeforeUpdate(Cancel As Integer) Dim latestChild As Variant If IsNull(Me!FinalDate) Then MsgBox "Enter a final date." Cancel = True Exit Sub End If If IsNull(Me!ParentID) Then Exit Sub latestChild = DMax("ChildDate", "ChildTable", "ParentID=" & Me!ParentID) If Not IsNull(latestChild) Then If Me!FinalDate < latestChild Then MsgBox "The final date cannot be earlier than a child date." Cancel = True End If End If End Sub
Save the forms, switch to Form View, and try child dates before, equal to, and after the final date. Then try changing the parent’s final date to a value earlier than an existing child date.

Work with Office Documents and Date Lists in WPS Office
WPS Office is a free, lightweight, easy-to-use Office alternative with a familiar interface. Open compatible documents and spreadsheets for a smooth transition, while continuing to use Access for database forms and validation.

Frequently Asked Questions
Why use DMax instead of DLookup for the parent form?
A parent can have several child dates. DMax retrieves the latest saved ChildDate for the selected ParentID, which is the relevant value when every child date must be on or before the final date.
What happens if the parent has no child records?
DMax returns Null when it finds no matching child dates. In the example, the parent form still requires a final date but skips the child-date comparison.
Can I reverse the date rule?
Yes. First define the rule, then change the comparison operators in both Before Update events. If every child date must be on or after the final date, use DMin instead of DMax for the parent check.
Will this stop changes made directly in a table?
No. Form Before Update events run only when the associated form saves a record. Direct table edits, queries, and other forms need their own safeguards.




