Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stupid operator question

Status
Not open for further replies.

bfamo

Technical User
Feb 16, 2006
132
NO
Kind of a stupid question, but I'm too annoyed to go on.
I have been trying to get this to work and for some reason it does not.

I have the following code:

.fieldTest.Value = dblSum
If dblSum <= 1 And >= 10 Then
.LblHello.ForeColor = vbRed
Else
.LblHello.ForeColor = vbBlack
End If

The problem is that VB only allows me to have >= 10, and not <= 1 And >= 10. Does anyone know the easy thing that solves this?
 
It is indeed annoying. How about:
[tt]If dblSum <= 1 And dblSum >= 10 Then[/tt]
 
The lblHello.ForeColor = vbRed will never get executed with the code above, because there is no way that dblSum can be <=1 and >= 10 simultaneously.

You can use Or instead of And so that either condition will trigger the red coloured text. For example:

If (dblSum >= 1) Or (dblSum <= 10) Then
lblHello.ForeColor = vbRed
' ... do something else

I've put the conditions in brackets to make it easier to separate them.

John
 
I tried that right after I posted and It didnt work... It cant be this hard? :p
 
Right at the top - you are assigning the value of .fieldtest.value to whatever is in dblSum.

Do you initialise this value anywhere else?

John
 
Take a look at the full code:

Sub AvgSum()
Dim intCount As Integer
Dim dblSum As Double, y As Integer

For y = 1 To 3
If Me("Grp" & y) <> 0 Then
intCount = intCount + 1
dblSum = dblSum + Me("Grp" & y).Value
End If
Next y

If intCount <> 0 Then
dblSum = dblSum / intCount
End If

With Me
.txtAvg.Value = dblSum
If (dblSum >= 1) Or (dblSum <= 10) Then
.LblWarning.ForeColor = vbRed
Else
.LblWarning.BackStyle = 1
.LblWarning.ForeColor = vbBlack
End If
If (dblSum >= 11) Or (dblSum <= 20) Then
.LblWarning3.ForeColor = vbRed
Else
.LblWarning3.ForeColor = vbBlack
End If
If (dblSum >= 21) Or (dblSum <= 30) Then
.LblWarning4.ForeColor = vbRed
Else
.LblWarning4.ForeColor = vbBlack
End If
If (dblSum >= 31) Or (dblSum <= 40) Then
.LblWarning5.ForeColor = vbRed
Else
.LblWarning5.ForeColor = vbBlack
End If
End With

End Sub

Private Sub Grp1_AfterUpdate()
AvgSum
End Sub

Private Sub Grp2_AfterUpdate()
AvgSum
End Sub

Private Sub Grp3_AfterUpdate()
AvgSum
End Sub
 
Tried out your way John, but still no luck.
I can not think of some other easy way around this...

;|
 
OK, so if you run this through the debugger, does it calculate the total sum and average correctly? (this is up to the start of the "With Me" line).

John
 
Yeah I have checked all that out... works fine. Its just the lables that wont change color... hmmm...
 
Where are you calling AvgSum from? I think it needs to be on an On Format event to work.
 
What about something like this ?
If dblSum >= 1 And dblSum <= 10 Then
.LblWarning.ForeColor = vbBlack
Else
.LblWarning.ForeColor = vbRed
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top