Consulting

Results 1 to 5 of 5

Thread: Using a percentage in VBA

  1. #1

    Using a percentage in VBA

    good afternoon,

    I'm working on a user form where quality checks are completed in a system and dumps to Access. I am pulling this information from Access into an Excel userform.

    (user restrictions as to why this cant be done in access, so we can skirt around this)

    the issue im having is I need a caption to change depending on the score above it. But the score is a percentage? Does this matter when coding?

    for example
    If qcscore.Caption = "" Thenqcrate.Caption = "n/a"
    ElseIf qcscore.Caption < 85 And qcscore.Caption > 0 Then
    qcrate.Caption = "Below Expectations"
    ElseIf qcscore.Caption < 90 And qcscore.Caption > 85 Then
    qcrate.Caption = "Good"
    ElseIf qcscore.Caption > 90 And qcscore.Caption < 101 Then
    qcrate.Caption = "Outstanding"
    End If
    Problem I'm getting is even if the user has 100% across the board the caption reads "Below Expectations"
    if i change this to
    If qcscore.Caption = "" Thenqcrate.Caption = "n/a"
    End If
    If qcscore.Caption > 0 And qcscore.Caption < 80 Then
    qcrate.Caption = "Below Expectations"
    End If
    If qcscore.Caption > 80 And qcscore.Caption < 85 Then
    qcrate.Caption = "Good"
    End If
    If qcscore.Caption > 85 And qcscore.Caption < 101 Then
    qcrate.Caption = "Outstanding"
    End If
    I only get "outstanding".

    Can anyone advise what I'm doing wrong please?

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    the Caption property of something is usually a string. If you have the % character in there (a) it won't be able to implicitly convert the string to a number and (b) I'm surprised you don't get a Type mismatch error poppng up; do you have On Error Resume Next or somesuch in action while those lines of code are executing? What kind of control is qscore? It could be that you have to remove the % character before trying to numerically compare the caption:

    if replace(qscore.Caption,"%","") >0

    As an aside, if you have exactly 80, or exactly 85, qcrate's caption will not change at all since those values fall into no category. Taking just the 80 value, either:
    < 80 becomes <= 80
    or:
    > 80 becomes >= 80
    (preferably don't do both!).
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Tutor
    Joined
    Mar 2014
    Posts
    210
    Location
    FYI a better IF method is the CASE statement...esp. for readability:

    Select Case True
        Case qcscore.Caption = ""
            qcrate.Caption = "n/a"
        Case qcscore.Caption > 0 And qcscore.Caption < 80
            qcrate.Caption = "Below Expectations"
        Case qcscore.Caption > 80 And qcscore.Caption < 85
            qcrate.Caption = "Good"
        Case qcscore.Caption > 85 And qcscore.Caption < 101
        qcrate.Caption = "Outstanding"
    End Select

  4. #4
    only just picked up these replies to thanks. I did notice the exact 80 score etc, this was just a template to get it working before i got the official targets in to ensure it was working.

    All the qcscore is a % number pulled back from the access database. It is input from another external system (to which I have no way to control), I can change the label to a textbox if this will make it anyway easier. I just used label as it is easier to restrict user change.
    I'll give your if replace idea a go then come back with results. (or try text box) thanks.

    side note: thanks ranman for that. never sued case before

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by CuriousGeorg View Post
    (or try text box)
    Don't bother, it'll have the same problem of the .Value/.Text (it has no .Caption) not being coerced ino a number if it has a % character in it.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •