I want to make a program with an 'Immediate Window', where you can query the
variable value by typing the variable name. The problem is, I don't know how
to display the contents of a variable name written in the said Immediate
Window.

For example, I have a variable named 'username' in the program. If I wanted
to query the value of the variable during runtime, I would just type the
variable name (here, 'username') and the immediate window will display the
value.

Or:

username = "devon"
variablename = "username"

msgbox val(variablename)

Basically, I will evaluate the contents of variablename and treat it as a
variable name,
which would then become the 'username' variable, which will return 'devon'.

It's like the Eval function, I think:

textbox = "HelloWorld"
foo = "text"
bar = "box"
msgbox eval(foo & bar)

returns "HelloWorld"


Any ideas?
Thanks!

Re: Variable values as variable names by Bill

Bill
Tue Oct 14 05:37:15 CDT 2008

Hi Devon,

You could add a reference to Microsoft Script Control and use it's Eval
method. You'd have to add the objects to it first.



"DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> wrote in
message news:4A96EB36-4C23-4EEA-9B1F-25F06A15A3C1@microsoft.com...
>I want to make a program with an 'Immediate Window', where you can query
>the
> variable value by typing the variable name. The problem is, I don't know
> how
> to display the contents of a variable name written in the said Immediate
> Window.
>
> For example, I have a variable named 'username' in the program. If I
> wanted
> to query the value of the variable during runtime, I would just type the
> variable name (here, 'username') and the immediate window will display the
> value.
>
> Or:
>
> username = "devon"
> variablename = "username"
>
> msgbox val(variablename)
>
> Basically, I will evaluate the contents of variablename and treat it as a
> variable name,
> which would then become the 'username' variable, which will return
> 'devon'.
>
> It's like the Eval function, I think:
>
> textbox = "HelloWorld"
> foo = "text"
> bar = "box"
> msgbox eval(foo & bar)
>
> returns "HelloWorld"
>
>
> Any ideas?
> Thanks!
>
>
>


Re: Variable values as variable names by DevonWerkheiser

DevonWerkheiser
Tue Oct 14 06:03:00 CDT 2008

Thanks Bill,

but i'm not really sure on the 'add the objects' thing. First of all,
they're all really just variables, not objects.

But I am familiar with the eval of Microsoft Scripting, but I understand it
only evaluates mathematical expressions, but not variables used in the VB
program itself.

Hey, I appreciate your reply! It'd be great if you can help me out further.


"Bill McCarthy" wrote:

> Hi Devon,
>
> You could add a reference to Microsoft Script Control and use it's Eval
> method. You'd have to add the objects to it first.
>
>
>
> "DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> wrote in
> message news:4A96EB36-4C23-4EEA-9B1F-25F06A15A3C1@microsoft.com...
> >I want to make a program with an 'Immediate Window', where you can query
> >the
> > variable value by typing the variable name. The problem is, I don't know
> > how
> > to display the contents of a variable name written in the said Immediate
> > Window.
> >
> > For example, I have a variable named 'username' in the program. If I
> > wanted
> > to query the value of the variable during runtime, I would just type the
> > variable name (here, 'username') and the immediate window will display the
> > value.
> >
> > Or:
> >
> > username = "devon"
> > variablename = "username"
> >
> > msgbox val(variablename)
> >
> > Basically, I will evaluate the contents of variablename and treat it as a
> > variable name,
> > which would then become the 'username' variable, which will return
> > 'devon'.
> >
> > It's like the Eval function, I think:
> >
> > textbox = "HelloWorld"
> > foo = "text"
> > bar = "box"
> > msgbox eval(foo & bar)
> >
> > returns "HelloWorld"
> >
> >
> > Any ideas?
> > Thanks!
> >
> >
> >
>
>

Re: Variable values as variable names by Bill

Bill
Tue Oct 14 06:17:02 CDT 2008

Hi Devon,

For an example, add a script control to a form and paste this code into some
button click event or similar:


Dim foo As String
Dim bar As String
foo = "text"
bar = "box"

With ScriptControl1
.AddCode ("textbox = ""HelloWorld""")
MsgBox .Eval(foo & bar)
End With


There, I've used AddCode. But if TextBox1 was a control, you could add that
via AddObject, such as
.AddObject "TextBox1", Me.Text1

Note, you can give the object reference any name you want for the script
control to use in it's eval. Have a play with it, it's very powerful.



"DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> wrote in
message news:73C0665B-9F46-4F98-AE4A-72C088A13100@microsoft.com...
> Thanks Bill,
>
> but i'm not really sure on the 'add the objects' thing. First of all,
> they're all really just variables, not objects.
>
> But I am familiar with the eval of Microsoft Scripting, but I understand
> it
> only evaluates mathematical expressions, but not variables used in the VB
> program itself.
>
> Hey, I appreciate your reply! It'd be great if you can help me out
> further.
>
>
> "Bill McCarthy" wrote:
>
>> Hi Devon,
>>
>> You could add a reference to Microsoft Script Control and use it's Eval
>> method. You'd have to add the objects to it first.
>>
>>
>>
>> "DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> wrote in
>> message news:4A96EB36-4C23-4EEA-9B1F-25F06A15A3C1@microsoft.com...
>> >I want to make a program with an 'Immediate Window', where you can query
>> >the
>> > variable value by typing the variable name. The problem is, I don't
>> > know
>> > how
>> > to display the contents of a variable name written in the said
>> > Immediate
>> > Window.
>> >
>> > For example, I have a variable named 'username' in the program. If I
>> > wanted
>> > to query the value of the variable during runtime, I would just type
>> > the
>> > variable name (here, 'username') and the immediate window will display
>> > the
>> > value.
>> >
>> > Or:
>> >
>> > username = "devon"
>> > variablename = "username"
>> >
>> > msgbox val(variablename)
>> >
>> > Basically, I will evaluate the contents of variablename and treat it as
>> > a
>> > variable name,
>> > which would then become the 'username' variable, which will return
>> > 'devon'.
>> >
>> > It's like the Eval function, I think:
>> >
>> > textbox = "HelloWorld"
>> > foo = "text"
>> > bar = "box"
>> > msgbox eval(foo & bar)
>> >
>> > returns "HelloWorld"
>> >
>> >
>> > Any ideas?
>> > Thanks!
>> >
>> >
>> >
>>
>>


Re: Variable values as variable names by DevonWerkheiser

DevonWerkheiser
Tue Oct 14 06:35:01 CDT 2008

Hey Bill,

You're awesome, man!! It totally worked! This is exactly what I want.

It's quite complex, but it does the job. At least I have something to play
with. :)

Thanks a zillion!
Devon


"Bill McCarthy" wrote:

> Hi Devon,
>
> For an example, add a script control to a form and paste this code into some
> button click event or similar:
>
>
> Dim foo As String
> Dim bar As String
> foo = "text"
> bar = "box"
>
> With ScriptControl1
> .AddCode ("textbox = ""HelloWorld""")
> MsgBox .Eval(foo & bar)
> End With
>
>
> There, I've used AddCode. But if TextBox1 was a control, you could add that
> via AddObject, such as
> ..AddObject "TextBox1", Me.Text1
>
> Note, you can give the object reference any name you want for the script
> control to use in it's eval. Have a play with it, it's very powerful.
>
>
>
> "DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> wrote in
> message news:73C0665B-9F46-4F98-AE4A-72C088A13100@microsoft.com...
> > Thanks Bill,
> >
> > but i'm not really sure on the 'add the objects' thing. First of all,
> > they're all really just variables, not objects.
> >
> > But I am familiar with the eval of Microsoft Scripting, but I understand
> > it
> > only evaluates mathematical expressions, but not variables used in the VB
> > program itself.
> >
> > Hey, I appreciate your reply! It'd be great if you can help me out
> > further.
> >
> >
> > "Bill McCarthy" wrote:
> >
> >> Hi Devon,
> >>
> >> You could add a reference to Microsoft Script Control and use it's Eval
> >> method. You'd have to add the objects to it first.
> >>
> >>
> >>
> >> "DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> wrote in
> >> message news:4A96EB36-4C23-4EEA-9B1F-25F06A15A3C1@microsoft.com...
> >> >I want to make a program with an 'Immediate Window', where you can query
> >> >the
> >> > variable value by typing the variable name. The problem is, I don't
> >> > know
> >> > how
> >> > to display the contents of a variable name written in the said
> >> > Immediate
> >> > Window.
> >> >
> >> > For example, I have a variable named 'username' in the program. If I
> >> > wanted
> >> > to query the value of the variable during runtime, I would just type
> >> > the
> >> > variable name (here, 'username') and the immediate window will display
> >> > the
> >> > value.
> >> >
> >> > Or:
> >> >
> >> > username = "devon"
> >> > variablename = "username"
> >> >
> >> > msgbox val(variablename)
> >> >
> >> > Basically, I will evaluate the contents of variablename and treat it as
> >> > a
> >> > variable name,
> >> > which would then become the 'username' variable, which will return
> >> > 'devon'.
> >> >
> >> > It's like the Eval function, I think:
> >> >
> >> > textbox = "HelloWorld"
> >> > foo = "text"
> >> > bar = "box"
> >> > msgbox eval(foo & bar)
> >> >
> >> > returns "HelloWorld"
> >> >
> >> >
> >> > Any ideas?
> >> > Thanks!
> >> >
> >> >
> >> >
> >>
> >>
>
>

