Common OnChange Event Listener

S

Skullshock101

I have over 50 UNBOUND text boxes and combo boxes on a form. Instead of
using each control's OnChange event, I would like to know if it is possible
to write a common even listener for each of the text box and combo box
OnChange events. The OnDirty event does not work for unbound text
boxes/combo boxes.
 
A

Allen Browne

Are you writing a keylogger?

The Change event fires with each keystroke. I can't imagine a scenario where
you would want to respond to every keystroke in every control.

It is possible to loop through the controls and assign a function to an
event. This example opens a form in design view, and assigns
=MyFunc([EachControlNameGoesHere])
to the OnChange property of each text box and combo:

Sub SetupMyForm(strForm As String)
Dim ctl As Control
Dim strName As String

DoCmd.OpenForm strForm, acDesign
For Each ctl In Forms(strForm).Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
strName = ctl.Name
ctl.OnChange = "=MyFunc([" & strName & "])"
End Select
Next
Set ctl = Nothing
End Sub
 
B

bcap

For a start you could write a generic function and assign it to the "On
Change" property of each of the controls i.e. instead of the property being
set to "[Event Procedure]", you set it to something like this:

=MyGenericFunction()

It's a lot easier than creating 50 event procedures all doing the same
thing, even if that same thing is merely a procedure call!

If you want to avoid doing even that, you can create a class which "sinks"
the On Change event for a control. In the form's Open event, you would need
to iterate through the form's Controls collection, and for each control
where you want to sink the On Change event (identified how?) create an
instance of the aforementioned class and add it to a collection.
 
D

david

Change KeyPreview to 'Yes'. It is a property of the form, (found on the
Event tab).

Then use the form events: Key Down, Key Up, or Key Press.

(david)
 

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