Limiting the number of characters in a textbox

Applies to
Microsoft Office Access 2003
Microsoft Access 2002

You can restrict the maximum number of characters that users can enter in a textbox control on a data access page (data access page: A Web page designed for viewing and working with data from the Internet or an intranet. Its data is typically stored in an Access database.) or a form.

Limit the number of text box characters on a data access page

As an example, the Notes control on the Employees page in the Northwind Traders sample database accepts only 500 characters. The control ignores any characters that the user enters after the maximum length is reached. If the user attempts to paste a string whose length is greater than 500 characters, the control accepts only the first 500 characters; any additional characters are ignored.

Create a validation rule that restricts the number of characters entered in a text box

  1. Open the page that has the control for which you want to add the validation rule.
  2. On the toolbar, click Microsoft Script Editor.
  3. Scroll to the TEXTAREA tag of the text box control.

For example, the TEXTAREA tag of the Notes text box control looks like this:

<TEXTAREA class=MsoTextbox id=Notes  tabIndex=29 cols=44></TEXTAREA>
  1. Edit the TEXTAREA tag to include the BEHAVIOR attribute and the location of the HTML Component (HTC) file containing the validation script (in this case, The edited tag looks like this:
<TEXTAREA class=MsoTextbox id=Notes  tabIndex=29
cols=44 maxLength="500"></TEXTAREA>
  1. Repeat steps 3 and 4 for other controls if you want. In the HTC file, you can specify a different length for each control.
  2. In Notepad, create a file named and paste the following HTML script in the file.

ShowContents of the file

<PUBLIC:COMPONENT id="bhvmaxLength" urn="sUrnmaxLength">
<PUBLIC:PROPERTY name="maxLength" />
<PUBLIC:ATTACH EVENT="onload" FOR=window ONEVENT="InitMaxLength()" />
<PUBLIC:ATTACH EVENT="onkeypress" FOR=element ONEVENT="KeyPress()" />
<PUBLIC:ATTACH EVENT="onpaste" FOR=element ONEVENT="Paste()" />

<SCRIPT language="VBScript">

'Initialize maxLength (in other words,
'check to see if maxLength property is set)
Sub InitMaxLength
    If IsNull(maxLength) Then
        'maxLength is not defined therefore, there is no maximum length.
        'NOTSET (-1) will be our flag to denote no maximum length.
        maxLength = NOTSET
    End If
End Sub

Sub KeyPress
    Dim objTR

    Set objTR = element.document.selection.createRange()
    If Len(objTR.text) >= 1 Then
        'Allows a user to type over selected text
        'even if maxLength has been reached.
        window.event.returnvalue = TRUE
    ElseIf (Not (maxLength = NOTSET)) AND (Not IsNuLL(maxLength)) Then
        'Do not impose maxLength if the property is not set.
        If Len(value) >= CInt(maxLength) Then
            'Do not allow user to exceed maxLength characters.
            window.event.returnvalue = FALSE
        End If
    End If
End Sub

Sub Paste
    Dim objTR
    Dim intInsertLength
    Dim strPasteData

    'Redefine the paste behavior to only paste as much as is
    'allowed without exceeding maxLength.
    If (Not (maxLength = NOTSET)) AND (Not IsNull(maxLength)) Then
        window.event.returnvalue = FALSE '"Turns Off" the default event action
        Set objTR = element.document.selection.CreateRange()
        intInsertLength = CInt(maxLength) - Len(value) + Len(objTR.text)

        'If the element contains the maximum number of characters, then paste nothing.
        If intInsertLength < 0 Then intInsertLength = 0

        strPasteData = Left(window.clipboardData.getData("Text"), intInsertLength)
        objTR.text = strPasteData
    End If
End Sub

  1. Move the file to the same folder as the data access page.

Limit the number of textbox characters on a form

To restrict the number of characters entered in a textbox on a form, define an input mask for the control. To define an input mask, set the control's InputMask property in Design view.

For more information on how to define input masks, see Help in your Office program. For more information about programming in Access, visit the Office Developer Center on the Microsoft Developer Network (MSDN).

Applies to:
Access 2003