Link to home
Start Free TrialLog in
Avatar of Eric Sherman
Eric ShermanFlag for United States of America

asked on

Combo Box OnClick Event Not Firing?????

I have a Combo Box on a Form and all I need to do is when the Combo Box is Clicked, Got Focus or On Enter, to simply select the entire field.  I already have the db options set such that if the user Tabs into the field it will be selected.

I have tried the following code in OnClicked, GotFocus and OnEnter to no avail....  The only time the OnClick evet fires is when a selection is actually made in the combo box which doesn't seem correct to me.

Private Sub Combo12_GotFocus()
Me.Combo12.SetFocus
Me.Combo12.SelStart = 0
Me.Combo12.SelLength = Len(Me.Combo12)
End Sub


Thanks,

ET
Avatar of clarkscott
clarkscott
Flag of United States of America image

That's how combo boxes have been programmed - the On Click is when you click on a drop-down selection.

Scott C
Avatar of Eric Sherman

ASKER

Correct, thay's why I moved the event to the OnEnter handler.  Still when I enter the Combo Box using a mouse click, nothing is selected.

ET
Well, I would use Got Focus since this works no matter how you get into a control.
but....
that doesn't mean your code will run as you intend.

Is there a value already in the combo box - or is it mechanically waiting for you to select something.

Try this... put a STOP in the code in the different events and run your app.  Where ever the stop actually stops - that's where the code should be.

next... examine the value of the combo box.  use the immediate window and 'print me.YourComboBox"

Do you get a value????

Scott C
SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
When you click into the combo box's text area (not the drop down arrow), the entire text will be selected.  That's what you want, right ?

mx
>>>>When you click into the combo box's text area (not the drop down arrow), the entire text will be selected.  That's what you want, right ?<<<<

Yes, exactly.  If the user tabs into the field the entire text is selected.  Then they just start typing to use the Auto Expand, etc.  When the field is clicked to enter it, I'm trying to have the text selected as well.

I will give it a try.  Trying to figure out why GotFocus or OnEnter will not do the same.

ET
"Trying to figure out why GotFocus or OnEnter will not do the same."

I could *not* get either of those to work!  Kind of weird ... but, Mouse Down seems to do it.

mx
MX ...

Still doesn't work when the Combo Box is clicked.  If I tab into the combo box it does exactly what I'm trying to accomplish.  Problem is the users will click the field not tab into it.

clarkscott ...

It's just a combo box with two fields Item Description and Item Number.  Item Number is the bound field and Item Description is the displayed field.  I agree, the Got Focus Event should be used to trigger this event.  When the form first opens the value will be null and once the user makes a selection the value will be the selected Item Number.  This combo box is used to drive a search function on the form.  
All I need to do is when the box is clicked with the mouse have the same thing happens as when the user tabs into the combo box.  

ET

I'm seeing it happen right before my eyes.   Hummm.  What is the exact code you have?

mx
Well, sorry ... now it's NOT working ... weird.

mx
Hummm ....  I'm thinking is there some property setting that will also affect that process??

ET
Well, this clearly works in a Text box:

Private Sub text1_Click()
    Me.Text1.SelStart = 0
    Me.Text1.SelLength = Len(Me.Text1)
End Sub

but ...  as CS said, the On Click does not trigger in a combo until you make a selection - which I guess I have forgotten, because that seems .... ODD !  

mx
>>>>Well, sorry ... now it's NOT working ... weird.<<<<<

If I do this then the mouse down event will select the text but with two problems.

1.) I can't use the message box popping up.
2.) The text is selected but the cursor will be position where ever the user clicked in the field.  I need the coursor to be at th beginning to the field so the user can start typing.


Private Sub Combo12_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.Combo12.SetFocus
Me.Combo12.SelStart = 0
Me.Combo12.SelLength = 101
MsgBox "MouseDown Event"
End Sub


ET
OK ... this *seems* to work ... using the Text property:

