Writing today's date into a field

TimTDP

Registered User.
Local time
Today, 23:49
Joined
Oct 24, 2008
Messages
213
I need to check if the date in a field is today's.
The table containing the field only has one record, and will never have more than one record.

I have tried two pieces of code:
Code:
Dim dteLastLogInDate As Date
 
    If IsNull(DLookup("LastLogInDate", "tblCompanyDetails")) Or DLookup("LastLogInDate", "tblCompanyDetails") <> Date Then
        dteLastLogInDate = DLookup("LastLogInDate", "tblCompanyDetails")
    
    If IsNull(dteLastLogInDate) Or dteLastLogInDate <> Date Then
        DoCmd.SetWarnings False
        DoCmd.RunSQL "Update tblCompanyDetails set LastLogInDate = " & Date
        DoCmd.SetWarnings True
    End If
The above code is inefficient because it does a DLookUp twice. I need to do the DLookUp twice because I need to test for a null value. If I just have DLookup("LastLogInDate", "tblCompanyDetails") <> Date and "LastLogInDate" is null I get an error

Code:
Dim dteLastLogInDate As Variant
 
    If IsNull(DLookup("LastLogInDate", "tblCompanyDetails")) Or DLookup("LastLogInDate", "tblCompanyDetails") <> Date Then
        dteLastLogInDate = DLookup("LastLogInDate", "tblCompanyDetails")
    
    If IsNull(dteLastLogInDate) Or dteLastLogInDate <> Date Then
        DoCmd.SetWarnings False
        DoCmd.RunSQL "Update tblCompanyDetails set LastLogInDate = " & Date
        DoCmd.SetWarnings True
    End If
In the code above "LastLogInDate" is not today's date!

What is the most efficient method to adopt?

Thanks in advance
 
I wouldn't bother checking the value in the table, just set it. And if you use CurrentDb.Execute, you don't even need to "Set Warnings" and your code becomes . . .
Code:
CurrentDb.Execute "UPDATE tblCompanyDetails SET LastLogInDate = #" & Date & "#"
 
why do you need to check if it could be null, can it ever be null?
And if the "LastLogInDate" is < today, do you need to perform some other event(s)

David
 

Users who are viewing this thread

Back
Top Bottom