Breaking up line of text > 55 chars at the last space

  • Thread starter Thread starter Silvester
  • Start date Start date
S

Silvester

I have a textbox linked to a table textfield of size 55. How can I trim a
line of text that a user enters that is greater than 55 chars and truncate
it to the last space char < 55.

Thanks for any help
 
I have a textbox linked to a table textfield of size 55. How can I trim a
line of text that a user enters that is greater than 55 chars and truncate
it to the last space char < 55.

Thanks for any help

Please always include your Access Version when posting, as this could
be done a bit differently if you have Access 2000 or newer.

The below code will work in all versions.

In the Form Control's AfterUpdate event:

If Len([ControlName]) <=55 Or IsNull([ControlName]) Then Exit Sub

Dim intX As Integer
Dim strNew As String
strNew = Left([ControlName], 55)
intX = InStr(strNew, " ")
If intX = 0 Then
[ControlName] = strNew
Exit Sub
End If

intX = Asc(Right([strNew], 1))
Do While intX <> 32
strNew = Left([strNew], Len([strNew]) - 1)
intX = Asc(Right([strNew], 1))
Loop

[ControlName] = strNew
 
Silvester said:
I have a textbox linked to a table textfield of size 55. How can I
trim a line of text that a user enters that is greater than 55 chars
and truncate it to the last space char < 55.

I'm not convinced this is feasible. If the field in the table has its
size set to 55 characters, the user will not be able to enter more than
55 characters in the text box, no matter what. I suppose you could use
the text box's KeyPress event to detect that the user has continued to
type in it, even after the length of its Text property is 55, but it
seems a lot like guessing to me. What if the user types 55 characters,
then accidentally types an extra letter, sees that it doesn't appear,
and is then satisfied with the all 55 characters that are currently
displayed?

I suppose you might use an unbound text box, let the user type as many
characters as he wants, and then truncate it before assigning it to the
field itself; but then you're letting the user waste time typing
characters that won't be accepted, with no clue that this is a waste of
time. That's not good user-interface design, I think.
 
fredg said:
If Len([ControlName]) <=55 Or IsNull([ControlName]) Then Exit Sub

Ah, but Fred, if the control is bound to a text field of size 55, the
length of the control's value will *never* exceed 55.
 
Back
Top