Solved

How to formulate a pointer to an object

Posted on 2004-09-03
6
205 Views
Last Modified: 2012-03-15
Hi experts,
I'd like to access an object whose reference is based on contents of one or more strings. To help explain what I mean I'll give you an example.

I have a string variable called 'ObjectName', that will contain the name of the object that I want to access, and a variable called 'PropertyName, that will contain the name of the property within an object. What I'd like to do is basically combine the contents of those two strings to form a reference/pointer to the actual object property, as per below:

Dim ObjectName as String
Dim PropertyName1 as String
Dim PropertyName2 as String

     pointer?(ObjectName & PropertyName1) = "12345"
     pointer?(ObjectName & PropertyName2) = "ABC12345"
                            or
     set pointer?(ObjectName & PropertyName1) = xxxxxx
     set pointer?(ObjectName & PropertyName2) = xxxxxx


The above syntax examples are incorrect but serve the purpose of explaining (I hope) what I'm trying to achieve.

Can it be done?

/Michael
0
Comment
Question by:gem56
  • 2
  • 2
  • 2
6 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 11973502
How would you know which instance of the object to access if you only have a string with its name in?

Have you looked at the 'CallByName' function to see if that meets your requirements?
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11973520
Put htis code in form. This will set text1 property of text1 object

Private Sub Command1_Click()
Dim myObj As Object
    Set obj = Me.Controls("Text1")
    If obj Is Nothing Then
        MsgBox "Can not find control"
    Else
        CallByName obj, "Text", VbLet, "Some text"
    End If
End Sub
0
 

Author Comment

by:gem56
ID: 11974891
Hi mladenovicz,
Thanks for your response however I know the syntax for Form objects, which is as per your feedback, but what I was referring to is class objects. I think that JR2003 may be on teh right track.
/Michael
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 8

Expert Comment

by:mladenovicz
ID: 11974932
I think that you will have to store objects in collection in order to access them by name.
However, CallByname is the way to set/get/let property or call method of object
0
 

Author Comment

by:gem56
ID: 11975045
Hi JR2003,
I just had a quick look at help for your suggestiopn ('CallByName') and I think that you may be right however I don't have any examples so I'll have to try it out. If my initial understanding is right would I use that function in the following way for my example above?

Dim ObjectName as String
Dim PropertyName1 as String
Dim PropertyName2 as String

     CallByName(ObjectName, PropertyName1,vbLet,"12345")
     CallByName(ObjectName, PropertyName2,vbLet,"ABC12345")
                            or
     CallByName(ObjectName, PropertyName1,vbSet,xxxxxx)      ' where xxxxxxx is an object
     CallByName(ObjectName, PropertyName2,vbSet,xxxxxx)

Cheers,
     Michael
0
 
LVL 18

Accepted Solution

by:
JR2003 earned 250 total points
ID: 11975214
gem56,

Here's the help for the CallByName function.
ObjectName is the actual reference to the object not a string containing the name of the object.
So where you would normally hard-code say:

You could code this:
s = MyObject.StartTime

as:

Dim sFuncName as string
sFuncName = "StartTime"
s = CallByName(MyObject, sFuncName, vbGet)

It gives you flexibility to code function calls in a "soft" way.

JR



CallByName Function
     
Executes a method of an object, or sets or returns a property of an object.

Syntax

CallByName(object, procname, calltype,[args()])

The CallByName function syntax has these named arguments:

Part Description
object Required; Variant (Object). The name of the object on which the function will be executed.

procname Required; Variant (String). A string expression containing the name of a property or method of the object.

calltype Required; Constant. A constant of type vbCallType representing the type of procedure being called.

args() Optional: Variant (Array).


Remarks

The CallByName function is used to get or set a property, or invoke a method at run time using a string name.

In the following example, the first line uses CallByName to set the MousePointer property of a text box, the second line gets the value of the MousePointer property, and the third line invokes the Move method to move the text box:

CallByName Text1, "MousePointer", vbLet, vbCrosshair
Result = CallByName (Text1, "MousePointer", vbGet)
CallByName Text1, "Move", vbMethod, 100, 100

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA: Personal Macro Retain/Highlight/Remove values in a selected column 4 36
How to make an ADE file by code? 11 102
vbModal 12 66
pop out of webbrowser1 control vba6 5 44
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question