Solved Text Box Color Changing Only At Debug But Not At OnLoad event code (1 Viewer)

Ashfaque

Student
Local time
Today, 18:15
Joined
Sep 6, 2004
Messages
894
Hi,

For some reason, I am calculating some %age in a text box using vba codes in form's OnLoad event.

I have number of other text boxes with similar logical code to change the colors and the code is working.

But when the calculated %age figure goes more than 100, the color effect does not take place.

But when I debug the code step-by-step using F8 function key, it provide the textbox color as per condition.

Is there a way to correct it?
Code:
 If TxtAdmPerc.Value < 24.15 Then
               TxtAdmPerc.BackColor = RGB(255, 0, 0) ' Red
               TxtAdmPerc.ForeColor = RGB(255, 255, 255) ' white
               TxtAdmPerc.FontBold = 12
    ElseIf TxtAdmPerc.Value >= 24.15 And TxtAdmPerc.Value < 40.15 Then
                TxtAdmPerc.BackColor = RGB(52, 79, 33) ' Light Green
                TxtAdmPerc.ForeColor = RGB(255, 255, 255) ' white
    ElseIf TxtAdmPerc.Value >= 40.15 And TxtAdmPerc.Value < 75.15 Then
                TxtAdmPerc.BackColor = RGB(255, 194, 14) ' Yellow
                TxtAdmPerc.ForeColor = RGB(255, 255, 255) ' white
    ElseIf TxtAdmPerc.Value >= 75.15 Then
                TxtAdmPerc.BackColor = RGB(84, 130, 53) ' Green
                TxtAdmPerc.ForeColor = RGB(255, 255, 255) ' white
    End If
 

Ranman256

Well-known member
Local time
Today, 08:45
Joined
Apr 9, 2015
Messages
4,339
Try putting the code in ON CURRENT instead of ON LOAD.
so the data can load.
 

Gasman

Enthusiastic Amateur
Local time
Today, 13:45
Joined
Sep 21, 2011
Messages
14,234
Anything in an OnLoad event occurs just once?, unless specifically called again.?
Forecolor appears to be white all the time, so no need to repeat?
 

Minty

AWF VIP
Local time
Today, 13:45
Joined
Jul 26, 2013
Messages
10,368
Are you saying the other conditions definitely work with the code as expected?

Try it like this, a select case only evaluates the first true case, and I think is slightly more intuitive that elseif's
SQL:
SELECT CASE Me.TxtAdmPerc
    Case >= 75.15
                Me.TxtAdmPerc.BackColor = RGB(84, 130, 53) ' Green
                Me.TxtAdmPerc.ForeColor = RGB(255, 255, 255) ' white
    Case >= 40.15
                Me.TxtAdmPerc.BackColor = RGB(255, 194, 14) ' Yellow
                Me.TxtAdmPerc.ForeColor = RGB(255, 255, 255) ' white
    Case >= 24.15
                Me.TxtAdmPerc.BackColor = RGB(52, 79, 33) ' Light Green
                Me.TxtAdmPerc.ForeColor = RGB(255, 255, 255) ' white
    Case Else
                Me.TxtAdmPerc.BackColor = RGB(255, 0, 0) ' Red
               Me.TxtAdmPerc.ForeColor = RGB(255, 255, 255) ' white
               Me.TxtAdmPerc.FontBold = 12
End Select
 

Ashfaque

Student
Local time
Today, 18:15
Joined
Sep 6, 2004
Messages
894
Try putting the code in ON CURRENT instead of ON LOAD.
so the data can load.
Thanks Ranman256

I shifted codelines to OnCurrent event of the form but is still same. I have attached pic just for ref. Percentage that is equal or less than 100....displaying colr effect but if more than 100 then code is not working I believe.
 

Attachments

  • 1.jpg
    1.jpg
    43.5 KB · Views: 223

Ashfaque

Student
Local time
Today, 18:15
Joined
Sep 6, 2004
Messages
894
Select case is no doubt good idea. It worked.

Thanks for all your support.

(y)(y)
 

Users who are viewing this thread

Top Bottom