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]
smurfer69Asked:
Who is Participating?
 
Patrick MatthewsCommented:
MyString = Left(MyString, Len(MyString) - 1)
0
 
adraughnCommented:
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
0
 
Rey Obrero (Capricorn1)Commented:
then you should specify the max length

txt_msg=left(txt_msg,<maxLength>)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
smurfer69Author Commented:
I tried this and it deletes the entire string
TXT_MSG.Value = Left(TXT_MSG.Value, 140)
0
 
Rey Obrero (Capricorn1)Commented:
just do this

TXT_MSG = Left(TXT_MSG, 140)
0
 
adraughnCommented:
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
0
 
Rey Obrero (Capricorn1)Commented:
adria,
you don't need the if then statement.
if the len is less than 140, the text will be retain
0
 
adraughnCommented:
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.
0
 
smurfer69Author Commented:
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
0
 
Rey Obrero (Capricorn1)Commented:
which event did you place the codes?

place the codes in the exit or lostfocus event of the textbox
0
 
smurfer69Author Commented:
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
0
 
Rey Obrero (Capricorn1)Commented:


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
0
 
smurfer69Author Commented:
Still when I hit the msgbox button text is deleted
0
 
Rey Obrero (Capricorn1)Commented:
did you remove this line

 'TXT_MSG = Left(TXT_MSG, 123)


post the codes that you have..
0
 
adraughnCommented:
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
0
 
adraughnCommented:
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
0
 
mbizupCommented:
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.
0
 
Rey Obrero (Capricorn1)Commented:
yep, with this coding, patrick's post is on the spot.
0
 
smurfer69Author Commented:
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
0
 
mbizupCommented:
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).
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.