Public Function GetLatestDate(Optional sWhereCondition As String = "")
'Usage:
' ?GetLatestDate("RecID = 23")
'---------------------------------------------------------------------------------------------------
' Name of table with fields "Phone", "Email" & "Text" ("Text" is reserved word !!!)
Const csTableName = "Your Table Name"
Const csTextFieldName = "TText" ' Your [Text] field Name ("Text" is reserved word !!!)
Dim sVal$, sFromAndWhere$
Dim rst As DAO.Recordset
' -------------------------------------------------------------------------------------------------/
On Error GoTo GetLatestDate_Err
sFromAndWhere = " FROM [" & csTableName & "]"
If Len(sWhereCondition) > 0 Then
sFromAndWhere = sFromAndWhere & " WHERE (" & sWhereCondition & ")"
End If
sVal = "Select TOP 1 * FROM (" & vbCrLf & _
" SELECT Phone AS LatestDate" & sFromAndWhere & vbCrLf & _
" UNION ALL " & vbCrLf & _
" SELECT Email" & sFromAndWhere & vbCrLf & _
" UNION ALL " & vbCrLf & _
" SELECT [" & csTextFieldName & "]" & sFromAndWhere & vbCrLf & _
") AS SubQ ORDER BY LatestDate DESC;"
Set rst = CurrentDb.OpenRecordset(sVal, dbReadOnly)
GetLatestDate = rst.Fields(0)
GetLatestDate_End:
On Error Resume Next
rst.Close: Set rst = Nothing
Err.Clear
Exit Function
GetLatestDate_Err:
Err.Clear
Resume GetLatestDate_End
End Function