Results 1 to 10 of 10

Thread: Open a form with name as string

  1. #1

    Thread Starter
    Lively Member Dazzler's Avatar
    Join Date
    Sep 2007
    Posts
    68

    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...

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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

  3. #3

    Thread Starter
    Lively Member Dazzler's Avatar
    Join Date
    Sep 2007
    Posts
    68

    Re: Open a form with name as string

    but what does that isLoaded function will return?

  4. #4
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    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
    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

  5. #5

    Thread Starter
    Lively Member Dazzler's Avatar
    Join Date
    Sep 2007
    Posts
    68

    Re: Open a form with name as string

    when i use the above code am getting "Subscript out of range" error....

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  7. #7
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Open a form with name as string

    I think you might confuse him by using the tag property.

  9. #9
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Open a form with name as string

    Yeah, but if it works for him he can attempt to understand it later...

  10. #10
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Open a form with name as string

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width