assigning functions to events dynamically

J

Jesper F

In some of my controls I assign a function to fx. the
OnMouseUp-event by writing =functionname() in the
property sheet.
However, I'd like to do it dynamically, but can functions
be assigned to controls' events through code?
For example with something like:
me!controlname.OnMouseUp = "=functionname"

It doesn't seem to work though.
Thanks for any input.
 
A

Allen Browne

Yes, you can do that by opening the form in design view (hidden if you
wish).

This example:
- opens the named form in design view;
- cycles through all text boxes and combo boxes;
- assigns the OnGotFocus and OnLostFocus properties to a generic function;
- passes the name of the control as an argument to the function.

Function SetupHightlight(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.OnGotFocus = "=HighLight([" & strName & "], True)"
ctl.OnLostFocus = "=HighLight([" & strName & "], False)"
End Select
Next
Set ctl = Nothing
End Function
 

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