PDA

View Full Version : [SOLVED] REFERENCING A VARIABLE FORM NAME



MINCUS1308
06-24-2014, 10:56 AM
' 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

Kenneth Hobs
06-24-2014, 11:40 AM
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.

MINCUS1308
06-24-2014, 12:21 PM
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.

MINCUS1308
06-24-2014, 12:45 PM
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?

Aflatoon
06-25-2014, 01:29 AM
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

MINCUS1308
06-25-2014, 06:02 AM
If yall would like you can demote me from a VBAX Regular to a VBAX NOOB. :crying:
Thank you Aflatoon, That answer was exactly what I was looking for.