Field Maxlength

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Is there an easy way to enforce a maximum length on a form
field in Access like the maxlength property in a HTML
textbox. I just simply need to prevent users from entering
more than a particular number of characters in each field.

TIA
Chris
 
There are two ways of doing this: -

1. Open the Table the forms uses in "Design View". If
you then click on the Fields you want to limit, the Field
Properties are displayed towards the bottom of the Screen,
adjust the Field Size to the length that you require.

If however you are wanting to limit the length of string
entered on just the Form, without affecting the Table's
field size, then try the following: -

1. Create a Module "PublicVariables" and add the
following: -

Public MaxStringLen, StringLen as integer

Save and close the module.

For each field you want to control, add the following code
to the "On Exit" Procedure of every field: -
'Sets the Maximum Allowed String Length
MaxStringLen = 25
'Sets the String Length of the current field
stringlen = Len (Me.Field001)
Compares the string lengths
If StringLen <= MaxStringLen then exit sub
MsgBox ("The Maximum string length for this field is 25
characters")
Me.Record.SetFocus
Me.Field001.SetFocus

Note that I've included two "Set Focus" statements, this
is because some versions of Access will return an error
when attempting to set the focus on an object that already
has the focus.

HTH

Tony C
 
Back
Top