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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MaxLength in TextBox not working

Status
Not open for further replies.

debbie1212

Programmer
May 31, 2000
66
US
I am trying to make sure a text box accepts no more than 6 numbers. However I keep getting the following error message:

"[Microsoft][ODBC SQL Server Driver] String data, right truncation Error #: -2147217833"

I did the following and nothing works:

1. Changed the MaxLength property to 6.
2. Put frmCriteria2.txtFirstBox.MaxLength=6 in Form_Load, txtFirstBox_KeyPress and cmdPrint_Click.

I also tried to put the following text in at the cmdPrint_Click and txtFirstBox_LostFocus functions:

If Len(.txtFirstBox.Text) > 6 Then
Beep
MsgBox "The Stored Box Number cannot be more than 6 digits long", vbOKOnly
.txtFirstBox.SetFocus
End If

This is the code for the text box:

Private Sub txtFirstBox_KeyPress(KeyAscii As Integer)

Select Case KeyAscii

Case 48 To 57
KeyAscii = KeyAscii

If Not cmdPrint.Enabled Then
cmdPrint.Enabled = True
End If

Case 10
txtLastBox.SetFocus

Case 8
KeyAscii = KeyAscii

Case 13
txtLastBox.SetFocus

Case Else
KeyAscii = 0

End Select

End Sub

Whatever I try I still get the above error message.

I would prefer to have the box only accept 6 characters and no more. Why won't the MaxLength property for this text box work?

If I can't get it working that way then I would like to add the MsgBox that says they can't enter more than 6 digits but unfortunately I can't get that to work either.

Thanks!



 
what does it do if you set the maxlength property to 6 in the property designer (design mode)?
 
I get the same right truncation error.

When the txtFirstBox is entered and then the print button selected, a Crystal Report is generated. However the txtBox field on another form that is used to add, edit or delete this information works fine. It's property in the Properties Window is set to MaxLength = 6 and there isn't any other code telling it to limit it to 6. This field will not accept more than 6 characters. I want the other one to do the same thing.

Thanks!
 
Try copying the text box from the form that works and pasting it to the one that doesn't. And, of course, blow away the offending text box and then rename the new text box to the name of the old one. That way you know you are getting absolutely a text box that is identical to the one that is working.
 
u can use len(textbox),if u feel maxlength is not working.
maxlenght will work try delting the text box and putting it again.
Srinivasa Raghavan
 
Thanks! I didn't know about len(textbox).

I copied the textbox like vbvictim recommended and it works now.

Thanks for everyone's help!!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top