If else Statement

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need help with this statement. It is looking at two text boxes and I want a
message to appear in a currently blank text box when conditions are met.

Private Sub Unit_Click()
If Me!TypeUnit = 1 Or 2 And Me!Totals > 15 Then
Me!Message = "Leak Rate Exceeded"
ElseIf Me!TypeUnit = 3 Or 4 And Me!Totals > 35 Then
Me!Message = "Leak Rate Exceeded"
Else
Me!Message = ""
End Sub

Thanks for the help!
 
You're missing the End If, and you can't use Or like that in VBA.

Private Sub Unit_Click()
If (Me!TypeUnit = 1 Or Me!TypeUnit = 2) And Me!Totals > 15 Then
Me!Message = "Leak Rate Exceeded"
ElseIf (Me!TypeUnit = 3 Or Me!TypeUnit = 4) And Me!Totals > 35 Then
Me!Message = "Leak Rate Exceeded"
Else
Me!Message = ""
End If
End Sub
 
You have to code it as:

If (Me!TypeUnit = 1 Or Me!TypeUnit =2) And Me!Totals > 15 Then

You need the parentheses for this to work.

It's probably easier to use a Select statement here. Just code:

Select case Me!TypeUnit
Case 1, 2
If Me!Totals > 15 Then
Me!Message = "Leak Rate Exceeded"
End If
Case 3, 4
If Me!Totals > 35 Then
Me!Message = "Leak Rate Exceeded"
End If
Case Else
Me!Message = vbnullstring
End Select

vbnullstring is a vb constant that represents "". It's slightly more
efficient to use it, since vb already has a variable defined to represent the
null string.
 
Thanks for the help- but when I put the new one in
Private Sub Unit_Click()
Select Case Me!TypeUnit
Case 1, 2
If Me!Totals > 15 Then
Me!Message = "Leak Rate Exceeded"
End If
Case 3, 4
If Me!Totals > 35 Then
Me!Message = "Leak Rate Exceeded"
End If
Case Else
Me!Message = vbNullString
End Select

It is still not working. Could there be something else I did wrong? I have
checked all the names- It is looking at a text box bound to an equation-is
that a problem?
Thanks again so much for the help!
 
Back
Top