Solved

How to formulate a pointer to an object

Posted on 2004-09-03
6
201 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

12 Experts available now in Live!

Get 1:1 Help Now