Code Help - Automatically show true or false

stu_c

Registered User.
Local time
Today, 06:01
Joined
Sep 20, 2007
Messages
494
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
 
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 )
 
can be done in a single line
Code:
Me.VehicleAvaliable = (isDate(me.datein) and isdate(me.dateOut))
 
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
 
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.
 
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

Back
Top Bottom