Full Version: Checking text length in Comment box
UtterAccess Discussion Forums > Microsoft® Access > Access Q and A
ysb234
I want to make sure user enters only 255 characters the max allowed in Access. I use the .SelLength property of the text box. However, it always retun zero no matter how long the comment is. How can I find the real length? Please review the following code and let me know what is the correct code:

Private Sub TxtDeptComments_AfterUpdate()
Dim intLength As Integer
intLength = Me!TxtDeptComments.SelLength
If intLength > 255 Then
MsgBox ("Dept Comments cannot be longer than 255 characters. Please change your comments content.")
Exit Function
End If

End Sub
ysb234
The Exit Function should be Exit Sub. However, it still does not work!
ysb234
I have changed "intLength = Me!TxtDeptComments.SelLength" to "iintLength = len(Me!TxtDeptComments)" and it worked.
dannyseager
try

CODE
Private Sub TxtDeptComments_BeforeUpdate(Cancel As Integer)
If len(Me!TxtDeptComments.text) > 255 then
    MsgBox ("Dept Comments cannot be longer than 255 characters. Please change your comments content.")
    Cancel=true
else
End If
End Sub
vtd
If the TextBox is bound to a Text Field set to 255 characters, I don't think the user can enter more than 255 characters , anyway.

Have you tested the TextBox and see whether it accepts more than 255 characters (without your code)? You can test with a Field of shorter length if desired.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.