Results 1 to 4 of 4
  1. #1
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152

    SOLVED: Limiting textbox to number issue

    Hello.

    I have the following code on Keypress of a textbox. to limit the repsonse to just numbers. Which works great!


    However I need it to only allow four digits and be able to use the backspace key to delete.
    But it allows any length of number and also wont allow the backspace key to delete.
    Any help please! Thanks.

    Code:
    Private Sub txtEntranceDoorsFlatsRenewYear_KeyPress(KeyAscii As Integer)
    Const Number$ = "0123456789" ' only allow these characters
    If KeyAscii <> 4 Then
       If InStr(Number$, Chr(KeyAscii)) = 0 Then
          KeyAscii = 0
          Exit Sub
       End If
    End If
    End Sub
    Last edited by GraemeG; 04-03-2011 at 04:33 PM.

  2. #2
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152
    RESOLVED!
    Changed to bound table field length then used the following code:
    Code:
    Private Sub txtEntranceDoorsFlatsRenewYear_KeyPress(KeyAscii As Integer)
        'If a backspace (ASCII-8) or a tab (ASCII-9) was
        ' entered, allow it unconditionally.
        If (KeyAscii = 8) Or (KeyAscii = 9) Then Exit Sub
        If IsNumeric(Chr(KeyAscii)) <> True Then KeyAscii = 0
    End Sub

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    What about length 4?

  4. #4
    GraemeG is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Feb 2011
    Posts
    152
    Quote Originally Posted by orange View Post
    What about length 4?
    I have set the length in the field from the bound table and then used an IF after update to restrict an invalid year i.e < than current year.
    That has worked for me.

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

Similar Threads

  1. limiting number?
    By vespid in forum Database Design
    Replies: 1
    Last Post: 01-10-2011, 10:34 PM
  2. Getting #Number ! on addition of 5 textbox
    By Grooz13 in forum Forms
    Replies: 0
    Last Post: 08-18-2010, 07:49 AM
  3. Replies: 2
    Last Post: 08-09-2010, 08:13 AM
  4. Report TextBox printing ID Number, not text
    By ZipDoc in forum Reports
    Replies: 8
    Last Post: 02-01-2010, 12:30 PM
  5. Limiting access to a Form
    By huskies in forum Forms
    Replies: 0
    Last Post: 12-02-2008, 09:21 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