Dim frm1 As Form
Dim frm2 As Form
Set frm1 = New Form_formname
Set frm2 = New Form_formname
frm1.RecordSource = whatever
frm1.Visible = True
You certainly can.
Code:Dim frm1 As Form Dim frm2 As Form Set frm1 = New Form_formname Set frm2 = New Form_formname
The forms are then addressed by their variable name.
Code:frm1.RecordSource = whatever frm1.Visible = True
Unfortunately, that's not possible.The class name has to be hardcoded in the variable assignment.So how do I dynamically create the name for the form.
I'd want the form to be frmx where x is a variable number.
. So how do I dynamically create the name for the form.
I'd want the form to be frmx where x is a variable number.
frm1.Caption = "whatever you want"
Can I open the same form more than once...
Unfortunately, that's not possible.The class name has to be hardcoded in the variable assignment.
Why do you need to open a form whose name you do not know in advance?
[Edit] I probably misinterpreted your question. If it is about the variable name and not the class name of the form, Galaxiom's hint to the collection is the way to go.[/Edit]
You can use a Collection to hold the instances of the form and then refer to them by whatever index names you give them.
All instances have the same Name property which is the default that is displayed in the header. However you can change that to whatever you want by using the Caption Property.
Code:frm1.Caption = "whatever you want"
Allen Browne covers it very well here:-
http://allenbrowne.com/ser-35.html
Sent from my SM-G925F using Tapatalk
Allen Browne covers it very well here:-
http://allenbrowne.com/ser-35.html
Sent from my SM-G925F using Tapatalk
private m_me as form
private sub form_open(cancel as integer)
set m_me = me
end sub
dim i as integer
for i = 1 to 5
with new form_form1
.visible = true
.caption = "instance number " & i
end with
next
private sub cmdclose_click()
set m_me = nothing
end sub
This works but but I'd need to set this up for each form where I need a 'collection' as the name of the form is specified in the function. I've tried passing the name of the form over to the function as a parameter but I can't create a new addition to the collection in this way. Probably doing it wrong anyway but this is what I'm doing
Original code is
Function OpenAClient()
'Purpose: Open an independent instance of form frmClient.
Dim frm As Form
'Open a new instance, show it, and set a caption.
Set frm = New Form_frmClient
frm.Visible = True
frm.Caption = frm.Hwnd & ", opened " & Now()
'Append it to our collection.
clnClient.Add Item:=frm, Key:=CStr(frm.Hwnd)
Set frm = Nothing
End Function
I've tried changing the function to
Function OpenAClient(CALForm As Form)
and changing the set frm to
Set frm = new CALForm
I get compile errors for this?
I resume I can't actually do this and I will need a function for each set of forms where I need a collection
I tried that before but will try again. I've not used EVAL though so will have a look at that.Wouldn't you just be passing in the name of the form as a string and then concatenate with "Form_" & CalForm or perhaps have to use EVAL() ?
OK but off to bed now s will do tomorrowIf you post a database that demonstrates the problem you are working on, I will modify it to show you a very simple way to enable non-default instances.
Mark
I tried that before but will try again. I've not used EVAL though so will have a look at that.
OK - no idea how to use EVAL
Tried this
Eval ("Set frm = New Form_" & CALForm)
and
Set frm = New Eval ("Form_" & CALForm)
neither work
Sorry, but as indicated earlier, it does not work this way. You have to hardcode the form name.No, I was thinking of Eval(CalForm).
You are just trying to evaluate the contents of the variable.
Public Function OpenAClient(FormName as string)
'Purpose: Open an independent instance of form frmClient.
Dim frm As Form
'Open a new instance, show it, and set a caption.
Set frm = New Form_frmClient
frm.Visible = True
frm.Caption = FormName
'Append it to our collection.
clnClient.Add Item:=frm, Key:=FormName
Set frm = Nothing
End Function
Private Sub Form_Close()
'Purpose: Remove this instance from clnClient collection.
Dim obj As Object 'Object in clnClient
Dim blnRemove As Boolean 'Flag to remove it.
'Check if this instance is in the collection.
For Each obj In clnClient
If obj.Caption = Me.Caption Then
blnRemove = True
Exit For
End If
Next
'Deassign the object and remove from collection.
Set obj = Nothing
If blnRemove Then
clnClient.Remove Me.Caption
End If
End Sub
Private Sub Form_Close()
‘Form Code
'Purpose: Remove this instance from clnClient collection.
RemoveFromCollection Me.Caption
End Sub
Public Sub RemoveFromCollection(FormCaption as string)
‘Put in standard module
'Purpose: Remove this instance from clnClient collection.
Dim obj As Object 'Object in clnClient
Dim blnRemove As Boolean 'Flag to remove it.
'Check if this instance is in the collection.
For Each obj In clnClient
If obj.Caption = FormCaption Then
blnRemove = True
Exit For
End If
Next
'Deassign the object and remove from collection.
Set obj = Nothing
If blnRemove Then
clnClient.Remove FormCaption
End If
End Sub