Consulting

Results 1 to 6 of 6

Thread: REFERENCING A VARIABLE FORM NAME

  1. #1
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254

    REFERENCING A VARIABLE FORM NAME

    ' I HAVE TWO FORMS: Macro1 AND Macro2.
    ' BOTH HAVE A TEXTBOX NAMED: TextBox1 ON THEM.
    ' CAN I CALL A FUNCTION: RandomFunctions(VariableForm)
    ' AND TAKE THE DATA FROM A VARIABLE FORM'S (I.E. Macro1 OR Macro2)
    ' CORESPONDING TEXTBOX: TextBox1 AND PLACE IT
    ' INTO CELL A1 ON Sheet1?



    Sub Macro1()
        VariableForm = "Macro1"
        Call RandomFunctions(VariableForm)
    End Sub
    
    
    Sub Macro2()
        VariableForm = "Macro2"
        Call RandomFunctions(VariableForm)
    End Sub
    
    
    Sub RandomFunctions(VariableForm)
    
    
        Sheets("Sheet1").Range("A1").Value = VariableForm.TextBox1.Value
        
    End Sub
    Last edited by Bob Phillips; 06-25-2014 at 02:49 AM. Reason: Added VBA tags

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Please use code tags. Does "Macro1".Textbox1.Value make sense to you? Obviously, the compiler does not know what that means.

    You can try UserForms("Macro1").TextBox1.Value syntax.

    I suspect there are easier ways to get at what you need.

  3. #3
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    Macro1.TextBox1.value works just fine but the idea is to get away from hard coded references. I don't want to write code for every form that will utilize the function. Hard coding the form references defeats the purpose of a function.

  4. #4
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254

    This seems to be what im looking for / i think

    Dim frmCurrentForm As FormSet
    frmCurrentForm = Screen.
    ActiveForm
    MsgBox "Current form is " & frmCurrentForm.Name


    Now my problem is Im getting a compile error.
    user -defined type not defined.

    Any thoughts?


  5. #5
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Why not just pass the actual form as an object instead of its name:
    Sub Macro1()Call RandomFunctions(Form1)
    End Sub
    
    
    
    
    Sub Macro2()
    Call RandomFunctions(Form2)
    End Sub
    
    
    
    
    Sub RandomFunctions(VariableForm)
    
    
    Sheets("Sheet1").Range("A1").Value = VariableForm.TextBox1.Value
    
    
    End Sub
    Be as you wish to seem

  6. #6
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    If yall would like you can demote me from a VBAX Regular to a VBAX NOOB.
    Thank you Aflatoon, That answer was exactly what I was looking for.

Tags for this Thread

Posting Permissions

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