Link to home
Start Free TrialLog in
Avatar of Millard Melnyk
Millard MelnykFlag for United States of America

asked on

No Click event for ComboBoxes when you first click them -- help!

In my never-ending quest to make things easy for my users, and for me as a coder trying to make things easy for my users, I actually figured out a way to auto-select a textbox's contents on the first click (only) into the control with one line of code in the control's Click event, thus without the need for logic flags for each such control to make sure auto-selection happens only on the first click in cluttering up the form's code.

All was well until I got to ComboBox controls.

On a ComboBox control, the Click event doesn't fire when you click into the control! WTF?

You have to drop the list down and click on a list item before the Click event will fire. And since it fires always and only after the AfterUpdate event, it's basically a waste of internal coding. Yet again another signature Microsoft design decision.

So if auto-content-selection fails when invoked in a control's GotFocus (true) leaving Click event as the only option, and the Click event doesn't fire on the first time clicking into a ComboBox, I'm screwed when it comes to them.

Is there a separate OnClick trigger for the textbox of a ComboBox?

Wow, the things those Microsoft design managers could have done to make us poor coders' lives easier, lol.
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

What are you really trying to do in the combo?

You could probably use the GotFocus event, but this will fire if the user tabs into it as well as if they click in it.

Dale
Please show us your code for to understand what you are doing.
On a ComboBox control, the Click event doesn't fire when you click into the control!
Why should it?

In my never-ending quest to make things easy for my users, ...
You deserve respect for this, but don't let it force you to hunt non-existing issues.
Avatar of Millard Melnyk

ASKER

@Dale Fye: Nothing mysterious, just a very common behavior such as is found in Word, OneNote, on Web sites, and various other places.

I want the contents of a textbox control automatically and completely selected on the first click into a control, but not on subsequent clicks while still in the control.


@Dale Fye:  I forgot to add, I'd love to use GotFocus but, in another twist in its endless array of quirks and puzzlements, Access fails to perform UNLESS IN DEBUGGING MODE, in which case it does exactly what I want. Set a breakpoint, sweet! No breakpoint, no joy. I can fill you in if you like, my question leading to that discovery was https://www.experts-exchange.com/questions/29197542/How-can-I-select-the-contents-of-a-textbox-automatically-when-the-user-enters-the-textbox-by-clicking-with-the-mouse.html 

@Gustav Brock: This is my fourth question on Experts Exchange, you have commented on every one of them, and to date you have been strong on editorializing and weak (almost nil) on value.

I'm the one paying for the privilege of picking some clever and experienced minds here.

Worse than giving me little to nothing for my money, ironically, you have often failed to understand what I was after (just like this example), but that hasn't seemed to temper your penchant for assuming that I'm the one who doesn't understand what's going after you failed to grasp my question.

To answer your "Why should it?" question -- simple: Cuz everybody else is doing it. If you're not aware of this very common GUI behavior (control content auto-selection first time in) then why are you here as an expert?

Or, alternatively, I could ask you: It's the damn OnClick event. I clicked into the control. Why would I NOT expect it to fire?

And sorry, but "... but don't let it force you to hunt non-existing issues" is just clueless.
@Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc: Here you go:

Private Sub cboTorQorAll_Click()
 
Debug.Print "cboTorQorAll_Click BEFORE SelectBoxContents(ActiveControl) & "}"

Call SelectBoxContents(ActiveControl)
 
Debug.Print "cboTorQorAll_Click AFTER  SelectBoxContents(ActiveControl) & "}"

End Sub

Open in new window

Here's the control's Property Sheet:

User generated image

Then I open the form, click the combo box, the cursor sits there blinking at me in the combo box, and the code does not execute.

ASKER CERTIFIED SOLUTION
Avatar of Dale Fye
Dale Fye
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
and a slight modification of that function will actually identify the first column actually displayed:
Private Function SelectContents()
   
    Dim ctrl As Control
    Dim colWidths() As String
    Dim intLoop As Integer
   
    Set ctrl = Screen.ActiveControl
    If IsNull(ctrl.Value) Then Exit Function
    ctrl.SelStart = 0
    colWidths = Split(ctrl.ColumnWidths, ";")
    For intLoop = LBound(colWidths) To UBound(colWidths)
        If Nz(colWidths(intLoop), "") <> "0" Then
            ctrl.SelLength = Len(ctrl.Column(intLoop))
            Exit For
        End If
    Next
   
End Function

Open in new window


