Hi everybody!
I'm a VB-beginner and nee help.
How can I define an Input-Field, where the User can only write numbers and noch letters or other Signs.
Ps.: Sorry for my bad english.. I'm from Germany ;)
Printable View
Hi everybody!
I'm a VB-beginner and nee help.
How can I define an Input-Field, where the User can only write numbers and noch letters or other Signs.
Ps.: Sorry for my bad english.. I'm from Germany ;)
Are you talking about an input box or a textbox on a form? If you're referring to a textbox, just put the property of the textbox values to numerical (you can do so in the properties windows, when you select the textbox).
or you can validate it using Isnumeric() function. :P
A Textbox.. where can I do that!?!?!?!?
Well, when you build your form you can insert a textbox, by clicking on the left panel on textbox and then dragging it over your form. Is this what you are asking ?!?
VBKinght yep you're right, IsNumerical() does work fine, you should however then set the KeyPreview = True on the form itself and use the function to filter the keyboard input, so that it just doesn't print other text ;0
Hey! guy, if you're talking about the textbox control, I've another 2 method as show below. The first is a bit more complicated as compare to the second.
Code:'Method 1
Option Explicit
Private Declare Function GetWindowLong& Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long)
Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Private Const ES_NUMBER = &H2000&
Private Const GWL_STYLE = (-16)
Private Sub Text1_Change()
Dim tmpValue&
Dim fAlignment&
Dim ret&
fAlignment& = ES_NUMBER
tmpValue& = GetWindowLong&(Text1.hwnd, GWL_STYLE)
ret& = SetWindowLong&(Text1.hwnd, GWL_STYLE, tmpValue& Or fAlignment&)
Text1.Refresh
End Sub
Code:'Method 2
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 47 Or KeyAscii > 58) And KeyAscii <> 8 Then KeyAscii = 0
End Sub
Hi Chris,
yes your 2nd option is the one I've suggested here below, still thanks for the code it's true - it does make things more clear to persons ;)
I still believe you have to set the From KeyPreview property to True for this to work properly.
Kind regards,
W.
Hi! Wildcat, I juz execute the code without set the KeyPreview properties to true and yet it still return the expected result too. :)
Quote:
Originally posted by wildcat_2000
Hi Chris,
yes your 2nd option is the one I've suggested here below, still thanks for the code it's true - it does make things more clear to persons ;)
I still believe you have to set the From KeyPreview property to True for this to work properly.
Kind regards,
W.
Woops! Does it? I'll better shut up then hehe ;)
Cheers,
W.
what about a mask input box? you can specify the location of your numbers and special characters using a mask input box.
It's called a Masked Edit Control, referred to in Project/Components/Microsoft Masked Edit Control, and it it's own properties as MaskedBox1. You can set a mask, and the only thing to remember is that when you clear a maskeditbox you must first clear the mask then clear the text and then reset the mask. With that in mind it makes a very nice option.
Will you think using MaskEdit control in this case is much more complecated?
No..you just have to know the little quirk I mentioned above.
MaskEdBox1.Mask = "##########" 'however long your field
then all you can enter is numbers and backspace. No other code required.
To clear the text.
MaskEdBox1.Mask = ""
MaskEdBox1.Text = ""
MaskEdBox1.Mask = "##########" 'however long your field
'if you wanted numbers seperated by / then
MaskEdBox1.Mask = "###/###/####" 'as per phone number
'then the / would remain in the box for guidance
You can add this code on the OnKeyPress event of any TextBox:
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> vbKeyBack _
And Chr(KeyAscii) <> "." Then
KeyAscii = 0
End If
fnajar that would disable keys like enter and tab. use this rather:
If you want to enable spaces change 31 to 32Code:If Not IsNumeric(Chr(KeyAscii)) And KeyAscii > 31 Then
KeyAscii = 0
End If