|
-
Dec 11th, 2005, 10:26 AM
#1
Thread Starter
New Member
[RESOLVED] KeyAscii problem
Hi all, Im very very new to this as you all can probably tell ! I am currently self learning for a hobby and I am referring to a book by Peter Wright called - Beggining Visual Basic 6. Anyow after a nice simple start I have come to an abrupt stop on hitting page 93 ! The problem is with code concerning a simple keypress command, the code I have is
Code:
Private Sub cmdOK_Click()
MsgBox "Input Accepted"
End Sub
Private Sub txtage_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") And KeyAscii > Asc("9") Then KeyAscii = 0
End Sub
Private Sub txtforname_KeyPress(KeyAscii As Integer)
If KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then KeyAscii = 0
End Sub
The code in itself seems self explanitory to me. it should have the effect of only allowing alphabetical numbers in txtforename, and numbers in in txtage by using conditions on the ascii code.
When in runtime the txtforename works A1 - only showing the alphabetical ascii characters while ascii code for the numbers are returned to 0 (is this what people refered to as nulled ?) But in the txtage it allows both text and numbers to be inputted and shown, when only numbers should be visable ? I have re-typed in the code a couple of times but still get the same results, can some one explain what is happening and wrong, thank you and TIA, please forgive my simpleness !
-
Dec 11th, 2005, 10:39 AM
#2
Re: KeyAscii problem
If you want to allow/disallow numeric characters only then here is a quicky for you"
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
'allow numeric and backspace characters only
If Not IsNumeric(Chr(KeyAscii)) And Not KeyAscii = 8 Then
KeyAscii = 0
End If
End Sub
Last edited by RhinoBull; Dec 11th, 2005 at 10:42 AM.
-
Dec 11th, 2005, 10:56 AM
#3
Thread Starter
New Member
Re: KeyAscii problem
Thanks RhinoBull, that has worked fine. If I was to reverse it to allow text only how would the code look ? Could I simply take the 'not' out of the equation ?
Code:
If IsNumeric(Chr(KeyAscii)) And KeyAscii = 8 Then KeyAscii = 0
Also - any reason for the original code I submitted not workin
Code:
"If KeyAscii < Asc("0") And KeyAscii > Asc("9") Then KeyAscii = 0"
is the book wrong ? as I say I am very new to all this ! Thanks
-
Dec 11th, 2005, 11:15 AM
#4
Re: KeyAscii problem
I've just noticed that's you are the newcommer so Welcome to our world!
To disallow numeric input use the following sample:
VB Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
End If
End Sub
NOTE: both samples are simple base code though. It takes much more effort that those few lines to develop fully functional NUMERIC textbox.
Cheers.
-
Dec 11th, 2005, 11:21 AM
#5
Addicted Member
Re: KeyAscii problem
You can also use the following to ensure that only numberes can be entered:
Code:
Private Sub Ans_KeyPress(KeyAscii As Integer)
' Where "Ans" is the name of your text box
If (KeyAscii = 8) Or (KeyAscii = 9) Then Exit Sub
If (KeyAscii < 48) Or (KeyAscii > 57) Then
KeyAscii = 0
End If
End Sub
RB's method is actually easier, it's just that i've always known this way. The nice thing about working with ASCII values is that you can actually stop and allow specific characters, not just groups
Good ASCII table ("Dec" is the number you're looking for)
-
Dec 11th, 2005, 11:21 AM
#6
Thread Starter
New Member
Re: KeyAscii problem
Thanks for the info there, and its nice to be a part of your world too I feel I can relate to the code in what you are doing, I am equally sure that I will have many questions to ask as I progress too
-
Dec 11th, 2005, 11:28 AM
#7
Re: KeyAscii problem
 Originally Posted by P6cxy
... I am equally sure that I will have many questions to ask as I progress too 
That's what we are here for - help people like yourself that willing to learn.
Also, since you're new to programming then it might be beneficial for you to start with VB.Net instead.
Good luck.
-
Dec 11th, 2005, 11:42 AM
#8
Thread Starter
New Member
Re: KeyAscii problem
I did think about going VB.Net but I thought IMHO that starting here and working up was the way - learning to walk before I can run as one would say. Am I incorrect in this way of thinking then? ive heard about the .net before - but I am unsure what .net platform is fully about to be truthful
-
Dec 11th, 2005, 11:45 AM
#9
Addicted Member
Re: KeyAscii problem
.NET is MS' new development platform, it's all new and fancy and shiny and stuff, supposedly bettwe than VB6. But I learnt most of my VB during college, where they're too cheapskate to buy .NET, so I'm stuck with VB6.. I quite like it though, TBH 
But, for future proofing and better job oppertunities, I guess you should learn .NET... (Bear in mind that most professional programs are written in C++, and some people say that learning VB before learning C++ actually makes it HARDER to learn C++ (because you have to unlearn the commands and formatting and such), so if you intend to learn C++ in the future, be warned
-
Dec 11th, 2005, 12:26 PM
#10
Re: KeyAscii problem
 Originally Posted by P6cxy
Thanks RhinoBull, that has worked fine. If I was to reverse it to allow text only how would the code look ? Could I simply take the 'not' out of the equation ?
Code:
If IsNumeric(Chr(KeyAscii)) And KeyAscii = 8 Then KeyAscii = 0
Also - any reason for the original code I submitted not workin
Code:
"If KeyAscii < Asc("0") And KeyAscii > Asc("9") Then KeyAscii = 0"
is the book wrong ? as I say I am very new to all this ! Thanks
Yeah! I think it is wrong. You should use OR instead of AND.
VB Code:
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then KeyAscii = 0
Try it.
-
Dec 11th, 2005, 12:31 PM
#11
Addicted Member
Re: KeyAscii problem
Haha yeah, with the book's example it's looking for the value to simultanously be a character less than 0 but greater than 9
You don't need "Asc" though, do you? Wouldn't
Code:
If (KeyAscii < 0) or (KeyAscii > 9) then
KeyAscii = 0
end if
work?
-
Dec 11th, 2005, 01:38 PM
#12
Thread Starter
New Member
Re: KeyAscii problem
Thanks for all the feedback in this thread, Replacing the 'and' with 'or' has indeed corrected the original error. Also thanks for the new work-arounds and different ideas that you have suggested. Im sure I will have many more questions as I go along and im pleased to find a great friendly forum I will now consider this thread as resolved - once again, thanks to all your input and time
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|