USe CreateObject to create a new form

BluePlastic

New member
Local time
Today, 13:00
Joined
Nov 3, 2006
Messages
5
Hiya

I have a function that accepts a string variable that is the name of a form (e.g. "frmMain") and I need to create a new instance of the form, which I can't use the New word to do as it doesn't accept a string variable - so I was expecting to use CreateObject:

Sub subA (pstrFrmName as string)
Set gfrmAny = CreateObject("MyDB.form_" & pstrFrmName)
End Sub

But I receive error 'ActiveX component can't create object'

Any ideas please?!
 
Yup. Google this forum, there is a number of posts about creating new instances of a form .
 
You can instantiate a Form by name, just not the ‘normal’ way people have tried over the years.

In its simplest form the code is quite easy but the explanation is not.

There are at least two ways to instantiate a Form as in…
1. The most common method quoted is:- http://allenbrowne.com/ser-35.html
2. Make it a subform.

The method described here uses both.

We can instantiate a Form by name if we pass the Function the name of the Form to be instantiated. However, we do not instantiate the Form name as passed but rather a Popup Container Form which has a subform control on it. Once the Container Form has been instantiated we can set the SourceObject of the subform control to the passed Form name argument.

The Form, as named in the passed argument, is an instantiated Form, all subforms are.

In its simplest form the instantiated Forms would be mutually exclusive.

Code behind a Form:-
Code:
Private Sub cmdPopup1_Click()

    InstantiatePopup "frmPopup1"
    
End Sub


Private Sub cmdPopup2_Click()

    InstantiatePopup "frmPopup2"

End Sub


Private Sub cmdPopup3_Click()

    InstantiatePopup "frmPopup3"

End Sub


Private Sub Form_Open(ByRef intCancel As Integer)

    DoCmd.Restore
    DoCmd.MoveSize 4000, 4000
    
End Sub


Private Sub Form_Close()

    ' Close all instances.
    Set frmCurrentPopupForm = Nothing
    
End Sub



Code in a standard module:-
Code:
Public frmCurrentPopupForm As Access.Form


Public Function InstantiatePopup(ByVal strPopupFormName As String)
                     
    Set frmCurrentPopupForm = New Form_frmPopupContainer

    With frmCurrentPopupForm
       .ctlPopupForm.SourceObject = strPopupFormName
       .Visible = True
    End With
 
End Function

A demo of the mutually exclusive version (V1) is attached.

Chris.
 

Attachments

spikepl: I've been googling this for a while and haven't found anything. I must be rubbish at it :-)

ChrisO: thanks for the suggestions, but:

The first one is just about using the New operator to create the instance - which can't accept a string variable
The second one seems a bit hacky to me - and I think there is a 3 layer limit on subforms, which this would trim to 2.

Looks like I just need to stick to a huge case statement in my function to implement the New operator:

select case pstrFrmName

case "frmA"
Set frmAny = New form_frmA

case "frmB"
Set frmAny = New form_frmB

case "frmC"
Set frmAny = New form_frmC

case ...

end select
 
What ‘seems a bit hacky’ to you is of no concern to me; my reply answers your question about Form instantiation.

Chris.
 
Whoooooooooooooooo

Here’s how I see it…
You want a free answer.
You do not tell us the version of Access you are using.
I answer your question about Form instantiation.

You say it ‘seems a bit hacky’ but I very much doubt if you have ever seen it done that way before. It answers the question and it’s the only way I know how to do it. It can’t be ‘hacky’ if it’s the only game in town. In the land of the blind the one-eyed man is king.

You throw up another objection about 3 levels of subforms but that restriction hasn’t existed since Access 97. It’s now 7 so if you have a concern about the limit of 3 then that concern is unfounded unless you are still using Access 97. You say “and I think there is a 3 layer limit on subforms” but if you had done the research you wouldn’t think it, you would know the answer.

It takes a bit of work to answer questions and it is done for free, even on a Sunday. There are times when the person asking the question should do a little more research.

Chris.
 
Here's how I see it...

The one eyed man ain't going to rule for long if he falls on the cliff on his blind side.

Off course it can still be hacky even if it's the only game in town. The answer to the question is that MS Access VBA won't let one create an instance of a class using a string variable for the name of the class. You have very kindly suggested a work-around, that seems to me to have a bit of an overhead.

I used 'I think' as I new I was being a bit lazy as I was in a hurry - I was just knocking out a possible flaw off the top of my head - most insensitive.
Nice to know it's 7 - I'd love to see an app that needs to use that many :-)

Thanks for the help anyway
Cheers
 

Users who are viewing this thread

Back
Top Bottom