if then on after update event of textbox in form

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
 
M

Maurice

You are close:

Private Sub kretydge_afterupdate()
If isnull (pen) Then
test = net + kretydge
Else
test = 0
End If
End Sub
 
J

John W. Vinson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top