I don't know your naming convention so:
the control bound to the field "V1", I named "cboV1"
the control bound to the field "V2", I named "cboV2"
the control bound to the field "V3", I named "cboV3"
And the controls bound to the associated date fields are:
"dtLastExamV1 ",
"dtLastExamV2" and
"dtLastExamV3".
In a standard module, add this code:
Code:
Option Compare Database '<- this line should be at the top of EVERY module
Option Explicit '<- this line should be at the top of EVERY module
Public Function GetLastExamDate(pVehicleID As Long) As Date
Dim d As DAO.Database
Dim r As DAO.Recordset
Dim sSQL As String
Set d = CurrentDb
'default return date
GetLastExamDate = #1/1/100#
sSQL = "SELECT TOP 1 ExamData.DateLastExam"
sSQL = sSQL & " FROM ExamData"
sSQL = sSQL & " WHERE ExamData.DateLastExam <= #" & Date & "# And ExamData.Vehicle_IDFK = " & pVehicleID
sSQL = sSQL & " ORDER BY ExamData.DateLastExam DESC;"
' Debug.Print sSQL
Set r = d.OpenRecordset(sSQL)
If Not r.BOF And Not r.EOF Then
GetLastExamDate = r("DateLastExam")
End If
r.Close
Set r = Nothing
Set d = Nothing
End Function
In the FORM module, usage would be like:
Code:
Private Sub cboV1_AfterUpdate()
Me.dtLastExamV1 = GetLastExamDate(Me.cboV1)
End Sub
Private Sub cboV2_AfterUpdate()
Me.dtLastExamV2 = GetLastExamDate(Me.cboV2)
End Sub
Private Sub cboV3_AfterUpdate()
Me.dtLastExamV3 = GetLastExamDate(Me.cboV3)
End Sub
You could set Conditional Formatting for the last exam date controls to have a red background if the date = #1/1/100#.
(This doesn't change my thoughts on your table designs. I still think you need to redesign them.)