if then on after update event of textbox in form

  • Thread starter Thread starter fipp
  • Start date Start date
F

fipp

I am trying to update a textbox called test based off of the value in the pen
field.
I wrote an if then else statement.

Here is what I have:
Private Sub kretydge_afterupdate()
If pen Is Null Then test = net + kretydge
Else: test = 0
End If
End Sub
 
You are close:

Private Sub kretydge_afterupdate()
If isnull (pen) Then
test = net + kretydge
Else
test = 0
End If
End Sub
 
I am trying to update a textbox called test based off of the value in the pen
field.
I wrote an if then else statement.

Couple of syntax errors. IS NULL is valid in SQL but not in VBA, you need the
IsNull() function instead. Also, you can't refer to form controls or table
fields directly, VBA will assume that they are variables. You can use
Me!controlname to refer to a control on the current form. Try

Private Sub kretydge_afterupdate()
If IsNull(Me!pen) Then
Me!test = Me!net + Me!kretydge
Else
Me!test = 0
End If
End Sub

Note that if either net or kretydge is NULL the sum will also be NULL - you
may want to use

Me!text = NZ(Me!net) + NZ(Me!kretydge)

You may also want to not store the sum AT ALL, if it's always just the sum of
net and kretydge or zero; instead you can display that sum in a textbox on the
form with an expression

=IIF(IsNull([Pen]), NZ([Net] + NZ([kretydge]), 0)

and don't store the result in any field in your table.
 
Back
Top