put controls on form through VBA

  • Thread starter Thread starter JJ
  • Start date Start date
J

JJ

I want to auto-create a form based on input in a table, so
I am looking for sample code showing:

- How to place a control on a form through VBA
- How to access the form-object in another .mdb

Please post any sample code here (instead of links to
othre istes) as I am on a slow modem connection...
 
Hi,


Probably the only documented way to create a control on a form is to use
CreateControl Method. The form has to be open, in design (but ok if it is
minimized, that is what the wizards do).

Access Form are class with the COM instantiation Public, but not creatable.
That means that other COM application can refer to them, but cannot create
one, by themselves. They need the help of a factory. Another common example
is a DAO recordset: you can't create one, by yourself, you can't say:

Dim rst As NEW DAO.Recordset


you have to relay on some factory that will manufacture (and properly
initialize ) the object:

Dim rst As DAO.Recordset
Set rst= CurrentDb.OpenRecordset( arguments that initialize the object
properly)


that is a trick that simulates C++ or C# constructor, if you have C++ or C#
knowledge. Anyhow, back in Access, the factory? yes, you can build one:
define, in a standard module, a public function that will create and return
the form (yes, for sure, in the db owning the form to be created):



' Form1 HAS TO have some code, cannot be a "light" form, codeless.

Public Function MySuperDuperFactoryForForm1( ) As Form_Form1
MySuperDuperFactoryForForm1=New Form_Form1
End Function



As you see, that was really tough (we didn't need any "argument", Access
know how to fully initialize the form all by itself, without our help). Now,
save and close the mdb. Open the other mdb, include the first mdb among its
reference (you need to click on the button Browser, form the References
dialog, to locate the file, after you changed the file filter, if required),
you would be able to reference the form:


Dim frm As FirstDbName.Form_Form1 ' to avoid collision with your local
Form_Form1,
' use the full reference syntax

Set frm = MySuperDuperFactoryForForm1( )


Sure, the form will be open, but invisible... If you turn its visibility to
true

frm.Visible=True


remember that the form will live not a second more than your frm variable
itself. In other words, if frm goes out of scope, so the form it represents
close and die.



Hoping it is clear, and in relation about what you really asked,
Vanderghast, Access MVP
 
Back
Top