checking multiple controls via click events

S

sylvian stone

Hi,

I've been trying to figure this problem out without any sucess.

I have a userform within a word document, and the form has about 100
checkboxes and a similar number of ComboBoxes. For example, if a
checkbox is called 'x', the combobox will be called 'x_combo'.

Currently, if I click on 'x', 'x_combo' is enabled, but if 'x' is
unchecked, 'x_combo' is disabled.

The code is below, but with about 100 controls, I need a similar number
of subs to validate each one.

Example:


Private Sub x_Click()

If report.x.value = True Then

report.x_combo.Enabled = True



End If

If report.x.value = False Then

report.x_combo.Enabled = False

End If

End Sub



Now, is it possible to trap a 'click' event and cycle through the
Me.Controls in order to validate each checkbox, and enable / disable
the combobox accordingly ? In fact, I know how I would do the
validation, but what I can't figure out a way to identify the fact that
a checkbox had been checked, without the click() sub being tied to a
specific control, as in the code above.....

Am I trying to do the impossible ?

Rgds
SS.
 
J

Jezebel

You can iterate all the controls on the form using code like

Dim pControl as Control

For each pControl in Me.Controls
...
Next

There are a lot of options for how to work out which is which: give them
names that are structured in some way; or set the .Tag properties at design
time; or use the TypeOf function; etc ...
 
S

sylvian stone

Thanks for the reply. What I guess I was trying to ask was how do you
get to the point where you can iterate over the controls.

Say I have control 'x' and control 'y'.

If I click on 'x' it triggers the sub 'x_click()' and if I click on 'y'
it triggers the sub 'y_click()'

Therefore, what I was trying to figure out was if it is possible to
avoid having 100 or so of the subs, and have one sub that would be
triggered if ANY control was clicked on, then cycle through the
controls as you outlined above....

Does this make any sense....
 
J

Jezebel

It makes sense. Unfortunately you've hit one of the main differences between
VBA and true VB. In VB you'd use a control array, so you'd have only one
piece of code. But in VBA ... :(
 
J

Jonathan West

You will need an event for each control. But each event can be a one-liner
which calls a common routine. In the common routine, you can identify the
selected control by using the ActiveControl property.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
H

Helmut Weber

Hi Sylvian,

you can create arrays of controls at runtime.

Google here for "array of controls"

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
H

Helmut Weber

....
no, not here, that is in the beginners' group, in

public.word.vba.general

Helmut weber
 

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