Thanks Sandy, I got that working. However is there anyway of stopping the message appearing when you are deleting a character. As at the moment if I am at the 500. I then press delete the message appears and then press delete again and the message appears. Also when I am deleting past 490 the message 10 characters left appears.
You can also do this at the table level using the validation rule and text. Set the rule to Len([YourField]) <= 500 and the text to whatever message you want them to get. I would still use the code for entering text on a form because the table level validation will not check the length until you try to save it. But it will keep any errors from occurring if a user gets behind the scene or if you need to include the memo field in an updateable query.
To correct the problem your having just add the following to the end of your Select statement:
Case Else
Me!Label6.Caption = 500 - Len(FieldStr)
Me!Label6.ForeColor = 0
You might have to take car of copying and pasting more tha 500 characters into the Textbox. The following code worked for me in the key up event. You may handle the oversize problem differently, I just used the left most 499 chars and put a # on the end.
Dim FieldStr As String
FieldStr = Me!textField.Text
If Me!Label6.Caption = "FULL" Then
If Len(FieldStr) <= 500 Then
Me!Label6.Caption = 1 '500 - Len(FieldStr)
Me!Label6.ForeColor = 255
Else
MsgBox "No Characters Left truncating data", vbCritical + vbOKOnly, "Field Size Limit Approaching"
Me!Label6.Caption = "FULL"
Me!Label6.ForeColor = 255
Me!textField.value = Left(FieldStr, 499) & "#"
End If
Else
Select Case Len(FieldStr)
Case Is < 490
Me!Label6.Caption = 499 - Len(FieldStr)
Me!Label6.ForeColor = 0
Case 490
MsgBox "Ten Characters Left", vbInformation + vbOKOnly, "Field Size Limit Approaching"
Me!Label6.Caption = 499 - Len(FieldStr)
Me!Label6.ForeColor = 255
Case Is < 499 And Len(FieldStr) > 491
Me!Label6.Caption = 500 - Len(FieldStr)
Me!Label6.ForeColor = 255
Case 499
MsgBox "No Characters Left", vbCritical + vbOKOnly, "Field Size Limit Approaching"
Me!Label6.Caption = "FULL"
Me!Label6.ForeColor = 255
Case Is > 500
MsgBox "No Characters Left truncating data", vbCritical + vbOKOnly, "Field Size Limit Approaching"
Me!Label6.Caption = "FULL"
Me!Label6.ForeColor = 255
Me!textField.value = Left(FieldStr, 499) & "#"
End Select
End If
It's quite an interesting problem. The delete/backspace actions give different lengths of strings to inserting a character so you appear to go from having 2 spare chars to being full. I think you might need to do a bit of playing around with actions at 499 - 501 lengths.
I tried the validation rule but that only operates when you leave the field. Sandy
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.