Consulting

Results 1 to 12 of 12

Thread: ASCii code to allow a space

  1. #1
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location

    ASCii code to allow a space

    How do I amend the following code to allow a space to be entered and not just numbers?
    [vba]Private Sub txtMobile_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
    Case Asc("0") To Asc("9")
    Case Asc("-")
    If InStr(1, Me.txtMobile.Text, "-") > 0 Or Me.txtMobile.SelStart > 0 Then
    KeyAscii = 0
    End If
    Case Asc(".")
    If InStr(1, Me.txtMobile.Text, ".") > 0 Then
    KeyAscii = 0
    End If
    Case Else
    KeyAscii = 0
    End Select
    End Sub[/vba]

  2. #2
    Case Asc(" ")
    2+2=9 ... (My Arithmetic Is Mental)

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Don't know how you want to handle a space

    [vba]

    Private Sub txtMobile_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
    Case Asc("0") To Asc("9")
    Case Asc(" ")
    Case Asc("-")
    If InStr(1, Me.txtMobile.Text, "-") > 0 Or Me.txtMobile.SelStart > 0 Then
    KeyAscii = 0
    End If
    Case Asc(".")
    If InStr(1, Me.txtMobile.Text, ".") > 0 Then
    KeyAscii = 0
    End If
    Case Else
    KeyAscii = 0
    End Select
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    I want to allow the user to be able to input a number such as:

    01234 567890

  5. #5
    A Brit on the east coast USA.
    Your original code does not prevent you from doing that.
    The handling routine around it may though.
    You would need to post the handling routine as well so that we can see.
    (Sorry but maybe my first reply was too acute.)
    2+2=9 ... (My Arithmetic Is Mental)

  6. #6
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    Sorry, I dont know what you mean about the handling routine
    I found that bit of code which sits on a user form and works when the text box is amended and is great at only allowing the user to input numbers 0-9
    I just wanted a bit of code that allows the user to input 0-9 and a space (like a phone number)

  7. #7
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Did you try the code xld provided you?
    Regards,
    Rory

  8. #8
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    No
    I had totally missed that post ! - must be going screen blind - time to stop for today

    That works fine

  9. #9
    Wow ! and it was SO different to mine !!!!
    2+2=9 ... (My Arithmetic Is Mental)

  10. #10
    VBAX Tutor
    Joined
    Dec 2006
    Posts
    271
    Location
    Sorry
    I did try something like that but must have done asc("")

  11. #11
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    And I completely missed yours! (Does that make you an unremarkedhelicopter? )

  12. #12
    Story of my life ... don't ask me to make tea !!!!
    2+2=9 ... (My Arithmetic Is Mental)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •