For Smarties Only: Instantiate Forms as Objects

  • Thread starter Thread starter Ken Ismert
  • Start date Start date
K

Ken Ismert

As you experienced programmers know, you can instantiate an Access
form, just like any other object, by using its class name:
Dim rFrm as Access.Form
Set rFrm = New Form_YourForm ' form "YourForm"

This syntax allows you to create multiple open instances of a form.
With that background, here is my question:

Is there a simple, one-line way to instantiate a form object using a
string:
sFormName = "YourForm"
Set rFrm = MFOS("Form_" & sFormName) ' MFOS = "Magic Function or
Syntax"

NOTE: Because of the multiple instance requirement, DoCmd.OpenForm
won't work. It can only make one instance of a form.

There doesn't seem to be a CreateObject syntax that works.

I currently use a big Case statement matching the form name to it's
class. This gets to be a pain, because every time I add or remove
forms, I have to edit the function.

Do any of you have an answer?

-Ken
 
CreateObject is way not gunna do it! That will be looking for a progid (or
file extension, whatever) in the Registry. You could >maybe< do a
CreateObject on "Access.ShortCut.Form", but that's not going to achive what
you want - even if it worked (whatever "worked" means, there).

My half-assed guess is, there's no way to do what you want, from normal VBA.
To instantiate a class object, using the New keyword, surely needs an
early-bound scenario. So, the name of the form must appear explicitly (not
in a string). So nothing using strings will work.

But I'm happy to be proved wrong on that guess!

HTH,
TC
 
Ah, so right, TC. For CreateObject to work, the COM object must be
registered on your machine. Obviously, that is not the case with
Access forms, which are private to the application. A quick search in
RegEdit proved that.

Interestingly, when you use the undocumented Application.SaveAsText
function on a form, and inspect the results, you can see that the form
and all its controls have their own GUIDs, so they really seem to be
privately registered COM objects.

So, if this function existed, it would have to be an Access
application-level function, something like CreateAccessObject. There
certainly is no such thing, at least in A2K.

Thanks,
-Ken
 
Back
Top