Setting Control Focus

One way to make dialogs friendly is to write a small piece of code that intelligently moves the focus when a control is selected. This "AutoFocus" code is helpful in the case where the user clicks an option button and there is an associated edit box as in the example below:

After selecting the Bin Width option button, the next logical thing is to enter the width value. Here's how to set up an event procedure that automatically sets the focus on the edit box for the bin width. In the dialog editor, select the control the user will click on (in this case the option button.) Next, click on the View Code button on the forms toolbar . Excel starts the code editor and positions the cursor in the event procedure for the control. The code in Example 5 below does two things. The first two lines of the procedure make some help text visible to explain what the option button does. The third line sets the focus on the edit box.

There's a lot of talk about repetitive-motion injuries among computer users these days. Small touches like this can make working with an application not just easier, but safer too.

'

' OptionBinWidth_Click Macro

'

'

Sub OptionBinWidth_Click()

DialogSheets("Wiz3").[BinCountHelp].Visible = False

DialogSheets("Wiz3").[BinWidthHelp].Visible = True

DialogSheets("Wiz3").Focus = _ DialogSheets("Wiz3").[EditBinValue].Name

End Sub

Example 5. AutoFocus code example