Service Record Form Null Check not working as expected (1 Viewer)

JMongi

Active member
Local time
Today, 04:27
Joined
Jan 6, 2021
Messages
802
I have a button that is going to use DateDiff to calculate service call times.

I put a basic check in to verify that there are values in the two fields but it doesn't seem to be working as intended. I fear this is something super basic, please help.

Code:
Private Sub btnEndTime_Click()
Dim dt As Date

'Set the value to the current time
Me.EndTime = Now()

Debug.Print Me.StartTime

'Update the site time text box
If Me.StartTime <> Null Then
    MsgBox "Updating"
    dt = DateDiff("n", Me.EndTime, Me.StartTime)
    Me.txtSiteTime = dt
Else
   'Nothing
End If

End Sub

In stepping through the code, even when I can see that there are two date values in the fields and debug.print confirms that, the code portion of the IF statemetn won't run.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:27
Joined
Oct 29, 2018
Messages
21,523
That's because you can't compare Null to anything else, because it's unknown. Try using the IsNull() function instead.
 

JMongi

Active member
Local time
Today, 04:27
Joined
Jan 6, 2021
Messages
802
I think I've made this same mistake about 3 times every time I go to do something like this. I wonder where I got that idea in my head....
 

JMongi

Active member
Local time
Today, 04:27
Joined
Jan 6, 2021
Messages
802
Code:
Private Sub btnEndTime_Click()
Dim dt As Date

'Set the value to the current time
Me.EndTime = Now()

'Update the site time text box
If IsNull(Me.StartTime) = False Then
    dt = DateDiff("n", Me.EndTime, Me.StartTime)
    Me.txtSiteTime = dt
Else
   'Nothing
End If

End Sub

Took out the debugging parts, works great! Thanks!

Now to find the thread where it talks about formatting the difference into hh:mm:ss
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:27
Joined
Oct 29, 2018
Messages
21,523
Glad to hear you got it sorted out. Good luck!
 

Users who are viewing this thread

Top Bottom