OK, a little more sleuthing (an IDE's value being inversely proportional to the sleuthing it requires, lol,) I found that, since I had to do the extra coding to make my content-auto-selection capability work from the OnClick event anyway, it turns out that if I run my SelectBoxContents function from a control's OnMouseUp event it works great for both TextBoxes and ComboBoxes.

So although this solves my immediate need, since it allows me to avoid VBA's ComboBox OnClick anomaly, that by no means changes the fact that it's an anomaly.

And if anyone wonders why I continue using an IDE I persistently bash, that's easy.

I gave up coding in 2008. I never planned to return. The events of this year changed all that.

I would be using Visual Studio and coding in Python but for the high entrance cost (for me) of learning a new IDE environment on top of being new to Python on top of simultaneously teaching myself MQL4 (Metatrader's proprietary 90s-vintage version of C or C+, whichever,)  on top of the fact that my thousands of lines of utility code (like SelectBoxContents) developed in the 00s would need to be ported over, opening oodles of cracks for bugs that I'd then need to debug just to get to square one.

I don't have that kind of time.
just as another point of interest.

If you use a function, instead of a procedure, you can eliminate the need of opening the actual code module and simply call the function from the property window, see attached.  This allows you to select all of the combo boxes and assign this property to them all at once.  And if you modify your "SelectContents" function to test for the type of control, you could easily extend that one function to handle both textboxes and combos with a single function in a standard code module.

User generated image
@Dale Fye: Hey man, thanks, I saw your comments after I posted mine. I'm checking it out, will get back to you shortly.
@Dale Fye:  First, to answer your question, I'm using VB7 (64-bit) under an Office 365 Home license, which gives me Access 2016.
User generated image

So here's what happened when I inserted your code. I did it two ways.

1. Inserted the operable code into the GotFocus event procedure of my ComboBox named "cboTst"
2. Inserted the function as you provided it in a separate function procedure which I Call-ed from the GotFocus event.

Either way, it behaves in a way that's consistent with what I already reported in that other question: The text selection using .SelStart and .SelLength works, but then something clobbers it. If I break execution anywhere in the procedure containing those two lines of code, doesn't matter where they're located, the text selection stays intact. Otherwise (don't break execution or break it elsewhere after those two lines exectute) and the selection reverts to a blinking cursor.

Btw, the code as you supplied it didn't work, got me the following error:

User generated image
on this line:

User generated image
So I changed .Column(1) to .Text and it worked.


Here's a little blow-by-blow.

I took this screenshot after my initial click into the combo box but with the left mouse button still held down:

User generated image
Here's what happened immediately on letting the mouse button up (cursor blinking in the combo box's text box, although not visible here):

User generated image
So I don't doubt that something is wacky here. I should try it in a clean database. I'm not about to reinstall Office, lol.

As far as coding the SelectContents (or similar function) routine from the control's Property Sheet, I won't often use that method because I like using GotFocus for a wide variety of things (which is why it was the first event type I tried) precisely because it happens only once per entry into a control.
@Dale Fye: I tagged your response as the answer, even though it doesn't actually work for me, I believe it should and would if whatever glitch weren't interfering on my installation of VBA. Or maybe it's something less mysterious but just as esoteric as a conflict between libraries in my References list. I'm good to go at the moment, though, so I won't pursue it further unless it rears its mug somewhere else and freaks my code out, lol.

I'd love to just add a call to a 2-line SelectContents function in a control's GotFocus event, but for now I'll use my more elaborate method invoked from the MouseUp event. It works, so good enough for now.

Thanks again! I'd love to see what you think about my Navigation Pane sorting question.
@Dale Fye: Sorry, I completely missed your "why a combo box?" question.

There are many times when I have text in my copy buffer that is either a full or partial match for an item in a combo box. When you paste that text into the combo box's text box (don't drop the list down) it repositions the selection in the list to the first match, if any. If there is already a value displayed in the combo box's text box, simply clicking into the box and pasting the copy buffer contents will insert the text, not replace what's already there.

Another case is when I have a looooong list in a combo box (like, say, all the employees in a company) and I want to skip to the T's or the W's. If I type "T" into the combo box's text box it pops the value of the first qualifying item into its text box automatically selected (which is exactly the feature I'm looking for on entry to the control) except for the character I typed (a fairly complicated feature I'd say) because why? Because if I keep typing it keeps shifting the selection to the first item that qualifies for all the characters I typed, adding the remaining characters automatically selected.

This behavior does not happen if the text box already has contents before I click into it, forcing me to double-click it to fully select it -- the very nuisance I'm trying to avoid.
I can understand that, regarding the use case for a combo box.

I can understand why my code would have failed for a textbox, as I did not do any modifications to account for the control type.  That code was only for a combo box.  Did you test the second set of code?  That should work, it did for me.

Dale
I see what you are talking about.  I was tabbing into the control and it worked fine.  So, put that code in the mouseup like you indicated.  But that won't solve the issue if you tab into the control, so you might want to leave it on the GotFocus and add it to the MouseUp events.
Well, tabbing into the control defaults to contents-selected, no code required, so no need in that case.

So you're saying it does the same thing at your end? That's good to know. I was wondering if it was a glitch here. Access regularly (every other day) goes belly up, automatically backs up and repairs my database, sometimes in at the most inexplicable moments. I worry that the database gets more and more frazzled. I've already deleted everything and imported it back in from a copy, just to try to get it to fix what's broke. This problem has been a nuisance (and an aggravation more than once -- completely hosing the database) ever since my early days of love-hate relationship with Access. I once had to copy-n-paste all the code and rebuild all the forms from scratch to get back to square one with one such clobbered DB.
I've never even tried, I generally like to keep my sort order by name, ascending.  About the only  time I sort by date (modified or created) is when I have another developer also making changes to the FE.

I'm hoping one of the other experts will chime in.
"Well, tabbing into the control defaults to contents-selected, no code required "

Not necessarily, that is an option, not sure where to find it in 2016, but in 2007 and 2010 it is in the Options => Advanced => Behavior entering field.
Thanks Dale. I use descending because my eyes start to cross and I regularly confuse queries when working strictly by name. I have 238 queries in my little app, and by the time I'm done I'll probably have 2-3 times more, so naming conventions are really important, but I can only do so much within the space Access gives you to work with. So when working on a series of interrelated queries, it helps to group them all at the top of the list, especially if their names would otherwise spread them hither and yon. All my File Explorer windows default to descending Date Modified, too, a standing "most recent" feature that I can change with a click or two of the column heading label buttons.

On the tabbing thing, right you are! I'd forgotten I'd set that long ago (meaning months, lol). And I'll remember that users could maybe "break" my little darlin' merely by changing that option. Good catch.
You know you can create custom groups, right? And assign any type of object to a group.
Yes, I'm aware, thanks. I checked them out awhile back, but they are too rigid to help me. And groups are not sensitive to changes in mod date or creation date. I'm not sure what happens if you rename a table or query. I rename queries all the time as I get the data sorted out.