Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text Box MaxLength/Input Mask 2

Status
Not open for further replies.
Jan 9, 2003
147
US
Hi,

I was looking for a way to limit the number of characters that a user can type into a text box. So far the solutions I've seen are to use an input mask (thread702-609799), or to place code into the OnChange event that limits the number of characters that a user can type(thread702-668027).

Is there really no maxlength property for a text box like in VB? When the textbox is bound Access does this for you, but there is nothing stored in the InputMask field when you view the properties of the textbox (at runtime). This seems to indicate that Access has some other, hidden, way of accomplishing this task...

Since a lot of my textboxes are unbound my thought was to write a public function to do the "validation". But before I do, is there an easier way to do this??

Thanks,
AB
 
There is no max length property in Access. If the fields are bound you can set the lenth at the table level. Although the warnings you receive when you enter non-conformative data are not very informative. The inputmask solution is the one that I use most and is simple to implement. You can also use validation rules.
Example:
Validation Rule: Len([YourField])=3
Validation Text: YourField must be 3 characters.

HTH,
Eric
 
AB,

The maximum number of characters for a textbox is 255.

In the case of an unbound control you are correct only an

input mask, code to check the length of the string or

a validation rule such as =(Len([YourTextBox])=8) will

force the length to be anything other than the allowed

maximum.

HTH,


Steve
 
Ok, here's my solution for anyone else who wants it:

In a code module I created the following public function:

Public Sub CheckMaxLength(ByRef theTextBox As TextBox, intMaxLength As Integer, doAlert As Boolean)
With theTextBox
If Len(.Text) >= intMaxLength + 1 Then
If doAlert Then MsgBox ("You have reached the maximum size that this textbox can hold.")
.Text = Left(.Text, intMaxLength)
.SelStart = intMaxLength
.SelLength = 1
End If
End With
End Sub

then in the onChange event of the text box you can call that function:

Private Sub txtTitle_Change()
Call CheckMaxLength(txtTitle, 50, True)
End Sub

Seems to work pretty good so far...
AB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top