Public Sub tblUpdateLatestDocuments(Optional VesselID As Long, Optional DocumentID As Long)
'------------------------------------
'put a counter to time sub execution
Dim t As Long
t = GetTickCount
'------------------------------------
Dim db As DAO.Database
Dim rs(1) As DAO.Recordset
Dim strSQL(1) As String
'Here is the most complete working recordset
'we might require to work with:
strSQL(0) = "SELECT tblVessels.[#], tblDocumentNames.[#] " & _
"FROM tblVessels INNER JOIN (tblDocumentNames INNER JOIN tblDocuments ON tblDocumentNames.[#] = tblDocuments.document_ID) ON tblVessels.[#] = tblDocuments.vessel_ID"
'------------------------------------
'The optional arguments are intended to restrict the above working recordset
'to only the specific document set we require.
'Let's narrow down the working recordset, if there are any optional arguments
If IsNull(VesselID) Then
'We have no vessel ID
If IsNull(DocumentID) Then
'We have no vessel ID nor document ID.
'There is no criteria to add to the SQL query,
'meaning we'll work on the full recordset
'-------
'So we need to know how many vessels and document names we have
'before we can loop through them
Dim lgNumberOfVessels, lgNumberOfDocumentNames As Long
lgNumberOfVessels = DMax("[#]", "tblVessels")
lgNumberOfDocumentNames = DMax("[#]", "tblDocumentNames")
Else
'We only have a document ID
strSQL(0) = strSQL(0) & " WHERE (tblDocuments.document_ID)=" & DocumentID & ") "
End If
'*************************
Else
If IsNull(DocumentID) Then
'We only have a vessel ID
strSQL(0) = strSQL(0) & " WHERE (((tblVessels.[#])=" & VesselID
Else
'We have both a vessel ID and a document ID
strSQL(0) = strSQL(0) & " WHERE (((tblVessels.[#])=" & VesselID & _
") AND ((tblDocuments.document_ID)=" & DocumentID & ")) "
End If
End If
Set rs(0) = CurrentDb.OpenRecordset(strSQL(0), dbOpenDynaset)
'Check to see if the 1st recordset actually contains rows
If Not (rs(0).EOF And rs(0).BOF) Then
rs(0).MoveFirst 'Unnecessary in this case, but still a good habit
'************************
'LOOP
Do Until rs(0).EOF = True
'Let's make sub-recordsets for each vessel/document_name
'(comparing apple to apple, with a SQL query
strSQL(1) = "SELECT tblDocuments.vessel_ID, tblDocuments.document_ID, tblDocuments.expiryDate, tblDocuments.latest " & _
"FROM tblDocuments " & _
"WHERE (((tblDocuments.vessel_ID) = " & rs(0)![tblVessels.#] & ") And ((tblDocuments.document_ID) = " & rs( _
0)![tblDocumentNames.#] & ")) ORDER BY tblDocuments.expiryDate DESC, tblDocuments.issuingDate DESC" _
'We define the latest documents as the document with the latest expiry date
'or if no expiry date, with the latest issuance date.
'In any case, this should be the first document in the above SQL query
Set rs(1) = CurrentDb.OpenRecordset(strSQL(1), dbOpenDynaset)
With rs(1)
'If the recordset is empty, do nothing and move on to the next sub-recordset.
If .EOF Then
Else
'Take the first record
.MoveFirst
'Check whether the first document has an expiry date
'If IsNull(![expiryDate]) Then
.Edit
![latest] = True
.Update
'Now, update all the next records as 'not latest'
'Move to the next record
.MoveNext
'If there is a next record, it is not the latest document
While .EOF = False
.Edit
![latest] = False
.Update
.MoveNext
Wend
'Else
'End If
End If
.Close
End With
rs(0).MoveNext
Loop
End If
rs(0).Close
'------------------------------------
Debug.Print "Public Sub tblUpdateLatestDocuments took " & GetTickCount - t; " milliseconds to execute."
'------------------------------------
End Sub