Instr function query...correct query

Instr function query...correct query

Post by Hilt » Sun, 10 Mar 2002 01:31:02



sorry about the first message...

I have a text box and I want to restrict the input to only numbers and
decimal numbers. I want to error handle so that if "." is typed as the
first character, it is changed to 0. However when I type in "." as the
first character, I get a type mismatch error... I do not understand
why... any help is greatly appreciated. Below is the code segment

Dim strMyNum As Currency

If txtCellvalue.Text <> "" Then
  If (txtCellvalue.Text > "100") Or (txtCellvalue.Text < 0) Or _
   InStr(txtCellvalue(Index).Text, ".") = 0 Then
     txtCellvalue.Text = 0
  Else
    strMyNum = CCur(txtCellvalue.Text)
  End If
End If

Private Sub txtCellvalue_KeyPress(KeyAscii As Integer)
' Converts the Ascii number of the key press
    ' and stores in the variable Key.
    Key = Chr$(KeyAscii)

    Select Case Key
    Case "0" To "9"
        'Allow the entry

    ' Allows for use of a decimal point.
    Case "."
        'Allow the entry

    ' Checks to see if there is already a decimal point.
        If InStr(txtCellvalue.Text, ".") And _
         InStr(txtCellvalue.SelText, ".") = 0 Then
            KeyAscii = 0
            Beep
        Else
            'Allow the entry
        End If

    ' Allows for the use of the Backspace key.
    Case Chr$(8)
        'Allow the entry

    ' Disables all other keys.
    Case Else
        KeyAscii = 0
        Beep
    End Select
End Sub

 
 
 

Instr function query...correct query

Post by mark » Wed, 13 Mar 2002 03:12:09



> sorry about the first message...

> I have a text box and I want to restrict the input to only numbers and
> decimal numbers. I want to error handle so that if "." is typed as the
> first character, it is changed to 0. However when I type in "." as the
> first character, I get a type mismatch error... I do not understand
> why... any help is greatly appreciated. Below is the code segment

> Dim strMyNum As Currency

> If txtCellvalue.Text <> "" Then
>   If (txtCellvalue.Text > "100") Or (txtCellvalue.Text < 0) Or _
>    InStr(txtCellvalue(Index).Text, ".") = 0 Then
>      txtCellvalue.Text = 0
>   Else
>     strMyNum = CCur(txtCellvalue.Text)
>   End If
> End If

> Private Sub txtCellvalue_KeyPress(KeyAscii As Integer)
> ' Converts the Ascii number of the key press
>     ' and stores in the variable Key.
>     Key = Chr$(KeyAscii)

>     Select Case Key
>     Case "0" To "9"
>         'Allow the entry

>     ' Allows for use of a decimal point.
>     Case "."
>         'Allow the entry

>     ' Checks to see if there is already a decimal point.
>         If InStr(txtCellvalue.Text, ".") And _
>          InStr(txtCellvalue.SelText, ".") = 0 Then
>             KeyAscii = 0
>             Beep
>         Else
>             'Allow the entry
>         End If

>     ' Allows for the use of the Backspace key.
>     Case Chr$(8)
>         'Allow the entry

>     ' Disables all other keys.
>     Case Else
>         KeyAscii = 0
>         Beep
>     End Select
> End Sub

I would run this in debug mode to find exactly what statement is causing the error.