Results 1 to 12 of 12
  1. #1
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402

    keypress question

    Hi Guys

    sorry if this has been asked before

    i have loads of forms that have text boxes that are for collecting numbers, I.E phone numbers etc

    these are set as text in the database feilds

    i have added this code on the keypress event of some of the form fields and it works really well, this prevent entering text, only numbers

    Code:
       
        Select Case KeyAscii
            Case 8, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 127
            Case Else
                KeyAscii = 0
        End Select

    what i would like to do to stop me entering the same code loads of time, is call this from a module

    something like

    Code:
    ' ----------------------------------------------------------------
    ' Procedure Name: NumbersOnly
    ' Purpose: Stop Entering letters
    ' Procedure Kind: Function
    ' Procedure Access: Public
    ' Parameter KeyAscii (Integer): This Is The ASCII Numbers For The Numberic, Number, Delete, Backspace And Arrow keys Key
    
    ' Date: 05/06/2018
    ' ----------------------------------------------------------------
    Function NumbersOnly(KeyAscii As Integer)
        On Error GoTo NumbersOnly_Error
        Select Case KeyAscii
            Case 8, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 127
            Case Else
                KeyAscii = 0
        End Select
        
        On Error GoTo 0
        Exit Function
    NumbersOnly_Error:
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure NumbersOnly, line " & Erl & "."
    End Function
    what i cant seem to figure out is the code i would put in the keypress event on the forms text boxes

    if i put

    Code:
    call NumbersOnly
    i get "Compile Error Argument not optional"

    any help would be fantastic

    Steve

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Try

    KeyAscii = NumbersOnly(KeyAscii)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi pbaldy

    thanks for the reply

    i tried that code on the keypress event of the textbox, but no data is entered, no numbers or letters
    but i get no errors

    Steve

  4. #4
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Can you attach the db here to play with?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Database1.zipHi

    i have zipped the DB,

    the form im testing is called frmDashBoardIncomingPayment
    on this form is a payment feild, i have entered the code there

    the module is in ModUtilities

    many thanks for taking a look

  6. #6
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I should have noticed you don't set the function return value. Try

    Code:
        Select Case KeyAscii        Case 8, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 127
                'MsgBox KeyAscii
                NumbersOnly = KeyAscii
            Case Else
    
    
                NumbersOnly = 0
        End Select
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    Oh, and this would be simpler:

    Case 8, 48 To 57, 127

    and you may want to allow a decimal point.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    A couple of suggestions - tested & working for me:

    a) Use KeyCode not KeyAscii

    Code:
    Function NumbersOnly(KeyCode As Integer)    
    On Error GoTo NumbersOnly_Error
        Select Case KeyCode
            Case 8, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 127
            Case Else
                KeyCode = 0
        End Select
        
        On Error GoTo 0
        Exit Function
    NumbersOnly_Error:
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure NumbersOnly, line " & Erl & "."
    End Function
    b) In your form, use the KeyDown event instead of KeyPress
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        NumbersOnly KeyCode
         'or use Call NumbersOnly(KeyCode)
    End Sub
    c) make sure you set Key Preview = Yes in the form properties
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  9. #9
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi guys

    many many thanks for your help
    I will amend the code

    im out the office at the moment, I will apply the changes first thing when I’m back in

    many thanks
    steve

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    You're welcome
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  11. #11
    sdel_nevo is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    hi both

    many thanks again, i have amended my code as per your suggestions
    works really well

    thanks so much again
    steve

  12. #12
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Excellent ... but you might want to add 45 (minus sign) & 46 (decimal point) to your list.

    One point I meant to ask originally.
    Why not just use a number datatype for your bound textbox?
    Or if its unbound what's wrong with just setting the textbox to number format
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Please reply to this thread with any new information or opinions.

Similar Threads

  1. When press tab, ignore keypress event
    By jlgray0127 in forum Forms
    Replies: 4
    Last Post: 08-16-2016, 10:24 AM
  2. KeyPress event please help
    By braveheart_bh in forum Forms
    Replies: 1
    Last Post: 08-25-2015, 12:00 AM
  3. A KeyPress Event and UNICODE character set
    By Marin in forum Programming
    Replies: 0
    Last Post: 02-27-2013, 08:32 PM
  4. Replies: 4
    Last Post: 08-25-2012, 07:19 PM
  5. Replies: 1
    Last Post: 03-31-2011, 02:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums