Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UserForm Type Mismatch Error 1

Status
Not open for further replies.

OscarAlberto

Programmer
Dec 4, 2002
15
CR
I have code writen for several Textboxes in a UserForm to obtain input from the user. Some of them produce the Type Mismatch Error when I enter a figure, and then Backspace to correct it, or on purpose enter a minus sign (not allowed), to simulate the response from the application, to an error from the user, like this one:

If ((CheckBox6 = True) And (IsEmpty(TextBox8.Value)) Or _
(Not IsNumeric(TextBox8.Value)) Or (TextBox8.Value < 0) Or (TextBox8 > 100)) Then
Msg = MsgBox(vbCrLf & vbTab & _.....

End If


In another instance - of a apparently identical code - I receive the Msg I expected when I wrote the code:

If ((TextBox2.Value <= 0) Or (Not IsNumeric(TextBox2.Value)) Or _
(TextBox2.Value > Concession)) Then
Msg = MsgBox(vbCrLf & vbTab & _

End If


What might be the cause that in some instances the error is trapped and in other simingly identical it isn't??

THANKS IN ADVANCE


Oscar Alberto
 
Hi Oscar,

I don't know what you want your check to do (how does Checkbox6 control what you want?) but I don't think any of the parentheses you have at present affect the evaluation of the condition and I suspect your Ands and Ors are not combining in the way you expect. Try breaking it down into a few separate conditions. I THINK this is equivalent to what you have at the moment:

Code:
If CheckBox6 = True And IsEmpty(TextBox8.Value) Then
Code:
' Msg = ...
Code:
ElseIf Not IsNumeric(TextBox8.Value) Then
Code:
' Msg = ...
Code:
ElseIf TextBox8.Value < 0 Then
Code:
' Msg = ...
Code:
ElseIf TextBox8.Value > 100 Then
Code:
' Msg = ...
Code:
Else
Code:
' No Msg - All is OK?
Code:
End If

Is that what you want?

Enjoy,
Tony
 
The Checkboxes are for the user to choose from two or three options, like liquid, solid or gaseous. Then the textboxes provide an area to input say, temperature, but it cannot be negative (-), or more than a certain maximum number, it cannot contain symbols, or be empty.

I would really like to have the user input tested and if wrong prompted to the user with a single MsgBox, that's why I used the parenthesis.

Maybe I could reduce the number of OR's provided I had a way to coerce a number with a symbol into a number, foregoing the symbol. Could you help me with this?
 
As an alternative approach, rather than telling the user he is wrong, prevent him from making the error in the first place.

Here is the base case allowing the user to only enter numbers from 1 to 100. You can combine the logic with additional tests for your check boxes etc.:
[blue]
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  Select Case Chr$(KeyAscii)
    Case 0 To 9
      If Len(TextBox1.Text) = 0 And KeyAscii = 48 Then
        KeyAscii = 0
      End If
      If Len(TextBox1.Text) >= 2 Then
        If TextBox1.Text <> &quot;10&quot; Or KeyAscii <> 48 Then
          KeyAscii = 0
        End If
      End If
    Case Else: KeyAscii = 0
  End Select
[/color]

You could also consider the use of a SpinButton that you can set the Min and Max properties according to which check boxes are checked.

BTW, from your description it sounds like you should be using radio buttons instead of checkboxes. Excel calls them OptionButtons. (Unless your user has found a substance that can be solid, liquid and gaseous at the same time.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top