referencing an unknown object

Fantast

Registered User.
Local time
Today, 22:40
Joined
Dec 13, 2011
Messages
41
Hi. I have this (hopefully) easy to solve question which has been bothering me for some time now. I usually work my way around it, but would like to have an actual solution for it.

So now and then I need to reference an object whose name is stored in a varaiable. As example, say I have a listbox whose name is stored in strListboxName. How can I change it's recordset? Is there a general way to reverence objects through a variable holding their name?

I know that you can reference a form by forms(strFormName), but there is no lists(strListName) or labels(strLabelName)... In my opinion there should be a way to access anything through their name, even a string: strings(strStringName). My main expertise lays within PHP and it is very doable there~

Thanks in advance for any help.

PS. I'm using Access 2007 VBA
 
Try the following:
Code:
'declare the variable
Dim strObjName as String
'object name assigned to a variable
strObjName = "NameOfControl"
'object referred to using the variable
Forms!FormName.Controls(strObjName)

Using this you can refer to the object and its properties.

Just change "FormName" to the actual name of your form.
 
Thanks a lot for your quick reply Mr. B. I tried your suggestion and it works great. However, this will work for form objects, but what if it is an object which isn't a control of a form?
 
Objects are in collections, and you can refer to a specific member of a collection in a similar manner.
 
I see. Is there an easy way to find out what collection an object is part of?
 
Logic.

What is the specific issue you are having?
 
Well logic usually comes from experience. And my experience in VBA is 5 weeks old now :)

To be honest my specific issue was solved by Mr. B's suggestion, as it was about form controls. However, I can imagine coming into a situation where I would like to do operations on a string variable by example, where the name of the string variable itsself is stored in a string. Or trying to call a function, whose name is stored in a string. Or trying do call a class whose name is in a string. That is the kind of situations I'm interested in to see how VBA deals with them~
 
I think you should cross that bridge when you get there. But to pass time, you can have a look at the function Eval, that in many cases, but not all, can do what you want.

And a bit of a warning: There are some programmers here that come from a different environment and try to fit Access into their perception of the world, at any cost. That often simply is not worth it, because they wind up with complex, and unmaintainable by anyone else, overhead. If you have coding experience, than you know that there most often are many ways to skin a cat. Choose what fits the situation, not what you used to do in some other environment.
 
Last edited:
I completely agree with your last statement. However, to be able to choose the right skinning method, you need to know more than one method.

The Eval function seems like what I was looking for indeed. Thanks for your help and patience :)
 

Users who are viewing this thread

Back
Top Bottom