Check for Null

N

Newbie

I have a form where users enter account numbers, customer names and type of
account. In some instances, users are not required to enter type of account.
I want the form to check if type of account is required. I have the
following code on the On Exit:

If CustAcct > 0 And NewAcct = Null Then
MsgBox "You did not enter type of account opened. Please choose what type
of new account was opened.", vbOKOnly, "Missing New Account Type"
NewAcct.SetFocus
Else
SavAcctNum.SetFocus
End If

When I step through the code, it shows the custacct > 0 and newacct = null
but it skips to the Else code. It won't run the msgbox. Any ideas?
 
D

Douglas J. Steele

You can't check for null values using = Null. Use

If CustAcct > 0 And IsNull(NewAcct) Then
 
F

fredg

I have a form where users enter account numbers, customer names and type of
account. In some instances, users are not required to enter type of account.
I want the form to check if type of account is required. I have the
following code on the On Exit:

If CustAcct > 0 And NewAcct = Null Then
MsgBox "You did not enter type of account opened. Please choose what type
of new account was opened.", vbOKOnly, "Missing New Account Type"
NewAcct.SetFocus
Else
SavAcctNum.SetFocus
End If

When I step through the code, it shows the custacct > 0 and newacct = null
but it skips to the Else code. It won't run the msgbox. Any ideas?

If [CustAcct] > 0 And IsNull([NewAcct]) Then
 
A

Allen Browne

You need to use the IsNull() function to test for Null.
Nothing is ever equal to Null.

Try:
If CustAcct > 0 And IsNull(NewAcct) Then

For an explanation of why, see:
Common errors with Null
at:
http://allenbrowne.com/casu-12.html

BTW, I suggest you use the BeforeUpdate event of the *form* for record-level
validation (including anything that involves comparing fields.)
 
Top