Results 1 to 9 of 9

Thread: [RESOLVED] Access variable by name from user input string

  1. #1

    Thread Starter
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

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

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  3. #3

    Thread Starter
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    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.
    Last edited by deathfxu; Mar 11th, 2009 at 12:33 PM.

  4. #4

    Thread Starter
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    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:
    1. 'these declares are in a module
    2. Global test1 As String
    3. Global test2 As Integer
    4. Global test3 As String
    5.  
    6. 'this is in a commandbutton on Form1
    7. Private Sub Command1_Click()
    8. test1 = "hello world"
    9. test2 = 5
    10. test3 = "testing 1 2 3"
    11. MsgBox CallByName(Me, Text1.Text, VbGet)
    12. End Sub
    Last edited by deathfxu; Mar 11th, 2009 at 12:53 PM.

  5. #5

    Thread Starter
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    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)?
    Last edited by deathfxu; Mar 11th, 2009 at 12:46 PM.

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

    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

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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

  8. #8

    Thread Starter
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 2009
    Location
    USA
    Posts
    279

    Re: Access variable by name from user input string

    That's a good idea man. Thanks.

  9. #9

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