Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

How can I pass a property to a sub?

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
BALAJI
Asked:
BALAJI
1 Solution
 
BALAJIAuthor Commented:
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
 
JetScootrCommented:
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
 
JetScootrCommented:
Sorry...I should have refreshed prior to posting...hang on, new code coming up....
0
Technology Partners: 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!

 
BALAJIAuthor Commented:
Ill wait... :)
0
 
JetScootrCommented:
Sorry...I should have refreshed prior to posting...hang on, new code coming up....
0
 
JetScootrCommented:
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
 
BALAJIAuthor Commented:
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
 
JetScootrCommented:
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
 
JetScootrCommented:
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
 
BALAJIAuthor Commented:
Adjusted points from 30 to 50
0
 
BALAJIAuthor Commented:
thanks.... ill up the points to 50
0
 
amebaCommented:
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
 
GordonpCommented:
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
 
JetScootrCommented:
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
 
BALAJIAuthor Commented:
Perfect!
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now