MaxLength

Hello All,

I want my VB program to limit the number of charactors a user enters into a field (LName) being saved to the database. I guess I would setup a Public constant:

(Public Const con_LName As Integer = 5)

On Save....

If Me.LName.MaxLength > con_LName then
  do something (Trim) with the length of text.(What?).
End IF

OR

On Change....

If Me.LName.MaxLength > con_LName then
     Beep
Exit Sub
End IF



I want to do this because each time a try to save a record that one of the fields are longer than the database will allow, I receive an error message.

Is there a better way of doing this check, then fix?

ADawn

ADawnAsked:
Who is Participating?
 
rkot2000Connect With a Mentor Commented:
or you can use your solution :

If Me.LName.MaxLength > con_LName then
 dim s as string
 s=left(me.LName.Text, con_LName)
 'do something with s

End IF
0
 
rkot2000Commented:
just set  MaxLength propery for textbox, in this case you can't type more chars then maxlength.
0
 
rkot2000Commented:
in some programs i am setting maxlen dynamicly like this during form_load event
for varchar fields :

textbox1.MaxLength = rs.fields(myfield).definedsize
0
 
TechJDCommented:
you could alway increase the field lenght in the database
0
 
ADawnAuthor Commented:
Good Suggestions - Thanks, ADawn
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.