How do I recognise an empty field?

GreenshootProgrammer

Registered User.
Local time
Today, 16:33
Joined
Jan 16, 2013
Messages
74
Why can't I get this simple piece of code to work?

Code:
Private Sub txtDateBack_AfterUpdate()

    ' updates loan status
    txtLoaned = 0
    If txtDateBack = Null Then txtLoaned = 1
    
End Sub
Code:
Private Sub txtDateBack_AfterUpdate()

    ' updates loan status
    txtLoaned = 0
    If txtDateBack = "" Then txtLoaned = 1
    
End Sub
 
Use functions IsNull(x) - returns T/F based on Null/Not Null
IsEmpty(x) - returns T/F based on empty (blank)/Not Empty

Also...

If nz(x,"") = "" then...
 
Code:
If IsNull(Me.txtDateBack) Or Me.txtDateBack = "" Then

Update: Don't forget about the End If
 
Last edited:
A couple of things:

  • IsEmpty is used to tell whether a Variable has been initialized, whether or not it has a value; it is not used to test Controls.
  • Doc's suggestion of If Nz(x,"") = "" Then, as well as Foe's example, both have the advantage of checking for Zero-Length Strings, as a well as Null, which can be important, depending on the particular app. There are a couple of there variations on these; most developers have the own favorite.
  • Lastly, and most importantly, you cannot use a Control's AfterUpdate event to check whether or not that Control actually has data in it! If the user simply ignores the Control, i.e. doesn't bother to enter the Control, the Validation is useless, because the AfterUpdate event will not fire; it will only fire if data is entered or edited in the Control! Code to validate whether or not a Control has data in it has to be done in the Form_BeforeUpdate event.
Linq ;0)>
 
Last edited:
txtLoaned = 0
If txtDateBack = Null Then txtLoaned = 1

the logic probably does what you want, but you cannot test anything to be equal to null.

instead, this would probalbly work,

txtLoaned = 0
If isnull(txtDateBack) Then txtLoaned = 1

As others have pointed out though, there are other nuances to consider, as well.
 

Users who are viewing this thread

Back
Top Bottom