If Then problem

  • Thread starter Thread starter Jeroen Verdrengh
  • Start date Start date
J

Jeroen Verdrengh

Hi,

Please have a look at this code:

Dim varX As Variant
varX = DLookup("[Field]", "Query")
If (varX <> Null) Then [Form].Label.Caption = varX

Why does the VBA interpreter refuses to execute [Form].Label.Caption = varX
even when varX has a value that is not null (I can see it in debug mode)??
[Field] is the number of records in Query (aggregate function).

greets,

Jeroen
 
From the VB Help section:

Use the IsNull function to determine whether an expression contains a Null
value. Expressions that you might expect to evaluate to True under some
circumstances, such as If Var = Null and If Var <> Null, are always False.
This is because any expression containing a Null is itself Null and,
therefore, False
 
Jeroen said:
Hi,

Please have a look at this code:

Dim varX As Variant
varX = DLookup("[Field]", "Query")
If (varX <> Null) Then [Form].Label.Caption = varX

Why does the VBA interpreter refuses to execute [Form].Label.Caption = varX
even when varX has a value that is not null (I can see it in debug mode)??
[Field] is the number of records in Query (aggregate function).

greets,

Jeroen
Jeroen,
Not clear to me whether the label is on the same form that contains this
code, or is on a different form.
If it's on the same form as the above code, then try:

If IsNull(varX) Then
Else
Me!LabelName.Caption = varX
End If

If the Label is on a different form, and the form is open at the time
this code is run, then use:

If IsNull(varX) Then
Else
forms!FormName!LabelName.Caption = varX
End If

varX must be a string not a number.
Either Dim varX as String or use quotes around it:

Me!LabelName.Caption = chr(34) & varX & chr(34)
 
Back
Top