Numeric data on forms

  • Thread starter Thread starter L. Fuernes
  • Start date Start date
L

L. Fuernes

Hi,

I am designing a form where I have some textboxes where I
want the user to enter numeric value (with or without
decimals).

since the textbox does not have properties to restrict
input data to only valid numbers, how do I do the
following:

(a) efficiently ensure that the data entered is a valid
number
(b) save the entered value (which is in text format) to a
spreadsheet cell in the format of a number

TIA
 
Hi

create a macro for the keypress event of your text box
like

private sub textbox1_KeyPress(KeyAscii as integer)
KeyAscii = textNumericOnly(KeyAscii)
end sub


Function textNumericOnly(KeyAscii as integer)

if KeyAscii = 8 or KeyAscii = 9 then
'TAB key or backspace
textNumericOnly = KeyAscii
exit function
end if

if KeyAscii < 48 or KeyAscii > 57 then
textNumericOnly = 0
exit function
end if
textNumericOnly = KeyAscii
End Function


Richard Daniels
 
I created a form with text box called TextBox1. In it's
key press event I put in the call to the function as you
have suggested. I also did a copy-paste of your function
code hereunder. And then I executed the form.

I am getting a compile error as follows:

ByRef argument type mismatch.

The line highlighted is KeyAscii = textNumericOnly
(KeyAscii)

The word highlighted is "(KeyAscii)"

Any idea what is going wrong?

TIA
 
No need to call a sub. Just put it in the event:

Private Sub TextBox1_KeyPress(ByVal KeyAscii _
As MSForms.ReturnInteger)
If KeyAscii = 8 Or KeyAscii = 9 Then
'TAB key or backspace
Exit Sub
End If

If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If
End Sub
 
Back
Top