Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I pass a property to a sub?

Posted on 2000-05-02
15
Medium Priority
?
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 

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
 
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 200 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses

722 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