By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,356 Members | 2,752 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,356 IT Pros & Developers. It's quick & easy.

"Cloning" a control and its event code

P: n/a
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.

Jun 2 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I'd say that on a subform, if your rectangle is in the detail section, and
if your subform is in continuous or datasheet view, then your rectangle will
naturally propagate to each "instance" of the detail section. In other
words, for every record in the subform, you will naturally see the whole
detail section laid out identically.

I'm having a hard time understanding why you might need to clone a rectangle
and its events. Can you say more about why you need to do this? You might
not have to take this approach.
Jun 2 '06 #2

P: n/a
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

Rick Wannall wrote:
I'd say that on a subform, if your rectangle is in the detail section, and
if your subform is in continuous or datasheet view, then your rectangle will
naturally propagate to each "instance" of the detail section. In other
words, for every record in the subform, you will naturally see the whole
detail section laid out identically.

I'm having a hard time understanding why you might need to clone a rectangle
and its events. Can you say more about why you need to do this? You might
not have to take this approach.


Jun 2 '06 #3

P: n/a
If there's any way of knowing ahead of time the maximum number of
rectangles, you might be better off creating all of them ahead of time in
design view. Then hide or reveal them based on your data.

Access is pretty object oriented, but it's not enough so, I think, for you
to be able to create your subform as an object and then create controls as
objects and add them to the collection as you could in flavors of C or VB
these days.

If having them all, and hiding or revealing, is good enough, you would be
able to load up the tag property of each rectangle with values needed to
accomplish the task related to that rectangle. As you click even, use
"=MyClickEvent()".

IN your module (subform, down where the rectangles are) you need a public
function named MyClickEvent. It would refer to Screen.ActiveControl to get
to the clicked rectangle. E.G.:

dim c as control
set c = screen.activecontrol
c.backcolor = vbwhite
if c.tag = "You Win" then
msgbox "Hot dog!"
else
msgbox "Try again"
endif

Would that help you?

By the way, if you're not showing multiple records, why put this in a
subform? There are plenty of other reasons, but if it's okay I'd like to
ask yours.
Jun 2 '06 #4

P: n/a
Thanks Rick,

I was using a subform in order to open it hidden in design mode, create
all the rectangles, save, then show the subform in a subform control on
the Main form. Another subform on the Main form will have the records
to create the rectangles by.

Hope this helps,

Christopher

Rick Wannall wrote:
If there's any way of knowing ahead of time the maximum number of
rectangles, you might be better off creating all of them ahead of time in
design view. Then hide or reveal them based on your data.

Access is pretty object oriented, but it's not enough so, I think, for you
to be able to create your subform as an object and then create controls as
objects and add them to the collection as you could in flavors of C or VB
these days.

If having them all, and hiding or revealing, is good enough, you would be
able to load up the tag property of each rectangle with values needed to
accomplish the task related to that rectangle. As you click even, use
"=MyClickEvent()".

IN your module (subform, down where the rectangles are) you need a public
function named MyClickEvent. It would refer to Screen.ActiveControl to get
to the clicked rectangle. E.G.:

dim c as control
set c = screen.activecontrol
c.backcolor = vbwhite
if c.tag = "You Win" then
msgbox "Hot dog!"
else
msgbox "Try again"
endif

Would that help you?

By the way, if you're not showing multiple records, why put this in a
subform? There are plenty of other reasons, but if it's okay I'd like to
ask yours.


Jun 2 '06 #5

P: n/a
Rick,

Is there another way to get the id of the rectangle clicked in the
subform?
Apparently there is no focus for rectangle so Screen.ActiveControl is
not working.

I may have to hard code names or use mouse coordinates, any ideas?

Thanks

ce*******@yahoo.com wrote:
Thanks Rick,

I was using a subform in order to open it hidden in design mode, create
all the rectangles, save, then show the subform in a subform control on
the Main form. Another subform on the Main form will have the records
to create the rectangles by.

Hope this helps,

Christopher

Rick Wannall wrote:
If there's any way of knowing ahead of time the maximum number of
rectangles, you might be better off creating all of them ahead of time in
design view. Then hide or reveal them based on your data.

Access is pretty object oriented, but it's not enough so, I think, for you
to be able to create your subform as an object and then create controls as
objects and add them to the collection as you could in flavors of C or VB
these days.

If having them all, and hiding or revealing, is good enough, you would be
able to load up the tag property of each rectangle with values needed to
accomplish the task related to that rectangle. As you click even, use
"=MyClickEvent()".

IN your module (subform, down where the rectangles are) you need a public
function named MyClickEvent. It would refer to Screen.ActiveControl to get
to the clicked rectangle. E.G.:

dim c as control
set c = screen.activecontrol
c.backcolor = vbwhite
if c.tag = "You Win" then
msgbox "Hot dog!"
else
msgbox "Try again"
endif

Would that help you?

By the way, if you're not showing multiple records, why put this in a
subform? There are plenty of other reasons, but if it's okay I'd like to
ask yours.


Jun 2 '06 #6

P: n/a
Of course you're right. I forgot about rectangles. Can you use textboxes
with appropriate back and fore colors instead? Text boxes can receive
focus, whereas rectangles, naturally, cannot.

Sorry about that.

Text boxes should fix you up.
Jun 2 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.