alter keyascii in VBA

January 15, 2010 at 01:41:22
Specs: Windows XP, athlon64x2 6000+/4GB
I have several text boxes where I want to restrict the input to be numbers only, so I've written a function to avoid repeating code, but I can't get it to work. The text boxes have code like this
Private Sub txtpost_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
KeyAscii = numbersonly(KeyAscii)
End Sub


Function numbersonly(key As MSForms.ReturnInteger) As MSForms.ReturnInteger
If Not (key > 47 And key < 58) Then
key = Null
End If
numbersonly = key
End Function

but on a keypress event I get the error
"object variable ro with block variable not set"
on the line
numbersonly = key

What am I doing wrong?

See More: alter keyascii in VBA

Report •

January 15, 2010 at 02:46:46
Sorry that's meant to be
"object variable or with block variable not set"

Report •

January 15, 2010 at 04:56:00
It is key = Null that is doing it.

Setting an object variable to Null and then trying to use it is a sure fore way to get that error.

Try this:

If (key > 47 And key < 58) Then
numbersonly = key
numbersonly = 0
end if
End Function


Report •
Related Solutions

Ask Question