Results 1 to 6 of 6
  1. #1
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305

    Only allow certain numbers in textbox


    I have a textbox bound to a number field in a form. I only want the user to be able to put '1', '2' or '3' in the textbox.

    Ideally, if the user put any other number in the field, it'd clear it out and populate a msg box.

    Any suggestions on how to code that besides switching it to a combobox?

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,530
    You could use the following code in the BeforeUpdate event of the textbox:
    Code:
        If Me.ActiveControl <> 1 And Me.ActiveControl <> 2 And Me.ActiveControl And 3 Then        
            MsgBox Me.ActiveControl & " is an invalid number", , "INVALID NUMBER"
            Cancel = True
            Me.Undo
        End If
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Nvm. Solved with validation rule

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,530
    Quote Originally Posted by templeowls View Post
    ...Ideally, if the user put any other number in the field, it'd clear it out and populate a msg box....
    I thought that meant that you wanted the invalid number removed. I don't believe that a validation rule will do that.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    Join Date
    Apr 2017
    Posts
    1,673
    Why not simply to use a combo box instead?

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,016
    Or an Option Group?

    It's almost always better, and saves the user's time, to prevent wrong data from being entered, rather than fixing it after it's been incorrectly entered.

    If space on your Form is limited, you can always arrange an Option Group with the choices available in a straight line, rather than the default 'up-and- down,' configuration...taking up no more space than a Combobox would.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Replies: 2
    Last Post: 02-12-2016, 05:31 AM
  2. Replies: 8
    Last Post: 03-10-2014, 11:47 AM
  3. Replies: 1
    Last Post: 11-29-2011, 08:43 AM
  4. Textbox and numbers
    By GraemeG in forum Forms
    Replies: 1
    Last Post: 03-16-2011, 06:05 AM
  5. Replies: 2
    Last Post: 08-12-2010, 11:21 PM

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