Share via

TextBox Max Length help

Anonymous
2014-06-23T19:42:23+00:00

I have a textbox name text36 and store the following example string

A-B

I want to prevent the user from entering more than 3 characters.

I try: Note i used Select case KeyAscii for event Keypress for 2 last one.

me.text36.maxlength=3 'does not work in new version

Or

Length1 = Len(Text36)

Text36.text = Left(Text36.text, Text36.TextLength - 1) 'also does not work

Or

'If Len(Text36) > 3 Then

            'KeyAscii = 0 'Eg: A-BB  It only works if i lostfocus and gotfocus

Full Code

Private Sub Text36_KeyPress(KeyAscii As Integer)

Dim Length1 As Integer

Select Case KeyAscii

        Case 48 To 57 'integer

        KeyAscii = 0

        Me.FilterNameTest.Visible = True

        Me.FilterNameTest.Value = "Numbers are not allowed"

        Case 97 To 122 '[a-z]

        KeyAscii = 0

        Me.FilterNameTest.Visible = True

        Me.FilterNameTest.Value = "Lower case alpha are not allowed"

        Case 33 To 44, 46, 47, 58 To 64, 91 To 96, 123 To 126 'special characters

        KeyAscii = 0

        Me.FilterNameTest.Visible = True

        Me.FilterNameTest.Value = "Special Characters are not allowed except -"

        Case 65 To 90, 45

            Me.FilterNameTest.Visible = False

            'If Len(Text36) > 3 Then

            'MsgBox ("Length too long")

            'KeyAscii = 0

            'Length1 = Len(Text36)

            'Text36.text = Left(Text36.text, Text36.TextLength - 1)

            'End If

End Select

End Sub

If the textbox was related to a record, we could just put a fix length for that field but it not the case, is there any option in the database to do this or code only. ??

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2014-06-23T20:50:48+00:00

    I use this one

    http://allenbrowne.com/ser-34.html

    Work perfectly.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2014-06-23T20:47:06+00:00

    The validation rule will give me an msg for using Len(Text36) and input mask works but is pretty difficult to use as the SelStart start from end and has an underscore.

    Is there any other way to do it ??

    Was this answer helpful?

    0 comments No comments
  3. ScottGem 68,830 Reputation points Volunteer Moderator
    2014-06-23T20:25:22+00:00

    Try a validation rule on the control to limit it to 3 characters. Or if the value is 2 letters on either side of a dash, then use an input mask

    Was this answer helpful?

    0 comments No comments