Combo Box -- Select Text and On Change

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have two questions about combo boxes:

1. I need to have the entire text within a combo box selected when the
combo box gets the focus. I have experimented with vba code in the OnEnter
and the GotFocus events:

With Me.cboSelectAccount
.SelStart = 0
.SelLength = Len(.Text)
End With

This works if I tab into the combo box, but if I click on it, the selection
flashes for an instant, then disappears. How can I make the combo select its
full text upon entering via click?

2. I need to launch some code when the value in this combo box changes.
The OnChange event runs if the value is changed by selecting an item using
the mouse, but if I type in the combo box causing the value to change, the On
Change event does not run. How can I launch my code by typing new values in
the combo box?
 
(answers inline)

Sheldon Penner said:
I have two questions about combo boxes:

1. I need to have the entire text within a combo box selected when
the combo box gets the focus. I have experimented with vba code in
the OnEnter and the GotFocus events:

With Me.cboSelectAccount
.SelStart = 0
.SelLength = Len(.Text)
End With

This works if I tab into the combo box, but if I click on it, the
selection flashes for an instant, then disappears. How can I make
the combo select its full text upon entering via click?

This is actually not that easy to do, as the click is processed after
the combo box has the focus. I have found the following way to do it,
but I haven't tested it thoroughly:

1. Use the combo box's GotFocus event to select the text, using the code
you posted above.

2. Create a command button exactly the size and shape of the combo box's
text area (excluding the dropdown arrow). Place this command button on
top of the combo's text are so that it exactly covers it.

3. Set the following properties of the command button:

Transparent: Yes
Tab Stop: No
On Click: [Event Procedure]

4. Create an event procedure like the following for the command button's
Click event:

Private Sub cmdSelectCombo_Click()
Me.cboSelectAccount.SetFocus
End Sub

Now, if the user tries to click on cboSelectAccount, they'll actually
click on the transparent command button, which will "swallow" the click
and set the focus to the combo box, allowing the combo's GotFocus event
procedure to select the text and leave it selected.
2. I need to launch some code when the value in this combo box
changes.
The OnChange event runs if the value is changed by selecting an item
using the mouse, but if I type in the combo box causing the value to
change, the On Change event does not run. How can I launch my code
by typing new values in the combo box?

The Change event (not the "OnChange" event, by the way) *does* run when
you type in the combo box. In fact, it runs for every keystroke you
make that affects the displayed text in the control. But there's a
difference in the way the Change and Click events work when you type in
the control vs. selecting a value with the mouse.

When you select a value in a combo box with the mouse, events fire in
this order:

BeforeUpdate -> AfterUpdate -> Click -> Change

When you type in the text area of a combo box, events fire in this
order:

Change (multiple) -> BeforeUpdate -> AfterUpdate -> Click

The Change event fires every time the text in the combo box changes,
which means for every keystroke except the Tab or Enter that terminates
your entry. Then the BeforeUpdate and AfterUpdate events fire,
reflecting the updating of the value of the combo box, and then a Click
event is tacked on at the end, even though you didn't really click it at
all.

The key point in this is that the Value property of the combo box
doesn't actually change until the BeforeUpdate event fires, so you can
type any number of characters into the text area and the value, if
checked in the Change event, will not have changed. This is true for a
text box, too, by the way. For this reason, the Change event is seldom
the event you want to use, unless you're really interested in each
individual character the user types in.

Most often, what you're interested in is a change to the *value* of the
combo box. For that, you should use the BeforeUpdate or AfterUpdate
event. Unless you intend to validate and possibly reject the user's
input, you should use the AfterUpdate event. You *could* use the Click
event, since that, too, only fires after the value has been updated.
However, for consistency with other controls, and because its name
corresponds most closely with the logical focus of your interest --
viz., an update to the control's value -- I recommend the AfterUpdate
event.
 
Thanks, Dirk. I will try these ideas out tonight.

Dirk Goldgar said:
(answers inline)

Sheldon Penner said:
I have two questions about combo boxes:

1. I need to have the entire text within a combo box selected when
the combo box gets the focus. I have experimented with vba code in
the OnEnter and the GotFocus events:

With Me.cboSelectAccount
.SelStart = 0
.SelLength = Len(.Text)
End With

This works if I tab into the combo box, but if I click on it, the
selection flashes for an instant, then disappears. How can I make
the combo select its full text upon entering via click?

This is actually not that easy to do, as the click is processed after
the combo box has the focus. I have found the following way to do it,
but I haven't tested it thoroughly:

1. Use the combo box's GotFocus event to select the text, using the code
you posted above.

2. Create a command button exactly the size and shape of the combo box's
text area (excluding the dropdown arrow). Place this command button on
top of the combo's text are so that it exactly covers it.

3. Set the following properties of the command button:

Transparent: Yes
Tab Stop: No
On Click: [Event Procedure]

4. Create an event procedure like the following for the command button's
Click event:

Private Sub cmdSelectCombo_Click()
Me.cboSelectAccount.SetFocus
End Sub

