adding components by code

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

Guest

Hi, is it possible to add components (like textboxes) to a form using VBA code.
I thougt it is possible with a form in design view, but I don't know how.

regards

Rob
 
Sure,

Just take a look at CreateControl for form controls and CreateReportControl
for report controls respectively.
 
Although using the CreateControl method will do this, it is not advised. The
problem you run into is that a form has a life time maximum of 754 controls.
That is, if you add a control to a form, the count goes up by one, but if you
delete a control, the count does not reduce by one. Therefore, it doesn't
take long before the form will error out and no longer work.

In a case where a control may or may not be needed at run time, the better
way is to create the control in design view and make it not Visible. Then
when you determine you need it at run time, just make it visible.

If you think you will not know how many controls you may need, I suggest you
rethink your design.
 
Dave,

There may be legitimate scenarios where you would need to use them - for
example, creating a form/report creation Wizard for your users, or
re-creating the database from another source (as I did in my case - I
serialize the database to xml files and deserialize them back into a new
database).
 
Both scenerios you describe are one time events. My comment has to do with
creating controls at run time for a form that is used in production. It is
never a good idea. The form will eventually fail.
 
Back
Top