Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building dynamic statements in VB

Status
Not open for further replies.

thampik

Programmer
Mar 5, 2003
8
US

Hello,

Is it possible to build and execute dynamic code statements in VB?
For example, consider the statement
Me!txt_Name1.Value = "Name1"

The text control could have been a different one from "txt_Name1" depending on certain criteria. The statement could as well have been
Me!txt_Name2.Value = "Name2"

Is it possible to use a string to hold the entire code statement like

Dim strCode as string
Dim strTxtFieldName as String

If <condition 1> Then
strTxtFieldName = &quot;txt_Name1&quot;
Else
strTxtFieldName = &quot;txt_Name2&quot;
End If

strCode = &quot;Me!&quot; & strTxtFieldName & &quot;.Value = 'Name1'&quot;

and execute this statement?

The reason for asking is that the form I am working with has about 100 text fields and the criteria is quite complicated that if I have to hard code the text field names with IF..THEN..ELSE or SELECT..CASE statements, the code can become a real mess.

Thanks
 
The &quot;Eval&quot; function is well documented in Help. The basic functionality can be achieved, but you need to revise the process a bit.




MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Hey MichealRed, great call on the Eval function.

The only other thing that I could add. For the first part where you are calling columns in a table.

Instead of:
[tt]
Me!txt_Name1.Value = &quot;Name1&quot;
[/tt]
You could use:
[tt]
Me.Field(Column Name Variable Here).Value = &quot;Name1&quot;
[/tt]

Craig, mailto:sander@cogeco.ca

In the computer industry, there are three kinds of lies:
lies, damn lies, and benchmarks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top