Can I exxecute a String?

R

RayC

Is it possible to execute a String? e.g. if I type in something like
"TextBox1.Visible = True",
the code executes that line and makes the TextBox1 Visible. What I am asking
is would it be possible to have strString1 ="TextBox1" and strString2 =
"Visible" then concatenate the two strings into strExecute = str1 & "." &
strString2 to leave strExecute = "Textbox1.Visible".
Whilst .Visible is probably not the best example but if there was a way to
execute that string, I could then have a very flexible Sub routine where I
could pass the required arguments to it and have the routine do some very
mundane work.
e.g.
Call util_Clearform(Forms.form1, Visible, False)

Public Sub util_ClearForm(frm As Form, Opps as String. Stat As Boolean)

Dim Cntrl As Control

For Each Cntrl In frm
' this is the bit I am enquiring about
Then Cntrl.Enabled = Stat
Next Cntrl

End Sub

Is there ant way I could execute something like
Then cntrl & "." & opps = Stat

If you understand what I am saying
Thaks RayC
 
D

Douglas J. Steele

Me.Controls(strString1).Properties(strString2) would give you what you
specifically asked about.

Why have you got the Then inside the For loop? Then is only used in
conjunction with If statements.

Try:

Public Sub util_ClearForm(frm As Form, Opps as String. Stat As Boolean)

Dim Cntrl As Control

For Each Cntrl In frm
Cntrl.Properties(Opps) = Stat
Next Cntrl

End Sub

You have to change how you call it from what you've got to

Call util_Clearform(Forms.form1, "Visible", False)

Note that you need to be very careful which property you pass to that
routine: not all properties apply to all controls. For example, if you
passed "Enabled" as the property, it would fail on Labels.
 
R

RayC

Hi Doug, you are briliant. You don't know how long and hard I have lookes for
suc a solution. Though I am sure the answer was in most of books I searched,
it was probably my naevity that prevented me from understanding what was
written.
You are quite right, my inclusion of the "Then" statement in my guess as to
the solution was an error on my part, sorry.
Once again, thanks
RayC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top