Solved

How to formulate a pointer to an object

Posted on 2004-09-03
6
202 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help in WHSCRIPT 9 47
VBA Word macro - how to get characters after the searched for string 5 67
Updates not working for MS Windows 7 12 152
using web browser with BING 40 110
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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

914 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now