Link to home
Start Free TrialLog in
Avatar of shinfotec
shinfotec

asked on

Open form by its name

how can i open unloaded VBForm by its name like:

forms.("FormName").show .... or any way
Avatar of Colosseo
Colosseo
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi

how about

FormName.Show

Scott
Or if you meant where you have the name as a string you could try this

' Add the form to the forms collection
Forms.Add "FormName"

' Show the form with the highest index (which is the last added and in this case FormName)
Forms(Forms.Count - 1).Show

Cheers

Scott
Or do you not want to 'hard-code' the form name (is it, perhaps, stored within a string)?

If so, try:

    Sub OpenFormByName(ByVal sFormName As String)

           Dim frm As VB.Form

           For Each frm In Forms
              If frm.Name = sFormName Then frm.Visible = True
           Next frm

    End Sub

Now just open the form, with

    OpenFormByName("myFormName")

Although, you will have to have loaded all forms beforehand, in an initialisation routine, as Forms collection only contains the loaded forms.

HTH

J.
Avatar of Shauli
Shauli

Dim myFormName As String, NewForm as Form

'to show the form
    myFormName = "Your form name"
    Forms.Add (myFormName)
    Set NewForm = Forms(Forms.Count - 1)
    NewForm.Show

'and when you unload the "NewForm" add this line to your code

Set NewForm = Nothing

S
ASKER CERTIFIED SOLUTION
Avatar of JR2003
JR2003

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
 Dim frm As Form
 
  For Each frm In Forms
    If frm.Name = "myLoadedForm" Then
      frm.Show vbModal
    End If
  Next


This depends on the form already being loaded