Help with new form instance coding please.

T

ThomasAJ

Help with new form instance coding please.

I use a variation of the following code for each form that I want to be able
to create a new instance thereof. It works fine.

However I want to be able to use ONLY ONE procedure and feed in the form
name as a variable and then use it in line 70 thus:
Set frm = New frmVariable

Nothing I tried works. It seems that 'Set frm = New ????' requires a form
name and not a variable.

Sub NewGraph1()
Dim frm As Form
'New instance
70 Set frm = New Form_Graph1
71 frm.Visible = True
' The Key value must be a string, so tack on a
' null string to force the conversion. You'll
' use the hWnd later when you try and
' remove the window from the collection of windows.
72 colForms.Add Item:=frm, Key:=frm.hWnd & ""
125 Set frm = Nothing
End Sub
 
S

Stuart McCall

ThomasAJ said:
Help with new form instance coding please.

I use a variation of the following code for each form that I want to be
able
to create a new instance thereof. It works fine.

However I want to be able to use ONLY ONE procedure and feed in the form
name as a variable and then use it in line 70 thus:
Set frm = New frmVariable

Nothing I tried works. It seems that 'Set frm = New ????' requires a form
name and not a variable.

Sub NewGraph1()
Dim frm As Form
'New instance
70 Set frm = New Form_Graph1
71 frm.Visible = True
' The Key value must be a string, so tack on a
' null string to force the conversion. You'll
' use the hWnd later when you try and
' remove the window from the collection of windows.
72 colForms.Add Item:=frm, Key:=frm.hWnd & ""
125 Set frm = Nothing
End Sub

I think you'll have to set frm 1st and pass it to your sub.

First make a slightly different sub, something like:

Sub InitForm(frm As Access.Form)
frm.Visible = True
colForms.Add Item:=frm, Key:=frm.hWnd & ""
End Sub

Then call it like this:

Dim frm As Access.Form

Set frm = New Graph1
InitForm frm
'do whatever else with frm

Set frm = New Graph2
InitForm frm
'do whatever else with frm

Set frm = nothing
 
D

Douglas J. Steele

If you're passing a string containing the name of the form, you can use

Set frm = Forms(strFormName)

(The form MUST be open for that to work)

If you're passing a reference to the form, you don't need the Set line at
all:

Sub NewGraph1(frmVariable As Form)
71 frmVariable.Visible = True
' The Key value must be a string, so tack on a
' null string to force the conversion. You'll
' use the hWnd later when you try and
' remove the window from the collection of windows.
72 colForms.Add Item:=frm, Key:=frm.hWnd & ""
End Sub
 
S

Stefan Hoffmann

hi Thomas,
Nothing I tried works. It seems that 'Set frm = New ????' requires a form
name and not a variable.
Yes, that's right. You can't use a variable here. You may use a function
like

Public Function CreateForm(AFormName As String) As Access.Form

Dim af As Access.Form

Set af = Nothing

Select Case AFormName
Case Is = "yourForm1"
af = New Form_yourForm
Case ...
End Select

CreateForm = af

End Function

You may create thie wrapper or any similar by code automatically.


mfG
--> stefan <--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top