Copy a Control in Form Design (1 Viewer)

MLUCKHAM

Registered User.
Local time
Today, 05:57
Joined
Jul 23, 2013
Messages
89
Looking for a bit of short cut code if anyone has it.

I have to do some tricky automatic form building stuff. I need to duplicate a number of controls on a form. They are all acCommandButton types, but they have different images, sizes etc.

Anyway, my thought process was if I have a "template" acCommandButton then when I create my new controls if I could use this template then the code I have hanging off them (I have a function being called on the onClick event, borders are set to hairline, themes turned off and various other settings) would be copied. Save me trying to set them all individually.

So, my question is, can I "copy" a control in form design through VBA. Once I create the copy of the control I can then change the couple of properties I need to and "hey presto" I have skipped a load of settings that I don't need to set.

At the end of the process when I rename my form I can delete my template control and I have a working form.

Any ideas from you wizards?
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 14:57
Joined
Jan 20, 2009
Messages
12,853
I would just copy and paste the template control using the clipboard. Then select those which need the same properties changed to the same and edit them.

If you need a lot of them, copy and paste the first one. Then select the two and copy and paste. Then select four and so on will quickly exponentiate.

It is certainly possible to make as many as you want using a script but it take a while to set up, especially creating the event procedures. It would need to be something you reused repeatedly.
 

MLUCKHAM

Registered User.
Local time
Today, 05:57
Joined
Jul 23, 2013
Messages
89
This has to be code based. I am working on a solution and will post it here if I get there...
 

missinglinq

AWF VIP
Local time
Today, 00:57
Joined
Jun 20, 2003
Messages
6,423
Perhaps if you explained why you have to do this through code it would help us to help you. Designing Forms in this manner is seldom appropriate, and fraught with problems.

Linq ;0)>
 

MLUCKHAM

Registered User.
Local time
Today, 05:57
Joined
Jul 23, 2013
Messages
89
OK, I take your point, probably need to give you some background.

I am doing some IOS development at the moment and need a RAD tool to design some levels for a game. The tool uses the design mode of an access form to move my game "assets" around to design the level, I then put the form in view mode and I can output the level into a file format containing pixel positions, object types etc. Hanging off the objects are a load of settings which form part of the export.

It all works and it has been a great help in speeding up a process that takes forever to hand crank out. For example, some levels may have over 100 assets (objects). In IOS all of these need to be displayed at a position on the screen to the exact pixel etc. There are no screen gui design shortcuts in IOS.

I have used a twips to pixel function to get me the pixel position based on the twips and all is good.

Anyway, I am working on a new tool which allows me to load or save previously created levels. When I load a level I need to basically import the file, create a load of objects and position them on the form in the correct place. Each object is the same type and has a number of fixed settings so rather than using the CreateControl command I wanted to have a templated control and use that as the basis for all new controls.

It looks like I am going to have to write the code to create the control from scratch each time. Not loads of work, but there is some onclick code hanging off these objects (when you click on them you can set various properties etc) which I will need to modify.

I used Access as it already has the GUI for designing forms. If I wrote the tool in .Net I would have to write the code to move and place items etc. This functionality already exists when a form is in design mode in Access which is cool.:eek:
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 14:57
Joined
Jan 20, 2009
Messages
12,853
Now that is probably the most novel use for Access that I have ever encountered.

Have you tried scripting the copy and paste?

Another way would be to create the new control then do a For Each loop through the properties of the template control and transfer their settings to the new control.

You could even have a table to hold the properties for several different styles of controls. Loop throgh the control properties and write them from a recordset based on the table. This would allow you to easily designate the property values by editing the table.

The origianal records in the table could be generated from a template control using a loop too.

You could make an number of new controls by looping the whole routine.
 

MLUCKHAM

Registered User.
Local time
Today, 05:57
Joined
Jul 23, 2013
Messages
89
Thanks Galaxiom, I do love Access. For 20 years now I keep coming back to it for quick solutions no matter what else I am doing.

I opted for the loop through the properties technique for this one in the end. I know that all the controls are command buttons so that makes it a little easier.

It is interesting that you can use a template for a new form, but not for a control.

Anyway, thanks for everyone who commented - back to the game!:eek:
 

ChrisO

Registered User.
Local time
Today, 14:57
Joined
Apr 30, 2003
Messages
3,202
I'm not entirely sure what you are trying to do but it sounds like this might help:-

http://www.access-programmers.co.uk/forums/showthread.php?t=228040

The above demo uses Labels but Command buttons or Image controls could be used.

You will not need to go into design mode to copy/paste a control because they pre-exist. Use the left mouse button to move and resize. Use the right mouse button to change properties and copy/paste. You can also copy/paste an entire layout.

The position, size and properties are saved in a table. It could be saved and loaded as a CSV file.

Chris.
 

Users who are viewing this thread

Top Bottom