Results 1 to 3 of 3

Thread: Using a variable to open a form

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Location
    Red Lion, PA, USA
    Posts
    1

    Post

    I've run into a problem where I have form names stored in a table on SQL server and then when selectivly retrieved and stored into a variable to be opened doesn't work.

    For example:

    If a have a Form call "frm_vbtest" in the SQL table and I retrieve that value and store it into a String Variable "FormName", I want to be able to pass that variable to the Load or Show command in order to display that form.

    By being able to do this I don't have to hard code all my form names using Case statements.

    Anyone have any ideas.

    Thanks,

    Ryan Grim
    IS Admin

  2. #2
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923

    Post

    FormName has to be declared as a Form for that to work, e.g.

    frmFormName As Form

    However, if you are able to convert a string name into a Form type I don't know. You could try setting the Name property of the Form type to the name in your database, e.g.

    frmFormName.Name = strFormName

    That [i]mightp/i] work, give it a shot!

    Regards,

    ------------------
    - Chris
    [email protected]
    Q. Why do mice have small balls?
    A. Not that many know how to dance

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Post

    Here's a Function I wrote to do just what you're looking for:
    Code:
    Private Function FormByName(ByVal FormName As String) As Form
        Dim oForm As Form
        'Make sure Form isn't already Loaded..
        For Each oForm In Forms
            If LCase(oForm.Name) = LCase(FormName) Then
                'If it is, Return a Pointer to it..
                Set FormByName = oForm
                Exit Function
            End If
        Next
        'Not in the Collection, so Add it..
        Set FormByName = Forms.Add(FormName)
    End Function
    
    Private Sub Command1_Click()
        Dim oForm As Form
        
        Set oForm = FormByName(Text1)
        oForm.Show
    End Sub
    ------------------
    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Certified AllExperts Expert

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