If End If Construct (1 Viewer)

gbil

Registered User.
Local time
Yesterday, 19:50
Joined
Aug 30, 2013
Messages
26
Hello.

i have this code:

Private Sub cboOrgNick_LostFocus()
'making auto-populate textbox with incremental numbering
'if combo box is not empty
If Me!cboOrgNick = "" Or Me!cboOrgNick = Null Then
Exit Sub
'do nothing or
Else
'get new number
Me![nMembNum] = NewMembNum()
'prevent user from selecting the field
Me![tFName].SetFocus
End If

End Sub

even though cboOrgNick is empty (Null in debug window) it continue to execute after the Else Clause, executing the NewMembNum().

why is the construct do not exit even if is true?
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 12:50
Joined
Jan 20, 2009
Messages
12,856
Null cannot be tested with the equals operator.

Test for both Null and Null String with:

If Len(Me!cboOrgNick & vbNullString) = 0
 

gbil

Registered User.
Local time
Yesterday, 19:50
Joined
Aug 30, 2013
Messages
26
Null cannot be tested with the equals operator.

oh i did not know that.

thank you Galaxiom. the code now terminates properly.

:)
 

TJPoorman

Registered User.
Local time
Yesterday, 20:50
Joined
Jul 23, 2013
Messages
402
you could also do

Code:
If nz(Me!cboOrgNick, "") = "" Then

This basically converts a null string to ""
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 12:50
Joined
Jan 20, 2009
Messages
12,856
you could also do

Code:
If nz(Me!cboOrgNick, "") = "" Then

This basically converts a null string to ""

"" is the NullString.

The Nz expression posted converts a Null to a NullString.
 

Users who are viewing this thread

Top Bottom