Solved

How can I pass a property to a sub?

Posted on 2000-05-02
15
162 Views
Last Modified: 2013-11-25
I have a public method in a class mod, that i wish to pass a property to. I cant seem to figure out how though. If you look at the syntax for the .Add method of a BindingCollection object, you will see the propertyname passed as a string. But some how in the code it changes that string to a real property or something because it has to bind to that property of the passed object somehow, and that tells it which property to bind to. I know how to pass a string but one i have it in the procedure  how can i make it into an effective property?
0
Comment
Question by:BALAJI
15 Comments
 

Author Comment

by:BALAJI
ID: 2771390
After reading my question i realize it may be confusing. Here i will try to explain it again:

I want to pass an object and the name of a property that belongs to that object to a sub, then in that sub i want it to read/write to the passed property of the passed object. Below is non working code of the idea i wish to accomplish, please give me working code:

Private Sub Command1_Click()

   MySub(Text1, "Text")
   Mysub(Label1, "Caption")

End Sub

Private Sub MySub(Object as Object, PropertyName as String)

   Object.PropertyName = "Hello World"
End Sub


-----Output would be that the Text1.Text and the Label1.Caption would both say "Hello World"

So how do i do it :)

0
 
LVL 2

Expert Comment

by:JetScootr
ID: 2771392
Create a new project, add DAO or ADO of your choice.  This is to get an object that encapsulates properties already.  You could of course use your own object, if it already has properties.  ANyway, paste the following code in as an example.  Scope doesn't affect the function call (except of course regarding the syntax you use when calling it!)
Note that I point to my own location for the Northwind sample database that we all know and love.


Private Sub Form_Load()
MySub
End Sub


Sub MySub()
Dim MyProp As Property
Dim MyDB As Database
Set MyDB = OpenDatabase("E:\devstudio\vb\nwind.MDB")
Set MyProp = MyDB.Properties(0)
myClassFunction MyProp
End Sub


Function myClassFunction(ByRef GimmeProp As Property)
Debug.Print GimmeProp.Name

End Function
0
 
LVL 2

Expert Comment

by:JetScootr
ID: 2771396
Sorry...I should have refreshed prior to posting...hang on, new code coming up....
0
 

Author Comment

by:BALAJI
ID: 2771400
Ill wait... :)
0
 
LVL 2

Expert Comment

by:JetScootr
ID: 2771422
Sorry...I should have refreshed prior to posting...hang on, new code coming up....
0
 
LVL 2

Expert Comment

by:JetScootr
ID: 2771428
OK, here we go again...note that not all objects support a properties collection.  Cuzzathis, I included a database, cuz databases DO have properties collection.  Textboxes, labels, etc probably DONT.  ANyway, you need a new project, a reference to DAO, and a Command button.

Option Explicit
Dim MyDB As Database
'

Private Sub Command1_Click()
Prop_Handler MyDB, "StartUpMenuBar"
End Sub

Private Sub Form_Load()
Set MyDB = OpenDatabase("E:\DevStudio\VB\NWind.MDB")
End Sub
'
     
Sub Prop_Handler(Myobject As Object, PropName As String)
Dim ThisProp As Property

For Each ThisProp In Myobject.Properties
    If ThisProp.Name <> "Connection" Then
        Debug.Print ThisProp.Name, , ThisProp.Value
        If ThisProp.Name = PropName Then
            Debug.Print "I found it!"
        End If
    Else
        Debug.Print ThisProp.Name
    End If
Next
End Sub

   
0
 

Author Comment

by:BALAJI
ID: 2771436
I can see how this works with a property collection but how would it work if an object didnt support property collections such as a textbox and label? Because those are the type of controls i need this to work for. There must be a way because the BindingCollection.Add passes the name in string format of the property you wish to bind to the database. And it must use that property somehow.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Expert Comment

by:JetScootr
ID: 2771446
OK, here we go again...note that not all objects support a properties collection.  Cuzzathis, I included a database, cuz databases DO have properties collection.  Textboxes, labels, etc probably DONT.  ANyway, you need a new project, a reference to DAO, and a Command button.

Option Explicit
Dim MyDB As Database
'

Private Sub Command1_Click()
Prop_Handler MyDB, "StartUpMenuBar"
End Sub

Private Sub Form_Load()
Set MyDB = OpenDatabase("E:\DevStudio\VB\NWind.MDB")
End Sub
'
     
Sub Prop_Handler(Myobject As Object, PropName As String)
Dim ThisProp As Property

For Each ThisProp In Myobject.Properties
    If ThisProp.Name <> "Connection" Then
        Debug.Print ThisProp.Name, , ThisProp.Value
        If ThisProp.Name = PropName Then
            Debug.Print "I found it!"
        End If
    Else
        Debug.Print ThisProp.Name
    End If
Next
End Sub

   
0
 
LVL 2

Expert Comment

by:JetScootr
ID: 2771455
sorry about that...EE seems to be reposting my comments...not sure why.
If the object doesn't support a Properties collection, then the properties cannot be symbollically addressed using a "Property" type variable.
Let me research the controls a little...this may take till tomorrow...but it's an interesting question....
0
 

Author Comment

by:BALAJI
ID: 2771458
Adjusted points from 30 to 50
0
 

Author Comment

by:BALAJI
ID: 2771459
thanks.... ill up the points to 50
0
 
LVL 15

Accepted Solution

by:
ameba earned 50 total points
ID: 2771613
Option Explicit

Private Sub Command1_Click()
    Call MySub(Text1, "Text")
    Call MySub(Label1, "Caption")
End Sub

Private Sub MySub(Object As Object, PropertyName As String)
   'Object.PropertyName = "Hello World"
   CallByName Object, PropertyName, VbLet, "Hello World"
End Sub
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2772361
USe CallByName

Private Sub MySub(Object as Object, PropertyName as String)

CallByName Object, PropertyName, VbLet, "Hello World"

End Sub

the vbLet Constant tells CallByName that function PropertyName is a Property LEt procedure so the property PropertyName will be set to "Hello World"

Hope this Helps

Gordon
0
 
LVL 2

Expert Comment

by:JetScootr
ID: 2773473
If I understand the question right, you're wanting to pass a property name and an object to a sub.
The sub would then be able to modify the property's value.
I suspect you're thinking something like referencing the fields of a recordset or items in a collection, like this:
rsMyTable("MyField") = "MyValue"
colMyCollection("MyItem") = "MyValue"

From what I've been able to find, or actually, NOT find, in MSDN, docs, etc, there's no way to symbolically reference ( ("MyField") ) an object's properties other than by the "wrappers" provided by the object itself, such as a Properties collection, and the individual Property Let/Set/Get methods.
Perhaps if you tell us what capability you're trying to get at, we can come up with another way to achieve the same result.
0
 

Author Comment

by:BALAJI
ID: 2774560
Perfect!
0

Featured Post

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.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

758 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

21 Experts available now in Live!

Get 1:1 Help Now