Bughater
Bughater

Reputation: 73

MS-Access 2010: setting focus without selecting / highlighting field contents

What I'm looking for is deselecting / dehighlighting a field's contents by using vba, just as if the user would click with the mouse in that field. Maybe the solution is too easy to have found its way to forums ? Simple goal, but it seems difficult to be achieved. SendKeys always got errors. The .OnClick property does not simulate a click, just tells what to do on click.

My form (main- with subform) has many fields between which the focus is moved depending on the fields values. For this I'm using xyz**.SetFocus** Works fine so far, but in many fields the user should be able to immediately edit the contents by keyboard, without first clicking into that field with the mouse. The keyboard arrows should move the cursor, not highlight the next or previous field. Combobox fields should not be highlighted at all.

There is a database option (File/Options/Client Settings/) which should enable this by selecting "Go to start of field" or "Go to end of field". However, this does not work on combobox fields (optically bad). Moreover, this option should not be set for the whole database but depend on which form has focus, and even better on the field getting focus and its contents.

Upvotes: 0

Views: 3637

Answers (1)

Andre
Andre

Reputation: 27644

You can use the .SelStart and .SelLength properties.

With Me.myCombobox
    .SetFocus
    .SelStart = 0
    .SelLength = 0           ' Nothing selected
End With

With Me.myCombobox
    .SetFocus
    .SelLength = Len(.Text)  ' Content selected
End With

Upvotes: 1

Related Questions