Running a string as a line of code.

  • Thread starter Thread starter Steven
  • Start date Start date
S

Steven

Is there a way to run a string variable as a line of
code. I am not talking about DoCmd.RunSQL ........ but a
basic line of code.

This issue arrises out of the possible combination of
possibilities and I do not want to catch each item
individually but be able to loop and change a variable and
run some code.

For example:
I want to say:
Dim varStringToRun as String
varStringToRun = "Me.Co" + rs("CoNumberCode") + ".Caption
= '" & rs("Co") & "'"
This might result in:
varStringToRun = "MeCo001.Caption = 'CIS'"
Is there a way to run this varStringToRun like a line of
code that would be the same as saying
MeCo001.Caption = "CIS" on a line of code by itself.

Thank you for your help.

Steven
 
Try

Me.Controls("Co" & rs("CoNumberCode")).Caption = ????

You might also look at help for Eval function, although it isn't useful for
your specific situation

BTW...it is a bad idea to use + when concatenating strings....you may see
unexpected results. Use & instead.
 
Not exactly. You can use variables to fill in field names and control names, but not an
entire line of code. Using what you have, something similar to this ought to work.

Me.Controls("Co" & rs("CoNumberCode")).Caption = "'" & rs("Co") & "'"

This would place the value of rs("Co") in the Caption of Me.Co001 if CoNumberCode was 001.
 
Thank you very much, Thats perfect.
-----Original Message-----
Not exactly. You can use variables to fill in field names and control names, but not an
entire line of code. Using what you have, something similar to this ought to work.

Me.Controls("Co" & rs("CoNumberCode")).Caption = "'" & rs ("Co") & "'"

This would place the value of rs("Co") in the Caption of
Me.Co001 if CoNumberCode was 001.
 
Back
Top