If statments

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I cant get this If statment to work what am I missing
Private Sub Textbox1_afterUpdate()
If Me. Textbox1="Good" Then
Me. Textbox2="Bad"
End Sub
 
on one line:
If Me.Textbox1 = "Good" Then Me.Textbox2 = "Bad"

multiline:
If Me.Textbox1 = "Good" Then
Me.Textbox2 = "Bad"
End If
 
well, you need to get rid of the space after each Me., as

Me.Textbox1
not
Me. Textbox1

and close the If statement, as

If something Then
do this
End If

make sure you're compiling your modules after writing or editing code. a
compile attempt would have erred and told you that you had a "Compile Error:
Block If without End If". also, i normally indent the code within my
procedures - makes it easier to spot this sort of thing, as

Private Sub Textbox1_afterUpdate()

If Me.Textbox1="Good" Then
Me.Textbox2="Bad"

End Sub

in the above example, it's very easy to see that you're missing the End If.

hth
 
In addition to what the others have told you, make sure you can handle the
case where the user has deleted their input from Textbox1:

Private Sub Textbox1_AfterUpdate()

If Nz(Me.Textbox1, vbNullString) ="Good" Then
Me.Textbox2="Bad"
End If

End Sub

And do you perhaps want to sete Textbox2 to nothing if Textbox1 isn't Good?

Private Sub Textbox1_AfterUpdate()

If Nz(Me.Textbox1, vbNullString) ="Good" Then
Me.Textbox2="Bad"
End If
Me.Textbox2=vbNullString
End If

End Sub

or, more simply

Private Sub Textbox1_AfterUpdate()

Me.Textbox2=IIf(Nz(Me.Textbox1, vbNullString) ="Good", _
"Bad", vbNullString)

End Sub
 
Back
Top