VBA Dynamic Control

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

We are needing to create a bunch of rectangle controls on a form, but do not
know how many are needed until run-time. So instead of creating a huge
amount at design time to compensate, we wish to create an array with an
appropriate amount at run time.

How is this done?

We have touched upon various methods as
"CreateObject("Forms.CommandButton.1")", and
"Dim newRectangles(10) as new rectangle"

but have various problems, one of which is assigning a parent control,
namely a form, and generally getting these controls displayed on the screen.
 
Hi,

Would it not be easier to create the maximum that will be required with
thier visible property set to false. Then change the visible property at
runtime?

Creating new controls means the form has to be open in design view. To
create new contols use the CreateControl method of the Application Object.
The code required would be like:

Dim k As Integer 'Counter
Dim ctlRec As Rectangle
Dim frm As Access.Form
Dim strNames(50) As String 'Array to store the contol names
DoCmd.OpenForm "frmTest2", acDesign
Set frm = Forms![frmTest2]
For k = 1 To 50
Set ctlRec = CreateControl(frm.Name, acRectangle, , "", "", k, k)
strNames(k) = ctlRec.Name
Debug.Print strNames(k)
Next

HTH

--

Cheers
Mark

Free Access/Office Add-Ins at:
http://mphillipson.users.btopenworld.com/
 
You can only create objects in design view, and worse, a form can only have
754 controls created on it *over its entire life*. So you will quickly
exceed that after you open the form a few times.

Certainly it is possible to create extra controls, and make them visible at
runtime, depending on other properties, but I wonder whether you are not
using the tools correctly. For instance a form in Continous Forms view can
have multiple records shown in controls, without the need to create any of
them in code.

--
Regards,

Adrian Jansen
J & K MicroSystems
Microcomputer solutions for industrial control
 
Back
Top