IIf(Nz) statement

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What is my mistake here. I have a number field that I want a number added to.

I want to know which day of an event a specific date is, i.e. day 1 or day
10. I set the default valve of the field to 0 because if I didn't the true
part of the following statement didn't work. What I can't get it to do is go
from one to 2 for the second day. How should I be handling the false part of
this statment? Thank you.

Private Sub txtDate_AfterUpdate()
Dim NewDay As Variant

NewDay = IIf(Nz(txtDay), 1, ((txtDay) + 1))
Me.txtDay = NewDay

End Sub

Fay
 
The Nz function takes the value passed to it, and returns the value if it's
non-null, or 0 if it is null.

The IIf function evaluates the first parameter, and choses the second
parameter if the first parameter is True, or the second parameter if the
first parameter is False.

In Access, False is 0, and and non-zero value is evaluated as True (although
True is normally -1)

Looking at your code, if txtDay is Null, it'll be assigned a value of 0,
which will be evaluated as False, so you'll be going to the ((txtDay) + 1))
section of your IIf statement. Null + 1 is Null.

If txtDay is not null, its value will be evaluated as True, so you'll be
going to the 1 section of your IIf statement.

All you need is:

Me.txtDay = Nz(Me.txtDay, 0) + 1
 
Okay I think all of the code is working as it should BUT I am not. The txtDay
should start with 1 for the first day of a session and go up for every day
added to that session number. So I think I need to do a DMax on the SessionID
then do the above.
 
Back
Top