Re: Variable values as variable names by Bill

Bill
Tue Oct 14 06:47:09 CDT 2008

You're welcome :)

"DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> wrote in
message news:2AB1A894-CE58-4478-A10A-EE9CC36E329F@microsoft.com...
> Hey Bill,
>
> You're awesome, man!! It totally worked! This is exactly what I want.
>
> It's quite complex, but it does the job. At least I have something to play
> with. :)
>
> Thanks a zillion!
> Devon
>
>
> "Bill McCarthy" wrote:
>
>> Hi Devon,
>>
>> For an example, add a script control to a form and paste this code into
>> some
>> button click event or similar:
>>
>>
>> Dim foo As String
>> Dim bar As String
>> foo = "text"
>> bar = "box"
>>
>> With ScriptControl1
>> .AddCode ("textbox = ""HelloWorld""")
>> MsgBox .Eval(foo & bar)
>> End With
>>
>>
>> There, I've used AddCode. But if TextBox1 was a control, you could add
>> that
>> via AddObject, such as
>> ..AddObject "TextBox1", Me.Text1
>>
>> Note, you can give the object reference any name you want for the script
>> control to use in it's eval. Have a play with it, it's very powerful.
>>
>>
>>
>> "DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> wrote in
>> message news:73C0665B-9F46-4F98-AE4A-72C088A13100@microsoft.com...
>> > Thanks Bill,
>> >
>> > but i'm not really sure on the 'add the objects' thing. First of all,
>> > they're all really just variables, not objects.
>> >
>> > But I am familiar with the eval of Microsoft Scripting, but I
>> > understand
>> > it
>> > only evaluates mathematical expressions, but not variables used in the
>> > VB
>> > program itself.
>> >
>> > Hey, I appreciate your reply! It'd be great if you can help me out
>> > further.
>> >
>> >
>> > "Bill McCarthy" wrote:
>> >
>> >> Hi Devon,
>> >>
>> >> You could add a reference to Microsoft Script Control and use it's
>> >> Eval
>> >> method. You'd have to add the objects to it first.
>> >>
>> >>
>> >>
>> >> "DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> wrote in
>> >> message news:4A96EB36-4C23-4EEA-9B1F-25F06A15A3C1@microsoft.com...
>> >> >I want to make a program with an 'Immediate Window', where you can
>> >> >query
>> >> >the
>> >> > variable value by typing the variable name. The problem is, I don't
>> >> > know
>> >> > how
>> >> > to display the contents of a variable name written in the said
>> >> > Immediate
>> >> > Window.
>> >> >
>> >> > For example, I have a variable named 'username' in the program. If I
>> >> > wanted
>> >> > to query the value of the variable during runtime, I would just type
>> >> > the
>> >> > variable name (here, 'username') and the immediate window will
>> >> > display
>> >> > the
>> >> > value.
>> >> >
>> >> > Or:
>> >> >
>> >> > username = "devon"
>> >> > variablename = "username"
>> >> >
>> >> > msgbox val(variablename)
>> >> >
>> >> > Basically, I will evaluate the contents of variablename and treat it
>> >> > as
>> >> > a
>> >> > variable name,
>> >> > which would then become the 'username' variable, which will return
>> >> > 'devon'.
>> >> >
>> >> > It's like the Eval function, I think:
>> >> >
>> >> > textbox = "HelloWorld"
>> >> > foo = "text"
>> >> > bar = "box"
>> >> > msgbox eval(foo & bar)
>> >> >
>> >> > returns "HelloWorld"
>> >> >
>> >> >
>> >> > Any ideas?
>> >> > Thanks!
>> >> >
>> >> >
>> >> >
>> >>
>> >>
>>
>>


Re: Variable values as variable names by Larry

Larry
Tue Oct 14 07:08:35 CDT 2008


"DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> wrote

> It's quite complex, but it does the job. At least I have something to play
> with. :)

You can do a similar task using intrinsic VB objects. Add a class to a
new project and add in the next two lines into its code module:

Public Name As String
Public Value As String


Then paste this into your form:

Option Explicit
Public Vars As Collection

Private Sub Form_Initialize()
Set Vars = New Collection
End Sub

