Dynamic Creation of Option Group

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an application that requires a matrix of option buttons that changes
based on a table.

So far, I have a method of creating a collection that identifies each item,
but have not been able to make the item into an option button. Is this
possible?

For intX = 0 To intTotal

For intY = intX To intTotal

strX = Format(intX, "00")
strY = Format(intY, "00")
ctlArray(intX, intY) = "ctl" & strX & "_" & strY
colRadio.Add (ctlArray(intX, intY))

Next intY

Next intX
For Each newRadio In colRadio

newRadio.Left = Me.WindowLeft + 720
newRadio.Top = Me.WindowTop - 720
newRadio.ControlType = acOptionButton

Next

This code dies at the "For Each newradio in colRadio" with "Run-time error
'424': Object required.

Thanks in advance.

Woody
 
Grwffyn said:
I have an application that requires a matrix of option buttons that changes
based on a table.

So far, I have a method of creating a collection that identifies each item,
but have not been able to make the item into an option button. Is this
possible?

For intX = 0 To intTotal

For intY = intX To intTotal

strX = Format(intX, "00")
strY = Format(intY, "00")
ctlArray(intX, intY) = "ctl" & strX & "_" & strY
colRadio.Add (ctlArray(intX, intY))

Next intY

Next intX
For Each newRadio In colRadio

newRadio.Left = Me.WindowLeft + 720
newRadio.Top = Me.WindowTop - 720
newRadio.ControlType = acOptionButton

Next

This code dies at the "For Each newradio in colRadio" with "Run-time error
'424': Object required.


That's too vague and out of context for me to answer with a
specific suggestion.

A general approach is to precreate all potentially needed
controls with names like ctl03_07. The controls can then be
referenced using this kind of syntax:
Me("ctl" & intX & "_" & intY)

Then instead of whatever your code is trying to do, just
make the needed controls Visible and adjust the Top and Left
properties.
 
That is my dillema, I don't want to pre-create a mass of controls that may or
may not be used. I want them to be created dynamically based on a table that
way when a user modifies the table, the next time the form comes up with the
controls on it, it will be the correct set of controls.
 
The problem with what you want is that creating controls is
a design time operation. If you do design kinds of things
in a running apllication, you open the door to big time
bloat, greatly increased chances of corruption and, if you
try to convert to MDE, a complete failure to function.

Besides all that, it requires less code to modify properties
of precreated controls than it does to create them on the
fly, even if it were OK to do it.
 
I understand that this is a design time operation. The purpose is to let
multiple users have their own specialized (and spelializable) matrix without
having to come to me to reprogram that screen.
What you are telling me is that there is no way to instantiate controls from
variables. Is that correct? If so, then I can go on to some other development
language that will allow this rather simple behavior.

Thanks!

Marshall Barton said:
The problem with what you want is that creating controls is
a design time operation. If you do design kinds of things
in a running apllication, you open the door to big time
bloat, greatly increased chances of corruption and, if you
try to convert to MDE, a complete failure to function.

Besides all that, it requires less code to modify properties
of precreated controls than it does to create them on the
fly, even if it were OK to do it.
--
Marsh
MVP [MS Access]

That is my dillema, I don't want to pre-create a mass of controls that may or
may not be used. I want them to be created dynamically based on a table that
way when a user modifies the table, the next time the form comes up with the
controls on it, it will be the correct set of controls.
 
What I am saying is that you should precreate a large (?)
pool of precreated invisible controls and manipulate them at
runtime. The size of the pool of controls should exceed
whatever any user needs (surely you have a reasonable limit
in what users can select). You don't even need to create
the controls manually, just write a little procedure
(utilizing CreateControl) for **you** to use one time. This
really is not a big deal and you can easily continue to use
your table for manipulating the controls that the user want
to see.

I would hate to think that you would go to a more complex
development tool just because Access optimizations require
design changes to be saved before a form/report can be used.
 
Thanks.
I presume the routine would look something like:
Set ctl00_00 = Createcontrol(frm.Name, acOptioinButton, , "", "", intX, intY)
Set ctl00_01 = Createcontrol(frm.Name, acOptioinButton, , "", "", intX, intY)
etc. through the last control then manipulate them after they are all created.

Marshall Barton said:
What I am saying is that you should precreate a large (?)
pool of precreated invisible controls and manipulate them at
runtime. The size of the pool of controls should exceed
whatever any user needs (surely you have a reasonable limit
in what users can select). You don't even need to create
the controls manually, just write a little procedure
(utilizing CreateControl) for **you** to use one time. This
really is not a big deal and you can easily continue to use
your table for manipulating the controls that the user want
to see.

I would hate to think that you would go to a more complex
development tool just because Access optimizations require
design changes to be saved before a form/report can be used.
--
Marsh
MVP [MS Access]

I understand that this is a design time operation. The purpose is to let
multiple users have their own specialized (and spelializable) matrix without
having to come to me to reprogram that screen.
What you are telling me is that there is no way to instantiate controls from
variables. Is that correct? If so, then I can go on to some other development
language that will allow this rather simple behavior.
 
I was thinking of something more like:

Public Sub MakeOptions()
Dim ctl As Control
Dim intX As Integer, intY As Integer

For intX = 0 To 10
For intY = 0 To 15
Set ctl = CreateControl("theform", acOptionButton)
ctl.Name = "ctl" & intX & "_" & intY
ctl.Visible = False
Next intY
Next intX
End Sub

Open the form in design view and run the above procedure
from the debug window. Then close and save the form.

Then, at run time, when you want to display some of the
controls according to the data in your table, use something
along these lines (in the forms Load event??):

Set rs = OpenRecordset("SELECT f1,... FROM ... WHERE ...")
IntX = 0 : intY = 0
Do Until rs.EOF
With Me("ctl" & intX & "_" & intY)
.Top = 300*intY 'vertical spacing in twips
.Left = 1000*intX 'horizontal spacing
.ControlSource = rs!fieldname
.Visible = True
End With
rs.MoveNext
Loop
 
Back
Top