Dynamic Function Calls

G

Grwffyn

I have created a matrix of command buttons as shown in the code below. I have
a function with two parameters that are based on the command button name and
position in the matrix. When I call the ClickEventProc (ctl, intY) function,
it creates the Click procedure and the function call but then I get a window:
Run-time error '29054' Microsoft Office Access can't add, rename, or delete
the control(s) you requested.

Is there a way to assign a click function dynamically?
Woody

**Code that creates the controls:
For intY = 0 To intTotal

Set ctl = CreateControl("frm__test_form", acCommandButton)
ctl.Name = "ctl" & intY
ctl.Caption = varArray(intY)
ctl.Top = 1000 * (intY + 1)
ctl.Left = intLeftEdge
ctl.Height = 288
ctl.Width = 720
ctl.Visible = True
bCtl = ClickEventProc(ctl, intY)

Next intY

**Code that assigns a click procedure
Function ClickEventProc(ctl As Control, intY As Integer) As Boolean

Dim mdl As Module
Dim lngReturn As Long
Dim frm As Form
Set frm = Form_frm__test_form

Set mdl = frm.Module

lngReturn = mdl.CreateEventProc("Click", ctl.Name)
mdl.InsertLines lngReturn + 1, "findDisc(" & intY & "," & intY & ")"
ClickEventProc = True

End Function

**Resulting procedure with the "findDisc(0,0)" in red when the error window
comes up.
Private Sub ctl0_Click()
findDisc(0,0)

End Sub
 
G

Grwffyn

Marsh,

This is a one-time (whenever the user wants a change to the matrix)
procedure that can only be accessed by the programmers.

The function call has to change for each button so it would be

findDisc(0,0) for the button named ctl0_0
findDisc(0,1) for the button named ctl0_1
etc.

I tried to use: "=findDisc(" & inty & "," & inty & ")" since I want each
button to have different parameters for the function.

Woody

Marshall Barton said:
Grwffyn said:
I have created a matrix of command buttons as shown in the code below. I have
a function with two parameters that are based on the command button name and
position in the matrix. When I call the ClickEventProc (ctl, intY) function,
it creates the Click procedure and the function call but then I get a window:
Run-time error '29054' Microsoft Office Access can't add, rename, or delete
the control(s) you requested.

Is there a way to assign a click function dynamically?
Woody

**Code that creates the controls:
For intY = 0 To intTotal

Set ctl = CreateControl("frm__test_form", acCommandButton)
ctl.Name = "ctl" & intY
ctl.Caption = varArray(intY)
ctl.Top = 1000 * (intY + 1)
ctl.Left = intLeftEdge
ctl.Height = 288
ctl.Width = 720
ctl.Visible = True
bCtl = ClickEventProc(ctl, intY)

Next intY

**Code that assigns a click procedure
Function ClickEventProc(ctl As Control, intY As Integer) As Boolean

Dim mdl As Module
Dim lngReturn As Long
Dim frm As Form
Set frm = Form_frm__test_form

Set mdl = frm.Module

lngReturn = mdl.CreateEventProc("Click", ctl.Name)
mdl.InsertLines lngReturn + 1, "findDisc(" & intY & "," & intY & ")"
ClickEventProc = True

End Function

**Resulting procedure with the "findDisc(0,0)" in red when the error window
comes up.
Private Sub ctl0_Click()
findDisc(0,0)

End Sub


First, you can not do any of that unless the form is open in
design view (which is a really bad thing to do in a running
application). What you are doing is only appropriate if
this code is part of a design time, one use, wizard.

If you have opened the form in design view somewhere else in
your code, then I'm fairly sure that you should refer to the
form's module as:
Set frm = Forms!frm__test_form.Module

Are you aware that you do not have to create a separate
event procedure for each button? Instead of creating all
the procedures and setting the OnClick property to:
[Event Procedure]
you can just set the OnClick property to:
=findDisc(0,0)
 
M

Marshall Barton

Grwffyn said:
I have created a matrix of command buttons as shown in the code below. I have
a function with two parameters that are based on the command button name and
position in the matrix. When I call the ClickEventProc (ctl, intY) function,
it creates the Click procedure and the function call but then I get a window:
Run-time error '29054' Microsoft Office Access can't add, rename, or delete
the control(s) you requested.

Is there a way to assign a click function dynamically?
Woody

**Code that creates the controls:
For intY = 0 To intTotal

Set ctl = CreateControl("frm__test_form", acCommandButton)
ctl.Name = "ctl" & intY
ctl.Caption = varArray(intY)
ctl.Top = 1000 * (intY + 1)
ctl.Left = intLeftEdge
ctl.Height = 288
ctl.Width = 720
ctl.Visible = True
bCtl = ClickEventProc(ctl, intY)

Next intY

**Code that assigns a click procedure
Function ClickEventProc(ctl As Control, intY As Integer) As Boolean

Dim mdl As Module
Dim lngReturn As Long
Dim frm As Form
Set frm = Form_frm__test_form

Set mdl = frm.Module

lngReturn = mdl.CreateEventProc("Click", ctl.Name)
mdl.InsertLines lngReturn + 1, "findDisc(" & intY & "," & intY & ")"
ClickEventProc = True

End Function

**Resulting procedure with the "findDisc(0,0)" in red when the error window
comes up.
Private Sub ctl0_Click()
findDisc(0,0)

End Sub


First, you can not do any of that unless the form is open in
design view (which is a really bad thing to do in a running
application). What you are doing is only appropriate if
this code is part of a design time, one use, wizard.

If you have opened the form in design view somewhere else in
your code, then I'm fairly sure that you should refer to the
form's module as:
Set frm = Forms!frm__test_form.Module

Are you aware that you do not have to create a separate
event procedure for each button? Instead of creating all
the procedures and setting the OnClick property to:
[Event Procedure]
you can just set the OnClick property to:
=findDisc(0,0)
 
M

Marshall Barton

Grwffyn said:
This is a one-time (whenever the user wants a change to the matrix)
procedure that can only be accessed by the programmers.

The function call has to change for each button so it would be

findDisc(0,0) for the button named ctl0_0
findDisc(0,1) for the button named ctl0_1
etc.

I tried to use: "=findDisc(" & inty & "," & inty & ")" since I want each
button to have different parameters for the function.


That should work:

frm("ctl" & intx & "_" & inty) = _
"=findDisc(" & intx & "," & inty & ")"

If findDisc is in a standard module, make sure it's Public
 
G

Grwffyn

Marsh,

Thanx! It worked great. I used the following code in the On_Open

With Me("ctl" & intX & "_" & intY)

.OnClick = "=findDisc(" & intX & "," & intY & ")"

End With

Woody
 

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