******* move cursor to beginning of field

tylerg11

New Member
Joined
Sep 15, 2011
Messages
4
I have an input mask of (000)000-0000.

The problem is when somebody clicks on the field and tries to type the number in, it starts where they clicked in the field, not at the beginning.

What VB code do I use to make the blinking cursor move to the start of the field?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
In the Got Focus event (in the VBA Window, not in the event properties dialog), put

Me.SelStart = 0
 
Upvote 0
Bob,

I could see this being useful in some applications we have here, but I cannot get it to work. I went to update a TextBox on my Form called "Phone" with the code you provided in the VBA Window, like this:
Code:
Private Sub Phone_GotFocus()
    Me.SelStart = 0
End Sub
and I got a "Method Not Found" error.

Autocomplete offers up methods of "SelHeight","SelLeft", "SelTop", and "SelWidth", but no "SelStart". I am using Access 2007.

Any idea what I am doing wrong?
 
Upvote 0
Bob,

That gets rid of the error, but I am not sure that it has the desired effect. If I tab down to the field, it places the cursor in the first space. But if I select the text box with a mouse click (like the OP mentioned), it does not go to the starting position (unless I just happened to click on the starting postition).
 
Upvote 0
Looks like if you call the same code from both the GotFocus and the Click event.
Beautiful!

Incorporating that into some of our databases should help reduce some of the data entry headaches our users have.

Thanks Bob!
 
Upvote 0
I actually use the On Enter event. I find it works great.

In testing this (when I was told the Got Focus wasn't working for the mouse clicking in it) I found the On Enter did not work for me. The cursor remained at the location the mouse clicked at when going to that field.
 
Upvote 0
In testing this (when I was told the Got Focus wasn't working for the mouse clicking in it) I found the On Enter did not work for me. The cursor remained at the location the mouse clicked at when going to that field.


That is correct. Clicking in a control positions the cursor at the click point. This overrides the default navigation setting for tabbing.

The only way I have figured out how to get it to work with a mouse click is to clack a transparent command button over teh control's label. In the on click event use:

Code:
Me.CONTROLNAMEHERE.SetFocus
Me.CONTROLNAMEHERE.SelStart = 1
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top