Passing Name of Control to a Function

S

Sprinks

I have a matrix of controls, and would like to write a custom function that
would calculate values for another control, depending on the control's name.

For example, I have a series of calculated controls r1, r2, r3, etc. While
I use the ControlSource = MyFunction("r1") for the first, = MyFunction("r2")
for the 2nd, etc., Because there are so many, I'd prefer to assign them all
at once = MyFunction(some reference), such that Access sends the name of the
control to the function.

Is this possible?

Thank you.

Sprinks
 
A

Allen Browne

If the control has focus at the time, you might use Form.ActiveControl.Name
to determine which control it is.

If not, you can open the form in design view, and loop through the numbers,
setting the ControlSource programmatically, e.g.:
For i = 1 to 99
With Me.("r" & i)
.ControlSource = "=MyFunction(""r" & i & "")"
End With
Next

You probably realize that lots of controls with similar names often suggests
a non-normalized design.
 
S

Sprinks

Hi, Allen.

Thank you for the suggestion; that's what I ended up doing successfully.

I understand regarding the design; it's not really a database application.
It's a very unusual game application that would be simpler to create in
Excel, but is being done in Access to take advantage of its more flexible
Conditional Formatting.

Best regards.
Sprinks
 
Top