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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to control textbox input (numeric) while user types in data 7

Status
Not open for further replies.

tjbvo

Technical User
Nov 8, 2002
4
0
0
ZA
Greetings

I would like to know how to code a TEXTBOX, to do the following ; -

1. Limit input by user to numeric data only.

2. Validate the input, while the user is entering data.

3. I have 12 textboxes, I need code to activate a command button, once all textboxes have data entered into them.

P.S.

Each textbox has it's own txtname. (No array).

Thankyou

tjbvo (Nexes)
 
A quick search turns up thread222-608927 along with many threads ont he subject.
 
1. Not sure what you mean - will some users be able to input text data and others numeric data?

2. In each textbox's Change event, have a little code that goes like:
Code:
If Not IsNumeric(Text1.Text) Then
   MsgBox "Numeric data only, please!"
   Text1.Text = ""
End If

3. I would make the command button active by default, then add some code in the Click event of the button to check that each text box has a value and that the value is valid, rather than disabling the command button and write some more complicated code to enable the button when all text boxes are populated.

HTH,
jp
 
You can also use something like this in the keypress event,

Private Sub Text1_KeyPress(KeyAscii As Integer)
'// Number only.
If Not (KeyAscii > 47 And KeyAscii < 58) Then KeyAscii = 0
End Sub

However this does not prevent someone from pasting in something else. Therfore you will need to use the Change and/or validate event as well, as jpbrassard mentioned.

Thanks and Good Luck!

zemp
 
I forgot to mention that you should leave access for some other keys as well, like a decimal point and the backspace. The following keypress is more complete.

Private Sub Text1_KeyPress(KeyAscii As Integer)
'// Catch values entered from keyboard.
If KeyAscii = 46 Then 'allow one decimal.
If InStr(Text1.Text, &quot;.&quot;) Then KeyAscii = 0
ElseIf Not ((KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8) Then 'allow backspace key.
KeyAscii = 0
End If
End Sub


Thanks and Good Luck!

zemp
 
Not sure if it will do exactly what you want, but I would recommend the Visual BASIC Masked Edit Box control. You can include it into your program by going Program -> Components, then scrolling down to ‘Microsoft Masked Edit Control 6.0 (SP3)’ and selecting the checkmark next to it. One you have done this you will have it in your tool box and can treat it much like a normal textbox with the addition of adding a mask so the user can only enter letters, numbers or a specific combination of both.

A good website on it is at:
Just make sure to distribute the MSMASK32.ocx file with your program.
 
The mask edit box can be a good choice. However sometimes I find it too specific. If I want a number then I need to know how big the number is in order to set the mask. Is it 10, 1000 or 10000. In this case the number of variations makes choosing the mask edit box difficult.

It works great for phone numbers and other set formatting.

Thanks and Good Luck!

zemp
 
One more thing zemp , the prompt character in Mask Edit Boxes make things dificult . If u want to set text , u have to first set mask = &quot;&quot; then set text to desired text.

Vikram
 
Private Sub Text1_Change()
Text1.Text = Replace(Text1.Text, &quot;a&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;b&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;c&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;d&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;e&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;f&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;g&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;h&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;i&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;j&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;k&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;l&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;m&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;n&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;o&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;p&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;q&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;r&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;s&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;t&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;u&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;v&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;w&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;x&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;y&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;z&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;A&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;B&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;C&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;D&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;E&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;F&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;G&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;H&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;I&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;J&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;K&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;L&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;M&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;N&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;O&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;P&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;Q&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;R&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;S&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;T&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;U&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;V&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;W&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;X&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;Y&quot;, &quot;&quot;)
Text1.Text = Replace(Text1.Text, &quot;Z&quot;, &quot;&quot;)
Text1.SelStart = Len(Text1.Text)
End Sub

You can obviously put it into an array and all that, but this is a really simple way to do it, and one I've used for a long time. I think you could also incorporate a for loop and some ascii values to make it a little less...long.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top