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

Access the content of a variable using its name

Status
Not open for further replies.

nath

Programmer
Dec 5, 2001
109
ES
How can I access the content of a variable/constant X when I use a variable Y which contains the name of the first variable, i.e. X?
Similarly how can I run a function/procedure if I call it using a variable containing its name?
I know there are many alternative ways to do that, but I'd like to know if someone is aware of how to do exactly what I need (no public variables, no arrays).

I want to do something like the following:

Code:
'*******
Public const MYCONST1 = "Portfolio1.txt"
Public const MYCONST2 = "Portfolio2.txt"

Sub Test()
	Dim i as Integer

	For i = 1 to 2
		Open "MYCONST" & i For Input as #1
	
		'I want it to open Portfolio1.txt and 
                'Portfolio2.txt but it will try to open 
		'MYCONST1 and MYCONST2 instead, which are 
                'not existing files!
	Next i
End Sub
'*******

A similar problem is:

Sub Test2()
	...
	sType = "Rectangle"	
	...
	Call "Print" & sType 	

       'I want it to call the procedure PrintRectangle
End Sub

Sub PrintRectangle()
	...
End Sub

'********************

Thank you for your help.

Nath.
 
Nath,
The first one is easy - just remove the double quotes around MYCONST, to convert it from a literal string to a variable reference - that should work just fine.
The second one I'm not sure you can do that way - I would write something like:

sub PrintShape(ShapeType as string, ...)
select case ShapeType
case "rectangle":printRectangle(...)
'etc
end select
end sub

Rob
 
Hi Rob,

Thanks for your input.

However for the first bit, if I remove the quotes around MYCONST it is going to search for a variable called MYCONST, which doesn't exist.

For the second question it is indeed what I do at the moment. But if it could be done differently, I'd be able to cover all cases with one line instead of one call for each case.

Thanks again though,

Nath
 
Hi Nath!

In case #1 use:

Public Const MyFile "Portfolio"
Public Const MyExt ".txt"

Dim strFileName As String
Dim intCounter As Integer

For intCounter = 1 to 2
strFileName = MyFile & Format(intCounter) & MyExt
Open strFileName etc.
Next intCounter

In Case #2 you can set up a control procedure that will accept your string and has a select case which will choose the proper print procedure to call. Alternatively you can a set of macros appropriately and then use the DoCmd.RunMacro YourVariable to call the macro. In the macros you can use the RunCode action to call your procedures.

hth Jeff Bridgham
bridgham@purdue.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top