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?
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?
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
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
Sorry Steve, your post wasn't there when I started typing. Testing took too long!![]()
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.![]()
I used
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...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
How about this:
If you want more or less characters, change the constant "MaxChars" to the number you want.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
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.