Private Sub Combo1_Enter()
    Me.Combo1.SelLength = 0
    Me.Combo1.SelLength = Len(Me.Combo1.Text)  '  TEXT
End Sub

mx

Still doesn't work MX ...

What's strange is I can Right-Click Combo Box and it will select it as desired without any code but you get the short cut menu plus the users will not right-click.  They want to just click in the field like all the others.

ET
Way too STRANGE.

IF ... I have this:

Private Sub Combo1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
   
End Sub

and


Private Sub Combo1_Enter()
    Me.Combo1.SelLength = 0
    Me.Combo1.SelLength = Len(Me.Combo1.Text)  '  TEXT
End Sub

It works ... EVEN THOUGH this is NO code in the Mouse Down event.  If I remove the Mouse Down event - it stops working !!!  

What do you get ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"Same results as you got ..."

Which of course makes NO sense, lol.  Yes, I see now the cursor is where you click.

mx
I know the disposition has been made, but I wanted to provide a bit of info ... the OnEnter and such all fire just fine ... even a virtual duplicate (note, the combo I used was named ShopID) does as it is supposed to ... {if you set a breakpoint on End Sub, you will see your text highlited as you would expect}

Private Sub ShopID_Enter()
    Me.ShopID.SelStart = 0
    Me.ShopID.SelLength = Len(Me.ShopID.Column(1) & "")
End Sub

Notice that I used the second column of the combo to determine length of the selection .. I think that is how you should do it too after reading the description of your combo .. but, back to what is happening .... the reason you see the cursor end up where you clicked, and no selected text, is for one simple reason ... the Mouse click has to be handled, and it is handled by Access, which will set the cursor position where clicked when clicking on a text/combo control.  As such, the clicking on the control occurs AFTER the OnEnter, OnClick .. etc ... Now the reason why MX's solution where he (assuming MX is a He ... sorry if my assumption is incorrect) had an "Empty" MouseDown event worked is simple ... the MouseDown event is now sunk (handled) by VBA instead of the OS & Access ... So .... to me MX's solution, which is basically two event handlers is the "proper" one ... so to recap the event handler code should look something like this ....

Private Sub ShopID_Enter()
    Me.ShopID.SelStart = 0
    Me.ShopID.SelLength = Len(Me.ShopID.Column(1) & "")
    Me.ShopID.Dropdown
End Sub

Private Sub ShopID_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    'Do Nothing
End Sub

One thing you will notice though is that the cursor is still blinking on where you click .. its just that the entire text is now selected .. :)

So .. in summary, its all about which component is handling the events! ... VBA or Access
"assuming MX is a He "
The icon is correct, lol.

I guess the MouseDown 'sunk' would make sense.  However, why do you suppose a text box does not have this issue and the combo does?

Also, I would probably skip the DropDown line, as this would defeat the purpose of doing the look ahead once the text is selected.

mx
Hello Mx ...

Thanks for the confirmation on the gender! ... LOL {just for the record ... my icon is correct too!}
----
I definate agree on the DropDown line .. I only included because the OP did ...
----
>> However, why do you suppose a text box does not have this issue and the combo does? <<

Well ... the textbox does have this same issue (using the Enter event) {note Area is a text box control} ...

Private Sub Area_Enter()
    Me.Area.SelStart = 0
    Me.Area.SelLength = Len(Me.Area)
End Sub

Fires/executes just fine .. but the mouse click applied by Access afterword unselects the text.  So I had to sink the MouseClick event on that too ....

Private Sub Area_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    'Do Nothing
End Sub

The text boxes OnClick event handles it well though ... SO .. a GUESS is that since the combo box has different "actions" that can be done when it is clicked upon (ie: drop down or just enter), the handling of the mouse click (by Access) is different ("expanded" perhaps) on a combo box compared to a text box, and with that difference, the OnClick events of the respective controls are probably different in what actions get suppressed/sink (ie: a mouse click)
"Well ... the textbox does have this same issue (using the Enter event) {note Area is a text box control} ... "

I don't get that result.  

