Results 1 to 6 of 6
  1. #1
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Limiting No. of Char in Text Box (Access 2002)

    Is there any way to limit the number of characters that can be entered into an unbound text box?

    I have set up a form for entering three address lines in three text boxes. I then created a report that generates labels with a name selected from a table and the same set of three address lines in each label. I get the address lines directly from the unbound text boxes in the form. However, I notice that if the length of any of the address lines exceeds the normal column width on the labels report, Access (without warning) changes the page layout to a single colume with 1" margins instead of the three column setup for the labels. Thus, I need to limit the length of the address lines entered on the form, but I don't find any property that specifies the max length of the string being entered.
    I find it very frustrating that Access will change a report layout without permission, as I then have to totally re-layout the report form.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Limiting No. of Char in Text Box (Access 2002)

    Try something like this. You will have to repeat this for each text box:

    Private Sub txtSomething_KeyPress(KeyAscii As Integer)
    If Len(txtSomething.Text) >= 20 And Not(KeyAscii = vbKeyBack) Then
    KeyAscii = 0
    End If
    End Sub

    Replace txtSomething with the name of the text box and 20 by the maximum length you want.

    You can also limit the length of the text to the width of the text box: see HOW TO: Limit the Number of Characters to the Size of the Text Box in Access 2002.

  3. #3
    Gold Lounger
    Join Date
    Jun 2001
    Location
    Crystal Beach, FL, Florida, USA
    Posts
    3,436
    Thanks
    1
    Thanked 34 Times in 34 Posts

    Re: Limiting No. of Char in Text Box (Access 2002)

    You could use a input mask like this on each field:
    CCCCCCCCCCCCCCCCCCCC
    Mark Liquorman
    See my website for Tips & Downloads and for my Liquorman Utilities.

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Limiting No. of Char in Text Box (Access 2002)

    Another option for limiting the number of characters entered in a textbox is a technique described in the Access Developer's Handbook (ADH), Vol 1, which entails the use of the Windows SendMessage API function to send a EM_SETLIMITTEXT message to the active control (textbox). As noted in ADH, most Windows messages of this type do not work with Access controls, and can cause serious errors, but for whatever reason the Access textbox does respond to this message. (Note that in Access, unlike VB, a control is an actual "window" only when it has focus; you can then use GetFocus API function to get the textbox's hwnd. At any other time an Access textbox does not have a hwnd property.)

    The attached demo database has a form (Demo Form) that illustrates use of this technique. The form (based on Northwind Employees table, modified somewhat) has both bound and unbound fields. The Max Length textbox displays the maximum text length for the active control (updated whenever a textbox gets focus). The Field Size textbox displays the field size (as defined in source table) for current field (if text - if not text, or textbox is unbound, other info is displayed). DAO methods are used to determine source table & field info. The Max Length for bound fields is equal to the Field Size, unless otherwise specified. The textbox Tag property is used to specify Max Field length for both bound and unbound fields, where applicable (does not apply to numerical, date, etc fields, or unbound textboxes whose ControlSource is set to an expression; the code tests for these conditions & sets Max Length to Null). In most cases you'd set Max Length only for Memo fields or for unbound textboxes with no ControlSource specified. There are also labels alongside Max Length textbox that display the length of text in active control, and remaining text that can be entered, based on Max Length setting. The demo shows an example of a case where you may want to limit the text for bound field to number less than field size; the PostalCode field Max Length is set to 5 if Country is USA, 7 if UK, 10 if anything else.

    See attached file (A2K format) if interested to see how this works. NOTE: This works correctly in Access 2K on WIN XP system. Do not presently have other versions of Access installed to test. If testing the API functions used in demo, test with a BACKUP copy of your database - using the API as is done in this case is not officially supported and may not work correctly in later version of Access.

    HTH
    Attached Files Attached Files

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Limiting No. of Char in Text Box (Access 2002)

    Mark,

    Your demo works fine in Access 2002, the version used by Chuck.

  6. #6
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Limiting No. of Char in Text Box (Access 2002)

    Thanks for all the suggestion, but the simplest seems to be the input mask -- at least that's working OK so far.

    I find it truly amazing how wonderfully you Loungers respond to those of us who are still learning.

Posting Permissions

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