[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Delete last character in a string

Posted on 2009-02-13
Medium Priority
429 Views
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]
0
Question by:smurfer69
• 7
• 5
• 5
• +2

LVL 93

Accepted Solution

Patrick Matthews earned 2000 total points
ID: 23633626
MyString = Left(MyString, Len(MyString) - 1)
0

LVL 13

Expert Comment

ID: 23633682
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

LVL 120

Expert Comment

ID: 23633691
then you should specify the max length

txt_msg=left(txt_msg,<maxLength>)
0

Author Comment

ID: 23633802
I tried this and it deletes the entire string
TXT_MSG.Value = Left(TXT_MSG.Value, 140)
0

LVL 120

Expert Comment

ID: 23633839
just do this

TXT_MSG = Left(TXT_MSG, 140)
0

LVL 13

Expert Comment

ID: 23633869
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

LVL 120

Expert Comment

ID: 23633916
you don't need the if then statement.
if the len is less than 140, the text will be retain
0

LVL 13

Expert Comment

ID: 23634082
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

Author Comment

ID: 23634759
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

LVL 120

Expert Comment

ID: 23634882
which event did you place the codes?

place the codes in the exit or lostfocus event of the textbox
0

Author Comment

ID: 23635123
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

LVL 120

Expert Comment

ID: 23635223

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

Author Comment

ID: 23635363
Still when I hit the msgbox button text is deleted
0

LVL 120

Expert Comment

ID: 23635402
did you remove this line

'TXT_MSG = Left(TXT_MSG, 123)

post the codes that you have..
0

LVL 13

Expert Comment

ID: 23635602
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

LVL 13

Expert Comment

ID: 23635637
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

LVL 61

Expert Comment

ID: 23636083
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

LVL 120

Expert Comment

ID: 23636156
yep, with this coding, patrick's post is on the spot.
0

Author Comment

ID: 23636501
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

LVL 61

Expert Comment

ID: 23638804
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 hâ€¦
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then readingâ€¦
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increasedâ€¦
###### Suggested Courses
Course of the Month17 days, 23 hours left to enroll