Private Sub Form_Load()
NewVariable "Textbox", "Hello World"
MsgBox Vars("Text" & "Box").Value
End Sub

Sub NewVariable(Name As String, Value As String)
Dim v As New Class1
v.Name = Name
v.Value = Value
Vars.Add v, v.Name
End Sub

Private Sub Form_Terminate()
Set Vars = Nothing
End Sub


Just another way to do that job...
LFS



Re: Variable values as variable names by Eduardo

Eduardo
Tue Oct 14 17:00:55 CDT 2008

If you declare the variables as Public, you can use the CallByName function.

Example 1 (in a form):
*******************************************
Public username As String
Public userId As Long

Private variablename As String

Private Sub Command1_Click()
username = "devon"
userId = 1234

variablename = "username"
MsgBox CallByName(Me, variablename, VbGet)

variablename = "userId"
MsgBox CallByName(Me, variablename, VbGet)
End Sub
*******************************************

Example 2 (in a form):
*******************************************
Public TextBox As String

Private Sub Command1_Click()
Dim foo As String
Dim bar As String

TextBox = "HelloWorld"

foo = "text"
bar = "box"
MsgBox CallByName(Me, foo & bar, VbGet)
End Sub
*******************************************

"DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> escribió en el
mensaje news:4A96EB36-4C23-4EEA-9B1F-25F06A15A3C1@microsoft.com...
>I want to make a program with an 'Immediate Window', where you can query
>the
> variable value by typing the variable name. The problem is, I don't know
> how
> to display the contents of a variable name written in the said Immediate
> Window.
>
> For example, I have a variable named 'username' in the program. If I
> wanted
> to query the value of the variable during runtime, I would just type the
> variable name (here, 'username') and the immediate window will display the
> value.
>
> Or:
>
> username = "devon"
> variablename = "username"
>
> msgbox val(variablename)
>
> Basically, I will evaluate the contents of variablename and treat it as a
> variable name,
> which would then become the 'username' variable, which will return
> 'devon'.
>
> It's like the Eval function, I think:
>
> textbox = "HelloWorld"
> foo = "text"
> bar = "box"
> msgbox eval(foo & bar)
>
> returns "HelloWorld"
>
>
> Any ideas?
> Thanks!
>
>
>



Re: Variable values as variable names by Harvey

Harvey
Wed Oct 15 11:38:55 CDT 2008

Another apprach:

Private Sub Form_Load() ' sample
AddVariable "Animal", "Dog"
MsgBox GetValue("Animal")
PutValue "Animal", "Cat"
MsgBox GetValue("Animal")
End Sub

How to (short version):
- Add new dll activex project
- Instancig of class1: GlobalMultiUse
(name os class1 is optional)
- Add this code to class:

Option Explicit

Private Type structVariable
Name As String
Value As String
End Type
Private a() As structVariable
Private n As Long

Public Sub AddVariable(Name As String, Optional Value As String = "")
n = n + 1
ReDim Preserve a(1 To n)
a(n).Name = Name
a(n).Value = Value
End Sub

Public Function GetValue(Name As String) As String
Dim i As Long

For i = 1 To n
If a(i).Name = Name Then
GetValue = a(i).Value
End If
Next
End Function

Public Sub PutValue(Name As String, v As String)
Dim i As Long

For i = 1 To n
If a(i).Name = Name Then
a(i).Value = v
End If
Next
End Sub

<Harvey Triana>
http://vexpert.mvps.org

"Larry Serflaten" <serflaten@usinternet.com> escribió en el mensaje
news:exe8DWfLJHA.468@TK2MSFTNGP06.phx.gbl...
>
> "DevonWerkheiser" <DevonWerkheiser@discussions.microsoft.com> wrote
>
>> It's quite complex, but it does the job. At least I have something to
>> play
>> with. :)
>
> You can do a similar task using intrinsic VB objects. Add a class to a
> new project and add in the next two lines into its code module:
>
> Public Name As String
> Public Value As String
>
>
> Then paste this into your form:
>
> Option Explicit
> Public Vars As Collection
>
> Private Sub Form_Initialize()
> Set Vars = New Collection
> End Sub
>
> Private Sub Form_Load()
> NewVariable "Textbox", "Hello World"
> MsgBox Vars("Text" & "Box").Value
> End Sub
>
> Sub NewVariable(Name As String, Value As String)
> Dim v As New Class1
> v.Name = Name
> v.Value = Value
> Vars.Add v, v.Name
> End Sub
>
> Private Sub Form_Terminate()
> Set Vars = Nothing
> End Sub
>
>
> Just another way to do that job...
> LFS
>
>