Need help with Access VB nested If Statement

markey

Registered User.
Local time
Today, 00:18
Joined
Oct 25, 2007
Messages
17
I'll admit I'm a newb.....any help is much appreciated. I'm getting "else without if" errors. I know this is self explanitory, but I'm still not getting it after trying multiple variations.

txtTBASS1 is a text box on a form (frmService)
txtTBASS1pts is a text box on a form (frmService)

Here is my code:

Private Sub txtTBASS1_LostFocus()
If Me.txtTBASS1 <= 43.49 Then
Me.txtTBASS1pts = 0
Else
If Me.txtTBASS1 > 44.5 < 51.99 Then
Me.txtTBASS1pts = 40
End If
Else
If Me.txtTBASS1 > 52 < 59.99 Then
Me.txtTBASS1pts = 70
End If
Else
If Me.txtTBASS1 > 60 < 67.99 Then
Me.txtTBASS1pts = 135
End If
Else
If Me.txtTBASS1 > 68 < 75.99 Then
Me.txtTBASS1pts = 205
End If
Else
If Me.txtTBASS1 > 76 Then
Me.txtTBASS1pts = 270
End If
End Sub
 
You appear to have discrete values - not applicable to using "ELSE", skip the nesting. Just have a range.
Code:
If Me.txtTBASS1 <= 43.49 Then Me.txtTBASS1pts = 0
If Me.txtTBASS1 > 44.5 < 51.99 Then Me.txtTBASS1pts = 40 
If Me.txtTBASS1 > 76 Then Me.txtTBASS1pts = 270
 
A Select Case statement would be cleaner.
Code:
Select Case Me.txtTBASS1
   Case Is < 43.5
      Me.txtTBAS1pts = 0
   Case Is >= 43.5 And < 52
      Me.txtTBASS1pts = 40
   Case Is >= 52 And < 60
      Me.txtTBASS1pts = 70
   Case Is >= 60 And < 68
      Me.txtTBASS1pts = 135
   Case >= 68 And < 76 
      Me.txtTBASS1pts = 205
   Case >= 76
     Me.txtTBASS1pts = 270
End Select
 
Agree with the previous 2 answers, and I would use Select Case. But if you want to use nested Ifs, the problem is you haven't closed the first If. Add another End If before End Sub.
 
And, to complete the circle of choices, here is the IIF to add to the list... :)
Code:
With Me.txtTBASS1pts

  .Value = IIF(Me.txtTBASS1 <= 43.49, 0, _
    IIF(Me.txtTBASS1 <= 51.99, 40, _
      IIF(Me.txtTBASS1 <= 59.99, 70, _
    IIF(Me.txtTBASS1 <= 67.99, 135, _
  IIF(Me.txtTBASS1 <= 75.99, 205, 270)))))

End With
 
Thanks!!!!!!!

And, to complete the circle of choices, here is the IIF to add to the list... :)
Code:
With Me.txtTBASS1pts

  .Value = IIF(Me.txtTBASS1 <= 43.49, 0, _
    IIF(Me.txtTBASS1 <= 51.99, 40, _
      IIF(Me.txtTBASS1 <= 59.99, 70, _
    IIF(Me.txtTBASS1 <= 67.99, 135, _
  IIF(Me.txtTBASS1 <= 75.99, 205, 270)))))

End With

Thanks!!!!!! This worked like a charm. Thanks to everyone else for all the great responses. I appreciate it
 

Users who are viewing this thread

Back
Top Bottom