![]() | ![]() |
|
Welcome to the HEXUS.community discussion forums forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and other features. By joining our free community you will have access to post topics, respond to polls and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! |
| |||||||
| Software and web development Databases, graphics, programming, scripting and web development. |
![]() |
| | LinkBack | Thread Tools |
| | #1 (permalink) |
| Flak Monkey! Join Date: Jul 2003 Location: UK - South Wales
Posts: 1,762
Thanks: 36
Thanked 17 Times in 15 Posts
| Validating text entered into a VB text box Ok im makeing a program which asks a question and the user has to enter an answer into a txt box. When they do this an OK button appears and when that is clicked it tells them if their question is right or wrong. The problem i have is answers can only be numbers and not letters. How do i get the "ok" button to validate the answer in the text box. If a number is entered the program just gives feed back to the answer . If any thing other than a number is entered it displays a message saying "only numbers can be entered" for example. How do i do this? thanks for anyhelp. Edit: I know how to do all the other things mentioned. Its just that validation thing i cant work out |
| | |
| | #3 (permalink) |
| Member Join Date: Aug 2003 Location: Wimbledon
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
| In this situation it is better to prevent the user from entering letters in the text box rather than wait til they press the OK button. What you need to do is handle the KeyPress event and check which key was pressed. If you set the keycode to 0 they the KeyPress is effectively cancelled. In the code below all key presses are cancelled apart from 0 to 9... Code: Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("0") To Asc("9")
' Do nothing
Case Else
KeyAscii = 0
End Select
End Sub
|
| | |
| | #4 (permalink) |
| I live in a giant bucket. Join Date: Jul 2003 Location: South Africa
Posts: 617
Thanks: 0
Thanked 0 Times in 0 Posts
| Note that Mart's method will not work if the user pastes text using the right-click pop-up menu. Note also that Mart's method prevents the user from pressing backspace (ASCII char 8, I think). Code: Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("0") To Asc("9"), 8 'Allow backspace
' Do nothing
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Text1_Change()
Dim i as Long, sTemp as String
For i = 1 To Len(Text1)
If Instr(1, "0123456789", Mid$(Text1, i, 1)) > 0 Then sTemp = sTemp & Mid$(Text1, i, 1)
Next
Text1 = sTemp
End Sub
|
| | |
| | #6 (permalink) |
| I live in a giant bucket. Join Date: Jul 2003 Location: South Africa
Posts: 617
Thanks: 0
Thanked 0 Times in 0 Posts
| Fairly nice code you got there for a non-VB programmer. ![]() Myself, I'm currently employed coding VB. Though I know several other languages, a smidgeon of VC++ too. |
| | |
| | #8 (permalink) |
| Flak Monkey! Join Date: Jul 2003 Location: UK - South Wales
Posts: 1,762
Thanks: 36
Thanked 17 Times in 15 Posts
| Re opening an old post ![]() as i said above thanks for all your help. the code eldren provided worked best for my situation however i now need prevent the user from entering zero first then enter numbers after it e.g. 0333. However they are allowed to enter zeros after a first number is entered whcih is not zero e.g. 10350. How is this done? took 0 away from the list of allowed character but realized then that no 0 could be entered which is where i got stuck. lol any ideas? |
| | |
| | #9 (permalink) |
| I live in a giant bucket. Join Date: Jul 2003 Location: South Africa
Posts: 617
Thanks: 0
Thanked 0 Times in 0 Posts
| First, declare a module-level variable that will tell us whether there is a number in the textbox or not. For example: Private blnAllowZero As Boolean Now, in the Keypress sub, change 'Asc("0") To Asc("9")' to 'Asc("1") To Asc("9")'. Next, in the same Select block, add a case for zero itself, 'Case Asc("0")'. Inside that, put: If blnAllowZero = False Then KeyAscii = 0 'Block 0 if not allowed Lastly, in the Change sub, add the following at the end, just before End Sub: blnAllowZero = (Len(Text1) > 0) Hope you come right. :> |
| | |
| | #10 (permalink) |
| Registered User Join Date: Jun 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
| Would this work You could try this it basicly makes your textbox an interger Code: Private Sub Button_Click()
Dim number As Integer
'here the varible number is dimed as a interger (number)
On Error Resume Next
'if it errors which it will if anything but a number is entered_
resume next
number = Text1.Text
'text is number which is a interger numbers only
If Err <> 0 Then MsgBox ("number only") Else
'if it errors then message else do what you want it to do
End Sub
Last edited by hell_flame85; 09-06-2006 at 10:17 AM.. |
| | |
![]() |
| Breadcrumb | ||||||
| ||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |