ComboBox Problem - How's the best way to do this?

P

Patrick Pirtle

I have a number of combo boxes with associated code
that I want to run when something is selected. The
amateurish way I've been doing this is to set a global
boolean variable that doesn't get set to True until the
combobox is fully populated. As I step through my
code, I can see that each entry into the combobox
runs the ComboBox_Change code, but nothing happens
until the runOnce boolean is set:


Public runOnce As Boolean

Sub Main
'---->Populate the combobox
runOnce = True
End Sub

Private Sub ComboBox_Change()
If runOnce Then
'---->Do Stuff
End If
End Sub

How SHOULD I be accomplishing this? TIA for any
help and suggestions.
 
S

Saga

Actually, the global flag technique is the only way to
prevent event code from executing when the event
is fired by some initialization code (or other kind of
code where you don't want to execute the event's
code).

Amateurish? Yup, but hey it works and is the simplest
way to do it.

If anyone has any other way, I for one would also be
interested :)

Saga
 
G

George Nicholson

Change should be Click... unless the OPs trapping keystrokes.

What if the user tabs into the combo, makes a change, tabs out?

Maybe I've missed something but I'd suggest the control's AfterUpdate event
which only fires when the control loses focus *if* its value has changed
(i.e., the user has made a new selection).

HTH,
 
P

Patrick Pirtle

Thanks for the reply. My problem is that the AfterUpdate
doesn't fire until the control loses focus. I need the the sub
to fire each time the user makes a selection, but before the
combo loses focus, and only after the combo is fully
populated.
 
G

George Nicholson

Actually, the Update events fire whenever the selection in a combo changes,
losing focus is not a requirement. Even so, they fire before LostFocus does
(one of your requirements). Since Before Update can be canceled, it could
be used to prevent the focus from changing, if desired.

HTH,
 
K

Ken Halter

George Nicholson said:
What if the user tabs into the combo, makes a change, tabs out?

Regardless of when they make the change, Click will fire if ListIndex
changes.
Maybe I've missed something but I'd suggest the control's AfterUpdate
event which only fires when the control loses focus *if* its value has
changed (i.e., the user has made a new selection).

Must've missed something... since this is a VB Classic group, the combo
boxes we use don't have an AfterUpdate event.... not intrinsic combo's
anyway.
 
G

George Nicholson

Regardless of when they make the change, Click will fire if ListIndex

Even if they use the keyboard (no mouse, no click) to make the change?
Must've missed something... since this is a VB Classic group, the combo
boxes we use don't have an AfterUpdate event.... not intrinsic combo's
anyway.

A bit of Multi-posting confusion: I'm reading & posting to
Microsoft.public.office.developer.vba. Didn't notice the multi-post until
you mentioned it. Not sure what exactly the OP is using.
 
K

Ken Halter

George Nicholson said:
Even if they use the keyboard (no mouse, no click) to make the change?

Yes... No matter what. If you have VB handy, start a new project, add a
combobox, paste and run this code. You can use the arrow keys, mouse, any
other method, click still fires.
'========
Option Explicit

Private Sub Form_Load()
Combo1.Clear
Combo1.AddItem "Item 1"
Combo1.AddItem "Item 2"
Combo1.AddItem "Item 3"
Combo1.AddItem "Item 4"
Combo1.AddItem "Item 5"
End Sub

Private Sub Combo1_Click()
Debug.Print "'Form1:Combo1_Click", Timer
End Sub
'========
A bit of Multi-posting confusion: I'm reading & posting to
Microsoft.public.office.developer.vba. Didn't notice the multi-post until
you mentioned it. Not sure what exactly the OP is using.

I didn't notice either <g>... btw, that's "cross posting" and not "multi
posting" (at least in VB it is ;-) Eventually, we'll find out what he's
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top