[RESOLVED] Access variable by name from user input string
Is there a way to access a variable by name from a user input string? Here is some code to show you what I mean. Keep in mind this is psuedo-code, to help me convey what I'm asking:
Code:
Dim thisisavar1 As Integer
Dim thisisavar2 As String
Dim thisisavar3 As String
Dim thisisavar4 As Long
Sub Command1_Click()
MsgBox getvariabledata(Text1.Text)
End Sub
So when Text1.Text = "thisisavar3" it returns whatever string is in thisisavar3, etc.
Thanks. :)
Re: Access variable by name from user input string
VB6 has CallByName function:
Code:
Option Explicit
Public myText As String
Private Sub Form_Load()
myText = "Hello World!"
End Sub
Private Sub Command1_Click()
On Error Resume Next
'your textbox should have "myText" entered at this time
MsgBox CallByName(Me, Text1.Text, VbGet)
End Sub
Re: Access variable by name from user input string
I knew there was a function for it, I just couldn't find it. Thanks tons man.
EDIT: Issue not resolved.
Re: Access variable by name from user input string
RhinoBull, I ran the code exactly as described above and it didn't work.
vb Code:
'these declares are in a module
Global test1 As String
Global test2 As Integer
Global test3 As String
'this is in a commandbutton on Form1
Private Sub Command1_Click()
test1 = "hello world"
test2 = 5
test3 = "testing 1 2 3"
MsgBox CallByName(Me, Text1.Text, VbGet)
End Sub
Re: Access variable by name from user input string
Nevermind. I know why now. Moved the vars to "public" in the form and it worked fine. Thanks again RhinoBull. :)
So how do I use CallByName to access a variable in a module (like the global vars example above)?
Re: Access variable by name from user input string
Worked just fine for me
Code:
Option Explicit
Public test1 As String
Public test2 As Integer
Public test3 As String
'this is in a commandbutton on Form1
Private Sub Command1_Click()
test1 = "hello world"
test2 = 5
test3 = "testing 1 2 3"
MsgBox CallByName(Me, Text1.Text, VbGet)
End Sub
Re: Access variable by name from user input string
Hmmm... Here is an idea:
- add class module to your project
- define all of your public variables in the class instead of module
- declare public object variable as that class in the module
- rest should be the same except that you will your object variable instead of form name:
Code:
'in the class:
Option Explicit
Public var1 As String
Public var2 As String
'in the module:
Option Explicit
Public myClass As Class1
'in the form:
Private Sub Form_Load()
Set myClass = New Class1
myClass.var1 = "Hello!"
myClass.var2 = "Goodbye!"
End Sub
Private Sub Command1_Click()
MsgBox CallByName(myClass, Text1.Text, VbGet)
End Sub
Re: Access variable by name from user input string
That's a good idea man. Thanks. :)
Re: [RESOLVED] Access variable by name from user input string