Question

I have a userform with 2 Textboxes and Label. Textboxes are meant for inserting decimal values. Label role is to show the result of 1st value divided by 1nd value(Textboxes).

I am trying to validate if inserted values are correct. If they are wrong Label.Caption will become red.

I have a code like this:

If Me.netto.Value = "" And Me.vat.Value = "" Or Me.vat.Value = "" Or Me.netto.Value = "" Then

Else
Procent_check.Caption = FormatPercent(CDbl(Me.vat) / CDbl(Me.netto), 2)

    If Procent_check.Caption >= 27 Or Procent_check.Caption <= 15 Then Else: Procent_check.ForeColor = vbRed

End If

When executed, it shows error at line If Procent_check.Caption >=... saying type mismatch.

No idea what type should I provide to check if value equals or higher than 27 or equals or smaller than 15...

Edit: Code for now, does not work color change...

If Me.netto.Value <> "" And Me.vat.Value <> "" _
And Me.vat.Value <> 0 And Me.netto.Value <> 0 Then

proc = CDbl(Me.vat) / CDbl(Me.netto)
MsgBox (proc)
MsgBox (Round(proc, 2))
Procent_check.Caption = FormatPercent(proc, 2)
    If Round(proc, 2) >= 0.27 Or Round(proc, 2) <= 0.15 Then
        Procent_check.ForeColor = vbBlack
    Else
        Procent_check.ForeColor = vbRed
    End If

End If
Was it helpful?

Solution

FormatPercent returns string in format "20%" and you can't comare this string with numbers.

Try this one instead:

Dim res As Double
If Me.netto.Value <> "" And Me.vat.Value <> "" And Me.netto.Value <> 0 _
        And IsNumeric(Me.netto.Value) And IsNumeric(Me.vat.Value) Then
    res = CDbl(Me.vat) / CDbl(Me.netto)
    Procent_check.Caption = FormatPercent(res, 2)

    If res >= 0.27 Or res <= 0.15 Then
        Procent_check.ForeColor = vbBlack
    Else
        Procent_check.ForeColor = vbRed
    End If
Else
    Procent_check.Caption = ""
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top