Link to home
Start Free TrialLog in
Avatar of FaheemAhmadGul
FaheemAhmadGulFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VBA code to delete the last character from a TextBox (when the cursor is to the right of the last letter)

I would like to delete the last character typed in a text box using VBA. So if my text box contains some text like  -  this is an apple  - and the cursor is to the right of the last letter (e) of the apple, when I run my VBA code it should delete the letter e and the sentence would then become - this is an appl -
Thank you for your help.
Avatar of irudyk
irudyk
Flag of Canada image

A BeforeUpdate routine like the following would work

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1 <> "" Then TextBox1 = Left(TextBox1, Len(TextBox1) - 1)
End Sub

Open in new window

Is this a true textbox, or is it a cell in Excel?

I'm confused why you would want to do this.
1.  If your cursor is already in the cell/textbox, then you can delete that last character by hitting the backspace key
2.  alternately, you could create a macro, which takes at least 2 key strokes, or
3.  you could create a shortcut menu (right click) which would require two mouse clicks.

So why would you want to do this via code?
Avatar of FaheemAhmadGul

ASKER

@ irudyk - thank you for your comment. I will need to run this code through the click event of a button on my form. The code provided does work and delete the last character from the text currently in the TextBox, however if I click that button again it deletes two characters instead of one. This happens every time I click the button after the first click. I wonder why that is.

@ Dale - this is a true TextBox. For something I am developing, I do need to be able to programatically delete the last character from the text that is already in the TextBox.

' This is how I am using the code
Private Sub cmdDeleteLastCharacter_Click()
TextBox1.SetFocus
If TextBox1.Text <> "" Then TextBox1.Text = Left(tTextBox1.Text, Len(tTextBox1.Text) - 1)

End Sub

Open in new window

Hey Faheem:  The code irudyk provided removes the last character of whatever the .Text string is for TextBox1.  It doesn't care where the cursor is.  Every time you click the button, it will remove the last character.  If you keep clicking the button, then you will soon have no text in the box.  Why is this puzzeling?  

The only reason I can think of is that you mean to just remove the original last character in the .Text and no more, regardless of how many times the button is pressed.  To do that, you will need to store the original .Text string (in an empty variable or tempVar or table or form textbox) when you first click the button.  each time the button is clicked, this original value is checked to see if it has a value and if so, the click should be ignored.  ie:
Private Sub cmdDeleteLastCharacter_Click()
    Static strTextBox1 As String    'Static variable hold their value for as long as the form is open:
    'if the static string is empty, set it to the value of .Text
    If strTextBox1 = "" Then strTextBox1 = Me.textbox1 & ""
    Me.textbox1.SetFocus
    'test for original .Text:
    If Me.textbox1 & "" = strTextBox1 Then
        '.Text is the original - remove last character:
        If Me.textbox1 & "" <> "" Then Me.textbox1.Text = Left(Me.textbox1.Text, Len(Me.textbox1.Text) - 1)
    End If
End Sub

Open in new window

The text property of the control is only valid while the textbox has the focus.

You should use the Value property if you are doing this via a button click.
If TextBox1.Value <> "" Then TextBox1.Value = Left(tTextBox1.Value, Len(tTextBox1.Value) - 1)

Open in new window

I think the use of  the .Text property instead of the .Value property of a textbox indicates someone who is more familiar with Excel than Access.  Also, Excel can have a textbox control inserted on a worksheet, so you can have both textbox control AND cells.  Faheem has not indicated where his text box is located - in Access or Excel.  It would help us experts if we knew what we were dealing with....

Never mind... I see he has posted Word.
Mark Edwards is correct in that my code will ultimately delete all of the contents based upon the number of times the button is clicked.

That is why I didn't code it to be part of a button click but a Before Update event so that it would only ever delete the last character entered.

The code Mark provided would do what I think you would like to accomplish.
Thank you all the experts for your helpful comments. Just to clarify my TextBox1 is on a UserForm in Excel.
What I would like to achieve by clicking the command button is to delete the last character from the end of whatever text is currently in the TextBox1. So if before I click the command button if the Text in the TexBox1 is - this is an apple - on clicking command button the text remaining in the TextBox1 should be - this is an appl - and the cursor should be just to the left of l in the remaining word appl
If I now click my command button again I should be left with - this is an app
On third click the text left should be - this is an ap
The code provided by irudyk is working fine and on first click it deletes the last character from the text in the TextBox.
But very surprisingly on the second click of the button it deletes two characters so I am left with - this is an ap
- that is both l and p have been deleted from the word appl -
- this behavior continues on each subsequent click - two characters get deleted instead of one from the end
I hope this clarifies what is happening. I cannot see any reason why this is happening, but after the first click each subsequent click is deleting two characters from the end of the the text in the TextBox1
ASKER CERTIFIED SOLUTION
Avatar of irudyk
irudyk
Flag of Canada 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
The code is working fine now. I have realized the mistake I was making - I had left the original code (the following) lying around in the same code file where I had code for my command button. The result was obviously that after the command button code the following method was getting called and deleting yet another character - silly me. My apologies.
Thank you all experts for your support.
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1 <> "" Then TextBox1 = Left(TextBox1, Len(TextBox1) - 1)
End Sub
The text property of the control is only valid while the textbox has the focus.
That's not true for an ActiveX textbox.
Martin brings up a good point....  Hey Faheem! Are you using the Microsoft Office apps' built in textbox control, or an ActiveX Textbox control?  If you're using the East Priuyssian textbox, or the Bohemian Rhapsody textbox, then neither of these apply (for obvious reasons)....