[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Combo Box OnClick Event Not Firing?????

Posted on 2008-11-17
29
Medium Priority
?
544 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Eric Sherman
  • 13
  • 9
  • 5
  • +1
29 Comments
 
LVL 20

Expert Comment

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

Scott C
0
 
LVL 19

Author Comment

by:Eric Sherman
ID: 22978018
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
0
 
LVL 20

Expert Comment

by:clarkscott
ID: 22978055
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 1000 total points
ID: 22978079
Try the Mouse Down Event:

Private Sub Combo1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Me.Combo1.SelLength = 0
    Me.Combo1.SelLength = Len(Me.Combo1)
End Sub

mx
0
 
LVL 75
ID: 22978092
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
0
 
LVL 19

Author Comment

by:Eric Sherman
ID: 22978284
>>>>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
0
 
LVL 75
ID: 22978408
"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
0
 
LVL 19

Author Comment

by:Eric Sherman
ID: 22978436
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

0
 
LVL 75
ID: 22978510
I'm seeing it happen right before my eyes.   Hummm.  What is the exact code you have?

mx
0
 
LVL 75
ID: 22978661
Well, sorry ... now it's NOT working ... weird.

mx
0
 
LVL 19

Author Comment

by:Eric Sherman
ID: 22978763
Hummm ....  I'm thinking is there some property setting that will also affect that process??

ET
0
 
LVL 75
ID: 22978872
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
0
 
LVL 19

Author Comment

by:Eric Sherman
ID: 22978874
>>>>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
0
 
LVL 75
ID: 22978918
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

0
 
LVL 19

Author Comment

by:Eric Sherman
ID: 22979055
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
0
 
LVL 75
ID: 22979139
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 ?
0
 
LVL 19

Accepted Solution

by:
Eric Sherman earned 0 total points
ID: 22979346
Same results as you got but you still get the cursor position in the field where the user clicked and not at the beginning.  When they start typing the text will be inserted where ever the pointer is which is not going to work.

Here's what I've found that seems to work with Combo Boxes .... Keying off your MouseDown Event .... I used the MouseUp Event instead which seems to work for the Combo's.

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

I will give you some points because your MouseUP Event pointed me in the right direction.

Thanks,

ET
0
 
LVL 75
ID: 22979383
"Same results as you got ..."

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

mx
0
 
LVL 11

Expert Comment

by:datAdrenaline
ID: 22982293
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
0
 
LVL 75
ID: 22982351
"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
0
 
LVL 11

Expert Comment

by:datAdrenaline
ID: 22982462
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)
0
 
LVL 75
ID: 22982523
"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
0
 
LVL 11

Expert Comment

by:datAdrenaline
ID: 22982560
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
0
 
LVL 75
ID: 22982614
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
0
 
LVL 11

Expert Comment

by:datAdrenaline
ID: 22982635
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 ... :) ...
0
 
LVL 75
ID: 22982652
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
0
 
LVL 19

Author Comment

by:Eric Sherman
ID: 22984481
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


0
 
LVL 11

Expert Comment

by:datAdrenaline
ID: 22985971
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! ...
0
 
LVL 19

Author Comment

by:Eric Sherman
ID: 22986634
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


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question