Results 1 to 12 of 12

Thread: [RESOLVED] KeyAscii problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Location
    Lincoln UK
    Posts
    5

    Resolved [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 !

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: KeyAscii problem

    If you want to allow/disallow numeric characters only then here is a quicky for you"
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     'allow numeric and backspace characters only
    3.     If Not IsNumeric(Chr(KeyAscii)) And Not KeyAscii = 8 Then
    4.         KeyAscii = 0
    5.     End If
    6. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Location
    Lincoln UK
    Posts
    5

    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

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     If IsNumeric(Chr(KeyAscii)) Then
    3.         KeyAscii = 0
    4.     End If
    5. 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.

  5. #5
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    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)

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Location
    Lincoln UK
    Posts
    5

    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

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: KeyAscii problem

    Quote 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.

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Location
    Lincoln UK
    Posts
    5

    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

  9. #9
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    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

  10. #10
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    Re: KeyAscii problem

    Quote 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:
    1. If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then KeyAscii = 0
    Try it.
    CS

  11. #11
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    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?

  12. #12

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Location
    Lincoln UK
    Posts
    5

    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
  •  



Click Here to Expand Forum to Full Width