Results 1 to 8 of 8
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    Text Box Properties restrict to 255 characters

    I'm sure this is simple...



    how do I restrict a form text box to 255 characters only (so the user can only enter up to that amount)

    is there a property?

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The max number of characters is determined by the field size in the table design.

    Text type fields have a max of 255 characters. Memo type field allow 63,999 characters (IIRC).

    I remember seeing some code to limit the numbers of characters in a text box control.... but it's been a while. Easier to set the field size for a text type field in the table design

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Simplest is probably to set the limit in the table/field the textbox is based on and let Access do the work. Alternatively you can probably use the before update event or the keypress event to test it. For example from a quick test:

    Code:
    Private Sub Text27_KeyPress(KeyAscii As Integer)
      If Len(Me.Text27.Text) > 5 Then
        KeyAscii = 0
      End If
    End Sub
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Sorry Steve, your post wasn't there when I started typing. Testing took too long!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Not a problem, Paul.
    I was thinking of code in the change event that would count characters as they were typed.... can't find that durn code
    The code you posted is probably shorter.

  6. #6
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I used

    Code:
    Private Sub txtEnterMemo_Change()
    If Len(Me.txtEnterMemo.Text) <= 225 Then
    
    
    ElseIf Len(Me.txtEnterMemo.Text) < 240 Then
    Me.txtEnterMemo = Left(Me.txtEnterMemo, 225)
    Else
    MsgBox "Only 255-digits allowed."
    SendKeys Chr(8), True
    End If
    End Sub
    however the elseif does not work (I was hoping that if the staff pasted in over 240 it would trim automatically but my code is wrong...

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    How about this:
    Code:
    Private Sub txtEnterMemo_Change()
        Const MaxChars As Integer = 225
    
        If Len(Me.txtEnterMemo.Text) > MaxChars Then
            MsgBox "Only " & MaxChars & " characters allowed." & vbNewLine & vbNewLine & "Trimming text to " & MaxChars & " characters"
            Me.txtEnterMemo = Left(Me.txtEnterMemo.Text, MaxChars)
        End If
    
        MsgBox "Changed"
    
    End Sub
    If you want more or less characters, change the constant "MaxChars" to the number you want.

  8. #8
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I might actually build something that chops the string up 255 characters at a time, issuing a command each time and then moving to the next 255 until all have been saved into the database. I have some staff copying and pasting into the field and if it's over 255 I'd like it to do what I described.

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

Similar Threads

  1. Text box with more than 255 characters
    By ssissons in forum Forms
    Replies: 6
    Last Post: 07-25-2014, 08:32 AM
  2. Change properties of a text box
    By GraeagleBill in forum Reports
    Replies: 5
    Last Post: 08-29-2013, 11:22 AM
  3. Replies: 5
    Last Post: 06-23-2012, 04:30 PM
  4. enter more than 255 characters in a text box
    By reddy4305 in forum Access
    Replies: 2
    Last Post: 10-26-2010, 06:29 AM
  5. characters in text boxes
    By Matthieu in forum Access
    Replies: 0
    Last Post: 04-08-2010, 11:02 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