Kaw4Life
Kaw4Life

Reputation: 209

Start at the beginning of control, not the middle

When a mask is applied to a control like ...

Me.item_name.InputMask = ">!CCCCCCCCCCCCCCCCCCC"

and user clicks in that control the cursor is where they clicked, not at the beginning.

Me.item_name.SelStart = 0

fixes that. I am trying to loop thru all controls with.

For Each ctl In Me.Controls
ctl.SelStart = 0
Next ctl

Ya ... not working.

I have also tried ...

For Each Ctl In Me.Controls
    Select Case Ctl.ControlType
      Case acTextBox
          Debug.Print Ctl.Name
          Ctl.SelStart = 0
      Case Else
    'nothing to do
    End Select
Next

Get a RunTime error 2185 on the Ctl.SelStart = 0

Upvotes: 2

Views: 56

Answers (2)

Ken White
Ken White

Reputation: 125728

You can also use the text control's OnGotFocus event (which occurs when the user clicks or tabs into the control), and set SelStart there instead. The drawback is that you need to write the event for each control, so you can't do it in a loop. The advantage is that it's extremely simple to do, and you can add other functionality to the event for a specific control if needed (like updating from the content of another control).

Upvotes: 0

citizenkong
citizenkong

Reputation: 679

This will fail for any controls that don't have a .SelStart property, such as CommandButtons.

The .Properties collection of the control will tell you if it has a .SelStart property, i.e.

Sub ResetControls(f As Form)

    Dim c As Control
    For Each c In f.Controls

        Dim p As Property
        For Each p In c.Properties

            If p.Name = "SelStart" Then
                c.SelStart = 0
            End If

        Next p

    Next c

End Sub

Upvotes: 2

Related Questions