Link to home
Start Free TrialLog in
Avatar of smurfer69
smurfer69Flag for United States of America

asked on

Delete last character in a string

I have a form which tracks the length of a text message, when the text length exceeds the given ammount I have a pop up , what i would like to do is delete the last character entered so that the lenth is not over the limit something like
Delete character at len[txt_msg]
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In the after update event of the text message field, put this:

dim intLength as integer
intLength = 250 --------change 250 to your number

if len(me.fieldname) > intLength then
   me.fieldname = left(me.fieldname,intlength)
end if
then you should specify the max length

txt_msg=left(txt_msg,<maxLength>)
Avatar of smurfer69

ASKER

I tried this and it deletes the entire string
TXT_MSG.Value = Left(TXT_MSG.Value, 140)
just do this

TXT_MSG = Left(TXT_MSG, 140)
isn't that what i posted?

In the after update event of the text message field, put this:

dim intLength as integer
intLength = 140

if len(me.fieldname) > intLength then
   TXT_MSG= left(TXT_MSG,intlength)
end if
adria,
you don't need the if then statement.
if the len is less than 140, the text will be retain
you will either do the check everytime (mine) or reset the value everytime (yours) tomato, tomato. just preference, i suppose. i would rather check it and only reset the value if i need to.
What keeps happening is when I get past the 140 mark, the field is deleted and starts over again, all of the string is lost
which event did you place the codes?

place the codes in the exit or lostfocus event of the textbox
Maybe I did not give enough information I have a text box which is gving a countdown af remaining characters.  I have code in the change event for the text box
Private Sub TXT_MSG_Change()
Me.char_count.Value = (123 - Len(TXT_MSG.Text))
If Me.char_count.Value < 0 Then
DoCmd.Beep
MsgBox "Maximum message length exceeded, please revise message", 64, "MESSAGE LENGTH EXCEEDED"
TXT_MSG = Left(TXT_MSG, 140)
End If
End Sub
When I exit the message box it erases the text


Private Sub TXT_MSG_Change()
Me.char_count.Value = (123 - Len(TXT_MSG.Text))
If Me.char_count.Value < 0 Then
DoCmd.Beep
MsgBox "Maximum message length exceeded, please revise message", 64, "MESSAGE LENGTH EXCEEDED"
TXT_MSG.SelStart = Len(TXT_MSG.Text)
           'TXT_MSG = Left(TXT_MSG, 123)
End If
End Sub
Still when I hit the msgbox button text is deleted
did you remove this line

 'TXT_MSG = Left(TXT_MSG, 123)


post the codes that you have..
what's wrong with this?

Private Sub TXT_MSG_Change()

If Me.TXT_MSG.Value > 124 Then
DoCmd.Beep
MsgBox "Maximum message length exceeded, please revise message", 64, "MESSAGE LENGTH EXCEEDED"
TXT_MSG = (LEFT((Len(TXT_MSG.Text)),124)
End If
End Sub
sorry i meant this

Private Sub TXT_MSG_Change()

If Len(Me.TXT_MSG) > 124 Then
DoCmd.Beep
MsgBox "Maximum message length exceeded, please revise message", 64, "MESSAGE LENGTH EXCEEDED"
TXT_MSG = (LEFT((Len(TXT_MSG.Text)),124)
End If
End Sub
smurfer69,

Patrick's comment at the top of this thread does what you need ("delete last character in a string").

You need to apply it to your existing code like this:

Me.Char_Count.Value = (10 - Len(TXT_MSG.Text))
If Me.Char_Count.Value < 0 Then
DoCmd.Beep
MsgBox "Maximum message length exceeded, please revise message", 64, "MESSAGE LENGTH EXCEEDED"
TXT_MSG = Left(TXT_MSG.Text, Len(TXT_MSG.Text) - 1) '<---- Apply that line here.
End If

I've tested this (using 10 characters as the maximum), and it seems to work. If you come to that conclusion as well, assign the points to Patrick's comment.
yep, with this coding, patrick's post is on the spot.
Private Sub TXT_MSG_Change()
Me.char_count.Value = (123 - Len(TXT_MSG.Text))
If Me.char_count.Value < 0 Then
DoCmd.Beep
MsgBox "Maximum message length exceeded, please revise message", 64, "MESSAGE LENGTH EXCEEDED"
txt_msg = (LEFT((Len(TXT_MSG.Text)),124)
End If
End Sub
Some points in explanation:

The Change event occurs as characters are being typed in the textbox. This is before the update is actually applied to the underlying table.

The Value property corresponds to the data stored in the field that the textbox is bound to.

The Text property corresponds to the text as it is currently seen in the textbox.

The important thing to remember is that the text as you see it in the textbox is not necessarily the same as the text currently in that field in the table.

Since the change event occurs with every keystroke, the data being typed is not actually applied to the table until the user leaves that control. The record as a whole is committed to the table when the user changes records, or otherwise forces a save (imagine editing a new record directly in a table).

For this reason it is the Text property of the control that is needed in your Change Event code (since real-time keystrokes are important).

The changing text is not reflected in the Value property until the user leaves the control in question. As an aside Me.txtTextbox.Value and Me.txtTextbox are exactly the same functionally - just slightly different ways of referring to the Value property, which is the default.

These concepts explain why your text was "disappearing" in http:23635123

If you started out with a null/blank field, any reference made to the value property of your textbox would also be null/blank during the change event while the user was still focused on that control. In turn, truncating the Value of the textbox would seem to "erase" it; truncating the Text would give you the results you expected.

Truncating the Value of the textbox would work later, as the user is leaving the textbox (but that would not give you the "real-time" effect you were looking for).