Open a form with name as string
Hi all
i have to open a form with its name.. my form name is in string type so i used the following code to open the form using forms object
Code:
Forms.Add(FormName).Show
and this works too.. the problem is that it opens a instance of the original form so resulting is that when i try to open the form from menu another form (ie) original form is being opened...
so plz tell me how to open a form with form name as string.. and not the instance...
Re: Open a form with name as string
If you use Forms.Add to add the form you must cycle thru the Forms collection to load it. Here's an example the way I use it.
Code:
Public Sub ShowForm(FormName As String)
Dim frm As Form
If Not IsLoaded(FormName) Then
Forms.Add (FormName)
Forms.Item(Forms.Count - 1).Tag = FormName
Else
For Each frm In Forms
If frm.Tag = FormName Then
frm.Show
End If
Next
End If
End Sub
Re: Open a form with name as string
but what does that isLoaded function will return?
Re: Open a form with name as string
can you clarify if you want to create a form or just open a form already created during design time? If you want to just open an existing form. Iam sure you can do more with the below snippet :thumb:
Code:
Dim frm2 As Form2
Private Sub Command1_Click()
Set frm2 = New Form2
frm2.Hide
End Sub
Public Sub gOpenHiddenForm(FormToOpenName As String)
For i = 1 To Forms.Count
If Forms(i).Name = FormToOpenName Then
'Do whatever you want
'say close the openForm
'
'
Forms(i).Show
Exit Sub
End If
Next i
End Sub
Re: Open a form with name as string
when i use the above code am getting "Subscript out of range" error....
Re: Open a form with name as string
How are you using it?
Post what you have and indicate what line is giving you the error.
Re: Open a form with name as string
Code:
Public Function IsLoaded(frmName As String) As Boolean
Dim frm As Form
IsLoaded = False
For Each frm In Forms
If frm.Tag = frmName Then
IsLoaded = True
Exit For
End If
Next
End Function
Re: Open a form with name as string
I think you might confuse him by using the tag property. :)
Re: Open a form with name as string
Yeah, but if it works for him he can attempt to understand it later...
Re: Open a form with name as string
Quote:
when i use the above code am getting "Subscript out of range" error....
Pls post the snippet you are using so that we may be able to help you