Private Sub Text1_Enter()
    Me.Text1.SelStart = 0
    Me.Text1.SelLength = Len(Me.Text1)
End Sub

Selects the entire text and puts the cursor at the beginning of the text, no matter where I click.

mx
Well thats interesting .... I have attached a db to this post ... OnEnter for the text field MyTextField .... the behavior is consistent with what has been described for the combo .... Do you have any other events (ie: OnClick) on the text box you tested??? ...
OnEnter.mdb
Sorry ... it's the On Click event

Private Sub text1_Click()
    Me.Text1.SelStart = 0
    Me.Text1.SelLength = Len(Me.Text1)
End Sub

And that is the only Event hooked.

mx
Yeah .... that is what I was saying about how Access handles the click event when over a text box or combo box ... when you click on a combo, more stuff can happen (drop down or select) so it is my ASSUMPTION that Access has the OnClick of a text box "mask" the system mouse click event ... but with a combo, since you can click to drop down the control, that same stategy could not be employed .... but that is truely just a guess ... and the behavior we are seeing could be just a bug .. er.. ummm ... I mean feature ... :) ...
Sounds like your guess is correct.  I have never tried or need to do this on a combo box before ... but now I know.

thx.mx
Thanks for the additional input datAdrenaline ....  I understand your comments but after testing every suggestion here as well as other combinations, the only code that seems to do exactly what I'm trying to accomplish is shown below using the MouseUp Event.  

Private Sub Combo12_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.Combo12.SelStart = 0
Me.Combo12.SelLength = 101
End Sub

I should clarify that I'm not just trying to highlight the text, I want to select that entire combo box when clicked which is the same as when a user tabs into the field.  This combo box drives a product search function.  Therefore, the user will click the field then start typing Bleach ..... so that the Auto Expand will navigate directly to ...

Bleach - 5 Gal
Bleach - 55 Gal
Bleach - Sticks

If you just select the highlighted text (which could vary in length) the Auto Expand does not work.  Yes, it's selected and when the user types it overwrites the selected text but the Auto Expand does not fire unless the entire field is selected.  101 is the size of the Item Description field in the table.

Basically, when using the mouse click, the code above duplicates the exact same behavior as if the user tabbed into the field.

ET


Yes mouse up is probably the best event to use ... but, and I am not trying to be a pain ... its not because all the text is not selected ... it has to do with where the cursor is after all the events have been handled (by Access or VBA)

One of my comments was ...
>> One thing you will notice though is that the cursor is still blinking on where you click .. its just that the entire text is now selected .. :) <<

That was for the "Enter" event with an empty Mouse Down event ... apparently the Mouse Up event (which was unhandled, in Mx and my cases) is still doing something to the control ... so with you having VBA handle the Mouse Up (with is presumably the LAST mouse event that needs to be handled), all is good ...

Now as to the varying length of text ... the way you described your situation (Column Widths property of 0;<some number> ... to accomodate for the varying length of text you need to look as the 2nd column (.Column(1)) ...

Me.SomeCombo.SelLength = Nz(Len(Me.SomeCombo.Column(1)), 255)

... But ... that is just me ... just setting to 101 obviously hilites the text ... If you are going to use a fixed number, I would just set it to 255 simply to accomodate a possible change in filed length.
....
On another note ... if you wanted to avoid code you could just click on the label that is "Associated" to the combo box ... The reason I say that is because I like the default click behavior ... what if the value I have in the combo is "Bleach - 5 Gal", and now I want "Bleach - 55 Gal" ... keeping the default behavior, I can click to the right of the 5 and just type 5 and now I have my new selection.... But, if I want too completely hilite, I just click on the associated label ... Just a thought.
.......

By the way ... really interesting stuff here if you ask me! ...
Thanks datAdrenaline ...  

ITA with your comments especially setting the length to 255.

>>>>By the way ... really interesting stuff here if you ask me! ... <<<<

Yes, condisering I thought the Combo Box would react the same as a regular Text Box.  Oh well ... you learn something new everyday.

ET