"Cloning" a control and its event code

C

cefrancke

I've read up on Access and the limits of creating visible controls at
run-time.
I'm using Access 2003 and assume that it cant be done.

I did find a clever method of having a main form (and in it's code)
open up a subform in design mode (hidden maybe) and then using the
CreateControl method, create controls.
Save the subform and set a subform control on the main form to point to
the subform (really a form) I just edited/designed.

What I would like to know is....

Can I have a control on that subfom, say a rectange (with all its code
I put in for click events), and "clone" it (along with the event code I
created) on the subform, of course multiple times as needed? So, that
the "cloned" rectangles also have the click event code I made for the
original.

This way I can create as many as I like and the functionality of each
rectangle is the same.


Thanks in advance for any advice.
 
D

Douglas J Steele

While it may be a clever method, is it really necessary? For one thing,
it'll mean you could never convert your application to an MDE.

There's no way to clone controls, but you can programmatically add code to
controls when you create them. Take a look at the CreateEventProc and
InsertLine methods of the Module object. There are also AddFromFile and
AddFromString methods that you might find useful.

Here's an example copied from the Help file:

Function ClickEventProc() As Boolean
Dim frm As Form, ctl As Control, mdl As Module
Dim lngReturn As Long

On Error GoTo Error_ClickEventProc
' Create new form.
Set frm = CreateForm
' Create command button on form.
Set ctl = CreateControl(frm.Name, acCommandButton, , , , 1000, 1000)
ctl.Caption = "Click here"
' Return reference to form module.
Set mdl = frm.Module
' Add event procedure.
lngReturn = mdl.CreateEventProc("Click", ctl.Name)

' Insert text into body of procedure.
mdl.InsertLines lngReturn + 1, vbTab & "MsgBox ""Way cool!"""
ClickEventProc = True

Exit_ClickEventProc:
Exit Function

Error_ClickEventProc:
MsgBox Err & " :" & Err.Description
ClickEventProc = False
Resume Exit_ClickEventProc
End Function

What many people do is create the maximum number of controls that might be
required, and toggle the Visibile property of those controls.
 
C

cefrancke

Thanks for the nudge Douglas,

I realize I can't protect my code, because it needs to actually design
in runtime.

I'm trying to convince the client to move to VB.net or C# where I could
instatiate a custom rectangle, but the cost is always an under
appreciated (misunderstood) issue.

But, in the situation where it is being used, code vulnerability is not
an issue (I hope!).

The following is a bit more detail of what I want ( I posted this
response in another group):

**************************************************************************************************
Sure Rick,

It's the same old story. I want to create a certain amount of
rectangles based on the amount of records in a table (which can vary)
and update the subform for any new or deleted records (also some other
info if the record has been changed, but not deleted).
But, unlike what everybody thinks, which is some kind of continuous
form of controls based on the records, I want to create a kind of
"Matrix", a nxn grid of rectangles.

So, the user has an interface, say the Main form, to
browse/update/delete the records (in a subform datasheet) and then
click a button that will generate the grid.
The grid will be on a form and shown in a subform control after
created.

Now, once the grid is generated, each rectangle should have the click
event code to respond to the user, who will click each rectangle as
needed and change the color of the rectangle clicked. There are
multiple colors which each mean something different.

When the user clicks a rectangle, a cooresponding record in the table
is updated.
If the user deletes or adds a record, a row of rectangles is added or
removed.

Mathematically (if I have this right) it is a "Cartesian Cross Product"
of the records in
a table with itself. Kind of like how a multiplication or addition
table looks.
The record id is at top and across, labeling each column in the grid,
and at the left
and down, labeling each row in the grid. The user marks the color of
each grid based
on the condition or meaning of combination of a row and column id
value.
Naturally, like squares in a multiplication table, the diagonal
rectangles would represent the pair (id,id).

So given a table with n records, create an nxn grid, labeling the top
and left with the record id, and allowing the user to click a rectangle
to change its color, update the table with the color value chosen after
the click event.

Now, one could, theoretically, upon creating the grid, calculate the
cross product pairs and populate another table, and with that other
table somehow generate each rectangle, of course with the same click
event features as above.


Hope this helps and thanks for responding,

Christopher
**************************************************************************************************
 
D

Douglas J Steele

I don't see anything in that description that precludes the possibility of
pregenerating rectangles, and only making as many as you need visible.

BTW, remember that you can create a function that looks for the active
control, and associate that one function with all of your controls. Select
all of them (click on each one while holding the shift key down, "lasso"
them, select a row or column from the ruler, or some combination of those
methods), then look at the Property dialog. Anything you type into an event
will apply to all of the selected controls. You can put something like
=MyFunction() for an event (include the equal sign and parentheses), and it
will fire when that event occurs for any of the controls that were selected.
You can find out which control was responsible using Me.ActiveControl.
 

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