Code Help - Automatically show true or false (1 Viewer)

stu_c

Registered User.
Local time
Today, 06:42
Joined
Sep 20, 2007
Messages
489
Hi all
I am trying to write some IF code on a form to do the following but doesn't seem to work, can someone give me some pointers please

Field Names
DateIn
DateOut
Avaliable (Yes/No)

Basically what I need is when DateIn has a date and DateOut is blank then Avaliable field is FALSE, when both DateIn and DateOut have dates then the Avaliable field is TRUE

I am very new to the coding scene so please don't laugh if its completely wrong ;/

Code:
Private Sub DateIn_AfterUpdate()
If Me.DateIn > Null And Me.DateOut = Null Then
Me.VehicleAvaliable = False
 
End If
 
If Me.DateIn > Null And Me.DateOut > Null Then
Me.VehicleAvaliable = True
 
End If
End Sub
 

Ranman256

Well-known member
Local time
Today, 01:42
Joined
Apr 9, 2015
Messages
4,337
Null is not a number, you must use IsNull():

If Not IsNull(Me.DateIn) And IsNull(Me.DateOut) Then

(in queries its: [field] is null )
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 01:42
Joined
May 21, 2018
Messages
8,527
can be done in a single line
Code:
Me.VehicleAvaliable = (isDate(me.datein) and isdate(me.dateOut))
 

MarkK

bit cruncher
Local time
Yesterday, 22:42
Joined
Mar 17, 2004
Messages
8,181
But, looking at this pattern, I expect you'll need something more along the lines of...
Code:
Private Sub DateIn_AfterUpdate()
   SetAvailability Me.DateIn, Me.DateOut
End Sub

Private Sub DateOut_AfterUpdate()
   SetAvailability Me.DateIn, Me.DateOut
End Sub

Private Sub Form_Current()
   SetAvailability Me.DateIn, Me.DateOut
End Sub

Private Sub SetAvailability(DateIn as Variant, DateOut as Variant)
   Me.VehicleAvaliable = IsDate(DateIn.Value) And IsDate(DateOut.Value)
End Sub
hth
Mark
 

Mark_

Longboard on the internet
Local time
Yesterday, 22:42
Joined
Sep 12, 2017
Messages
2,111
From what I gather, DateIn is not relevant to your needs. You are only checking if the vehicle is out or not, correct? Would you ever have a dateout without a preceeding datein?

This can make some of what you do much easier as you don't really need a flag, you just need to check if DateOut is null or blank. Then everything that would otherwise be driven by an "Available" flag is based off of DateOut.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:42
Joined
Feb 19, 2002
Messages
43,263
What you are trying to do is a violation of second normal form. You are creating a column whose value is dependent on other columns rather than the primary key.

As Mark_ suggested, eliminate the flag and just keep the dates. If IsNull(DateIn) = False then the vehicle is available.
 

Users who are viewing this thread

Top Bottom