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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need contents of variable whose name is in another variable

Status
Not open for further replies.

WaltW

MIS
Jun 14, 2000
130
US
I have a situation where I have the name of one variable in a second variable, but I want to look at and use the contents of the first variable. Example:

Code:
dim strVarName as string
strVarName = "txtData"

If txtData has information in it that I want to use, but all I have to go on is the fact that the contents of strVarName is "txtData", how do I use strVarName to get the contents of txtData?

Thanks for any help you can provide!

WaltW
 
Hi Walt,

In your example... strVarName = "txtData"

By placing the quotation marks around the text (txtData), you are assigning that text to the variable (strVarName).

If you want to capture the contents of the variable called txtData, here is one example that will enter the contents of the txtData variable into the current cell...

Sub Enter_TxtData()
txtData = "This is sample data."
ActiveCell.Value = txtData
End Sub

If you want to pass the contents of the txtData variable to the strVarName variable, then the following could be used...

Sub Get_TxtData()
Dim strVarName As String
txtData = "This is sample data."
strVarName = txtData
MsgBox strVarName
End Sub

I hope this helps. :) Please advise as to how you make out.

Regards, ...Dale Watson dwatson@bsi.gov.mb.ca
 
Hi,
Don't know of any substitution method. But here's a way...
Code:
Select Case strVarName 
   Case "txtData"
      WhatData = txtData 
   Case ...
      WhatData = ...
End Select
Not very elegant. Maybe some other Tipsters have a better solution. :) Skip,
SkipAndMary1017@mindspring.com
 
Thanks for the suggestions. In my case, "txtData" is actually the name of a text box control on a form in MS Access 97. What I'm wanting to do is generate the name of the text box control within VBA, then be able to use that generated name to find the contents of that text box. The reason for this is I'll have 20-30 text boxes on a form, all named something like "txtData01", "txtData02", ... "txtData30". Since the names of the text boxes can be easily generated, I want to generate the name and find the contents of the text box rather than having to deal with each of the 20-30 text box names individually. Someone in another forum suggested using Form.Controls(strVarName), which seems to work OK. Any other thoughts? Thanks.

WaltW
 
try something like:

Code:
dim strVarName as string

strVarName = "txtData"

Debug.Print Me.Controls(strVarName).Value
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top