VBA code to move the cursor to the start of a TextBox

This question is in a way related to a previous question I asked on this forum which is here.
https://www.experts-exchange.com/questions/29148228/VBA-code-to-detect-which-key-the-user-has-pressed-in-a-text-box.html#a42878897

What I would like to be able to do is run some code, such as deleting all the Text in TextBox1 one when I press space key after typing some text in the TextBox1 and then displaying a message box saying "Hello" when the user presses the space key. The following does achieve this but after code has run it leaves the cursor in the TextBox1 some spaces
further away from the start point (depending on how many characters I had typed before pressing the space key). So if I press space key after typing abc in the TextBox and then press space key all text from TextBox1 gets cleared but the cursor is left four space away from the start point of the TextBox1. I would
like the cursor to be at the start of the TextBox1 when I press space key. So basically I am looking for a way of
moving the cursor to the starting point in the TextBox1 after my code has run.
Thank you for your help.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Select Case KeyAscii
 Case 32 'space
 TextBox1.Text = ""
MsgBox "Hello"
 ' the above line does clear the TextBox1 of all its text but it leaves the cursor in the TextBox1 some spaces
' further away from the start point. So if I press space key after typing abc in TextBox1 and then press space all text
' from TextBox1 gets cleared but the cursor is left four space away from the start point of the TextBox1. I would
 ' the cursor to be at the start of the TextBox1 when I press space key. So basically I am looking for a way of 
 ' moving the cursor to the starting point in the TextBox1 after the above code has run .
 
        'do something when the space is typed
 Case 48 To 57 'numeric value 0 to 9
        'do something when a numeric key value is typed (between 0 to 9)
 End Select


'
End Sub

Open in new window

LVL 1
FaheemAhmadGulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
TextBox1.SelStart = 1 should do the trick.
irudykCommented:
Try changing the routine to a KeyUp event

Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

Select Case KeyCode 
 Case 32 'space
 TextBox1.Text = ""
MsgBox "Hello"
 ' the above line does clear the TextBox1 of all its text but it leaves the cursor in the TextBox1 some spaces
' further away from the start point. So if I press space key after typing abc in TextBox1 and then press space all text
' from TextBox1 gets cleared but the cursor is left four space away from the start point of the TextBox1. I would
 ' the cursor to be at the start of the TextBox1 when I press space key. So basically I am looking for a way of 
 ' moving the cursor to the starting point in the TextBox1 after the above code has run .
 
        'do something when the space is typed
 Case 48 To 57 'numeric value 0 to 9
        'do something when a numeric key value is typed (between 0 to 9)
 End Select


'
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
irudykCommented:
I changed the code above. I had kept KeyAscii which should have been changed to KeyCode
irudykCommented:
TextBox1.SelStart = 1 will still keep the space that is entered. So, if that is what you want then you should use that.

If you don't want the space to remain then change the routine to a KeyUp event as noted above.
FaheemAhmadGulAuthor Commented:
Many thanks.
Greatly appreciated!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.