Solved Validation between two ranges

Juett

Registered User.
Local time
Today, 12:37
Joined
Jul 16, 2019
Messages
71
Hi All,

I'm having some difficulty with an IF statement that checks if a text field's value is between a specified range of numbers, depending on the option of a combo box. There are two number ranges available, and I wish to check/validate according to a specific range based on the option the combo box has selected. What I'd like to do is essentially:

ComboBox has either Option 1 or Option 2 available to select.

And the code behind the textfield is something like:

IF
Me.ComboBox.Value = "Option 1" Then Me.TextField = Between 1 and 10 or MsgBox "Error"
ElseIf Me.ComboBox.Value = "Option 2" Then Me.TextFeild = Between 11 and 12 or MsgBox "Error"
End If

I can handle single range validations and if statements, but combining them into something like the above is vexing me. I assume I'm not nesting the statements correctly maybe?

Thanks for any help offered.
 
Not indenting either? :(
Code:
IF Me.ComboBox.Value = "Option 1" Then
    Me.TextField = Between 1 and 10 or MsgBox "Error"
ElseIIf Me.ComboBox.Value = "Option 2" Then 
    Me.TextFeild = Between 11 and 12 or MsgBox "Error"
End If

Second textfield is misspelt? Do you have Option Explicit at the top of each module?
Nor sure what the or MsgBox "Error" is about

Code:
IF Me.ComboBox.Value = "Option 1" Then
    If Me.TextField < 1 or Me.TextField > 10 then
        MsgBox "Error"
    End If
ElseIIf Me.ComboBox.Value = "Option 2" Then 
    If Me.TextField < 11 or Me.TextField > 12 then
        MsgBox "Error"
    End If
End If
 
I get the "expected: end of statement" once the code reaches the Then in bold below. It's this kind of error that keep occurring. I can seem to get it to move on to the next IF.

IF Me.ComboBox.Value = "Option 1" Then
If Me.TextField < 1 or Me.TextField > 10 then
MsgBox "Error"
End If
ElseIIf Me.ComboBox.Value = "Option 2" Then
If Me.TextField < 11 or Me.TextField > 12 then
MsgBox "Error"
End If
End If
 
Spotted it - I removed the extra I from the elseIf, then what you suggested worked:
Code:
IF Me.ComboBox.Value = "Option 1" Then
If Me.TextField < 1 or Me.TextField > 10 then
MsgBox "Error"
End If
ElseIf Me.ComboBox.Value = "Option 2" Then
If Me.TextField < 11 or Me.TextField > 12 then
MsgBox "Error"
End If
End If

Thanks very much.
 
Indent your code :(
Easier to see logic errors. Plus you did not say whether have you Option Explicit at the top of every module?, as the misspelt field name would have been highlighted as an error?
 

Users who are viewing this thread

Back
Top Bottom