Solved If statement to check if a text box holding a % is at 100.00% (1 Viewer)

Snuggle Walrus

New member
Local time
Today, 09:09
Joined
May 14, 2020
Messages
25
Hey all. I'm trying to write some code that checks if the % of a text box (formatted as a percentage with 2 decimal places) is at 100.00% but i cant seem to get it to work. :( I've tried a few things which i'll list after my code :).

  • PercentChecker is the text box holding the % and is calculated based on the other percentages updating.
  • This code is on an "After Update" for a box which affects the calculation of the Percent Checker
  • The Red part seems to work but i'm unsure if my % check is working but at least it does change its colour from here so I know that it will once i get my code right hopefully change to green.

Code:
Dim VCurrentPercent As Integer
Dim VRed As Long
Dim VGreen As Long

On Error GoTo OPSYSError

VCurrentPercent = Me.PercentChecker
VGreen = RGB(0, 255, 0)
VRed = RGB(255, 0, 0)


If VCurrentPercent = 100.00 Then
    Me.PercentChecker.ForeColor = VGreen
    End If
If VCurrentPercent <> 100 Then
    Me.PercentChecker.ForeColor = VRed
    End If

I've tried If VCurrentPercent = "100.00%", 1, 1.0 and 100 as well but none of these seem to work :( what am i doing wrong :(? Thanks have a lovely day!
 

theDBguy

I’m here to help
Staff member
Local time
Today, 01:09
Joined
Oct 29, 2018
Messages
13,163
Hi. I would have said try 1, but you already did that. So, it probably depends on how you populate the textbox with the percent value. Can you show us that part? Maybe it's a Text value?
 

Snuggle Walrus

New member
Local time
Today, 09:09
Joined
May 14, 2020
Messages
25
I'm not sure how to post pictures so i'll do my best to explain it. I have potentially 9 boxes that have %'s all are default at 0 to begin with. The PercentChecker has the control source of....

Code:
=[ChildItem1_Percent_txt]+[ChildItem2_Percent_txt]+[ChildItem3_Percent_txt]+[ChildItem4_Percent_txt]+[ChildItem5_Percent_txt]+[ChildItem6_Percent_txt]+[ChildItem7_Percent_txt]+[ChildItem8_Percent_txt]+[ChildItem9_Percent_txt]

which just adds up all the %'s in those potential 9 boxes. Both the boxes and the PercentChecker have the format "Percentage" and 2 decimal places (not sure if i'll need these yet or not so i added them for just in case I want to be really specific with the percentages). I can change the numbers in the 9 boxes but i have locked the PercentChecker. The code of....

Code:
Private Sub ChildItem1_Percent_txt_AfterUpdate()
Dim VCurrentPercent As Integer
Dim VRed As Long
Dim VGreen As Long

On Error GoTo OPSYSError

VCurrentPercent = Me.PercentChecker
VGreen = RGB(0, 255, 0)
VRed = RGB(255, 0, 0)


If VCurrentPercent = 1 Then
    Me.PercentChecker.ForeColor = VGreen
    End If
If VCurrentPercent <> 100 Then
    Me.PercentChecker.ForeColor = VRed
    End If

OPSYSError:
Exit Sub


If (Me.ChildItem1_Qty_txt >= 1 And Me.ChildItem1_Percent_txt >= 0 And Me.ChildItem1_Selection_CB <> 147) Then
    Me.ChildItem2_Selection_CB.Visible = True
    Me.ChildItem2_Percent_txt.Visible = True
    Me.ChildItem2_Qty_txt.Visible = True
End If
End Sub

I'm wondering if its because i'm storing it as an integer? maybe i need a different type but i cant see an option to store a percentage so i assumed integer would be ok? So this code is nearly identical throughout the other 8 boxes besides the bottom if checks that just makes the other visible almost like adding a new product line.

Does this help explain it? :)
 

plog

Banishment Pending
Local time
Today, 03:09
Joined
May 11, 2011
Messages
10,126
You're just shooting arrows into the air hoping that you hit a target. Find out what target you are aiming at:

Set the conditions such that Me.PercentChecker should trip your condition, then find out exactly what is in
Me.PercentChecker with either Debug.Print or MsgBox(Me.PercentChecker), then update your criteria such that the actual value in
Me.PercentChecker trips it correctly.
 

arnelgp

error reading drive A:
Local time
Today, 17:09
Joined
May 7, 2009
Messages
10,844
you may also use Single for your percent variable, since it can hold a a value of a fraction up to 1.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 09:09
Joined
Feb 19, 2013
Messages
12,573
format has no impact on the underlying value which is what you are adding - so masking the true values. Suggest clear the format property so you can see what you actually have.

Consider using the round function when calculating your individual percentages
 

Snuggle Walrus

New member
Local time
Today, 09:09
Joined
May 14, 2020
Messages
25
got it working funnily enough changing it to single instead of integer was the solution haha. Thankyou @arnelgp
 

Users who are viewing this thread

Top Bottom