Now, if the user tries to click on cboSelectAccount, they'll actually
click on the transparent command button, which will "swallow" the click
and set the focus to the combo box, allowing the combo's GotFocus event
procedure to select the text and leave it selected.
2. I need to launch some code when the value in this combo box
changes.
The OnChange event runs if the value is changed by selecting an item
using the mouse, but if I type in the combo box causing the value to
change, the On Change event does not run. How can I launch my code
by typing new values in the combo box?

The Change event (not the "OnChange" event, by the way) *does* run when
you type in the combo box. In fact, it runs for every keystroke you
make that affects the displayed text in the control. But there's a
difference in the way the Change and Click events work when you type in
the control vs. selecting a value with the mouse.

When you select a value in a combo box with the mouse, events fire in
this order:

BeforeUpdate -> AfterUpdate -> Click -> Change

When you type in the text area of a combo box, events fire in this
order:

Change (multiple) -> BeforeUpdate -> AfterUpdate -> Click

The Change event fires every time the text in the combo box changes,
which means for every keystroke except the Tab or Enter that terminates
your entry. Then the BeforeUpdate and AfterUpdate events fire,
reflecting the updating of the value of the combo box, and then a Click
event is tacked on at the end, even though you didn't really click it at
all.

The key point in this is that the Value property of the combo box
doesn't actually change until the BeforeUpdate event fires, so you can
type any number of characters into the text area and the value, if
checked in the Change event, will not have changed. This is true for a
text box, too, by the way. For this reason, the Change event is seldom
the event you want to use, unless you're really interested in each
individual character the user types in.

Most often, what you're interested in is a change to the *value* of the
combo box. For that, you should use the BeforeUpdate or AfterUpdate
event. Unless you intend to validate and possibly reject the user's
input, you should use the AfterUpdate event. You *could* use the Click
event, since that, too, only fires after the value has been updated.
However, for consistency with other controls, and because its name
corresponds most closely with the logical focus of your interest --
viz., an update to the control's value -- I recommend the AfterUpdate
event.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Just wanted to let you know I have applied both your recommendations and they
work like a charm. Many thanks.

Dirk Goldgar said:
(answers inline)

Sheldon Penner said:
I have two questions about combo boxes:

1. I need to have the entire text within a combo box selected when
the combo box gets the focus. I have experimented with vba code in
the OnEnter and the GotFocus events:

With Me.cboSelectAccount
.SelStart = 0
.SelLength = Len(.Text)
End With

This works if I tab into the combo box, but if I click on it, the
selection flashes for an instant, then disappears. How can I make
the combo select its full text upon entering via click?

This is actually not that easy to do, as the click is processed after
the combo box has the focus. I have found the following way to do it,
but I haven't tested it thoroughly:

1. Use the combo box's GotFocus event to select the text, using the code
you posted above.

2. Create a command button exactly the size and shape of the combo box's
text area (excluding the dropdown arrow). Place this command button on
top of the combo's text are so that it exactly covers it.

3. Set the following properties of the command button:

Transparent: Yes
Tab Stop: No
On Click: [Event Procedure]

4. Create an event procedure like the following for the command button's
Click event:

Private Sub cmdSelectCombo_Click()
Me.cboSelectAccount.SetFocus
End Sub

Now, if the user tries to click on cboSelectAccount, they'll actually
click on the transparent command button, which will "swallow" the click
and set the focus to the combo box, allowing the combo's GotFocus event
procedure to select the text and leave it selected.
2. I need to launch some code when the value in this combo box
changes.
The OnChange event runs if the value is changed by selecting an item
using the mouse, but if I type in the combo box causing the value to
change, the On Change event does not run. How can I launch my code
by typing new values in the combo box?

The Change event (not the "OnChange" event, by the way) *does* run when
you type in the combo box. In fact, it runs for every keystroke you
make that affects the displayed text in the control. But there's a
difference in the way the Change and Click events work when you type in
the control vs. selecting a value with the mouse.

When you select a value in a combo box with the mouse, events fire in
this order:

BeforeUpdate -> AfterUpdate -> Click -> Change

When you type in the text area of a combo box, events fire in this
order:

Change (multiple) -> BeforeUpdate -> AfterUpdate -> Click

The Change event fires every time the text in the combo box changes,
which means for every keystroke except the Tab or Enter that terminates
your entry. Then the BeforeUpdate and AfterUpdate events fire,
reflecting the updating of the value of the combo box, and then a Click
event is tacked on at the end, even though you didn't really click it at
all.

The key point in this is that the Value property of the combo box
doesn't actually change until the BeforeUpdate event fires, so you can
type any number of characters into the text area and the value, if
checked in the Change event, will not have changed. This is true for a
text box, too, by the way. For this reason, the Change event is seldom
the event you want to use, unless you're really interested in each
individual character the user types in.

Most often, what you're interested in is a change to the *value* of the
combo box. For that, you should use the BeforeUpdate or AfterUpdate
event. Unless you intend to validate and possibly reject the user's
input, you should use the AfterUpdate event. You *could* use the Click
event, since that, too, only fires after the value has been updated.
However, for consistency with other controls, and because its name
corresponds most closely with the logical focus of your interest --
viz., an update to the control's value -- I recommend the AfterUpdate
event.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Sheldon Penner said:
Just wanted to let you know I have applied both your recommendations
and they work like a charm. Many thanks.

Excellent. You're welcome.
 
Back
Top