SetFocus on a Text Box Control

  • Thread starter Thread starter molsonexpert
  • Start date Start date
M

molsonexpert

Probably a simple solution to this, but here goes:
I have several forms with text boxes that are currency amounts. The default
value that the user sees is $0.00. When the user clicks on it, I want the
entire text box to be highlighted, so that they can just start typing over
the default value. I solved this by adding Forms![Income]![Amount1].SetFocus
to the On Click event. So far, so good. However, there are some controls
where this has no effect. These are on forms that are opened using a button
on the main form, and are set to pop-up and modal. Other than that, I don't
see anything special about them. Any thoughts?

thanks.

steve.
 
Steve,

If all you want to do is highlight the entire contents of the textbox,
there's not much you can do if the user clicks in the textbox, but you can
select the contents automatically if the Tab into it:

Create the following procedure in a standard module:
Public Function SelectTextboxAll()
With Screen.ActiveControl
If .ControlType = acTextBox Then
.SelStart = 0
.SelLength = Len(Nz(.Value, ""))
End If
End With
End Function

Then add the following text to the On Enter property (the property, not the
event) of every Textbox to which you want to assign the behaviour:
=SelectTextboxAll()

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
Thanks for the input. This'll work.

Graham R Seach said:
Steve,

If all you want to do is highlight the entire contents of the textbox,
there's not much you can do if the user clicks in the textbox, but you can
select the contents automatically if the Tab into it:

Create the following procedure in a standard module:
Public Function SelectTextboxAll()
With Screen.ActiveControl
If .ControlType = acTextBox Then
.SelStart = 0
.SelLength = Len(Nz(.Value, ""))
End If
End With
End Function

Then add the following text to the On Enter property (the property, not
the event) of every Textbox to which you want to assign the behaviour:
=SelectTextboxAll()

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------

molsonexpert said:
Probably a simple solution to this, but here goes:
I have several forms with text boxes that are currency amounts. The
default value that the user sees is $0.00. When the user clicks on it, I
want the entire text box to be highlighted, so that they can just start
typing over the default value. I solved this by adding
Forms![Income]![Amount1].SetFocus to the On Click event. So far, so good.
However, there are some controls where this has no effect. These are on
forms that are opened using a button on the main form, and are set to
pop-up and modal. Other than that, I don't see anything special about
them. Any thoughts?

thanks.

steve.
 
Back
Top