Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting object property values

Status
Not open for further replies.

jpastika

MIS
Jan 30, 2002
10
US
I am trying to set the value of each property of an object. However, I do not want to have to hardcode the call to each property individually. I am trying to pass in a recordset of all of the property names and values. The object's property names match the recordset field names. I keep getting a "method or data member not found" error. Here is the code I am trying:

-----------------------------------------
maxFields = rs_MetaData.Fields.Count

For i = 0 To maxFields - 1
'Dynamically reference m_objMetaData properties using the name of each field in the rs_MetaData recordset

m_objMetaData.[rs_MetaData.Fields(i).Name] = rs_MetaData.Fields(i).Value

Next
------------------------------------------

The problem is "m_objMetaData.[rs_MetaData.Fields(i).Name]". I don't know the correct syntax for dynamically referencing the object's property name. Any suggestions would be greatly appreciated.

-Jeremy
 
Try
Code:
m_objMetaData.Fields(rs_MetaData.Fields("PropertyName").Value).Value = rs_MetaData.Fields("PropertyValue").Value

This assumes you have two fields in rs_MetaData called PropertyName and PropertyValue.

I assume that all fields have the same type? Otherwise you will run into trouble. You could get around it by having a third field in rs_MetaData that stores the data type, store all of the data values as text, then use the data type field to conditionally convert the data.
e.g.
Code:
Dim V As Variant
...
V = rs_MetaData.Fields("PropertyValue").Value
Select Case rs_MetaData.Fields("DataType").Value
Case 1: 'Long
    V = CLng(V)
Case 2: 'String
    V = CStr(V)
...
m_objMetaData.Fields(rs_MetaData.Fields("PropertyName").Value).Value = V
 
Thank you for the response. Unfortunately because "m_objMetaData" is a class object, not a recordset, referencing ".Fields()" does not work. Unless I am doing something else wrong.
 
I GOT IT!!!

I had to use the CallByName function. Here is the new code:

-----------------------------------------
maxFields = rs_MetaData.Fields.Count

For i = 0 To maxFields - 1
'Dynamically reference m_objMetaData properties using the name of each field in the rs_MetaData recordset

CallByName m_objMetaData, rs_MetaData.Fields(i).Name, VbLet, rs_MetaData.Fields(i).Value

Next
------------------------------------------

This works beautifully. Now all I need to add it the data type handler, as you suggested Norris68. Thanks!

-Jeremy

 
If it's a class object, then you could write a method to update the properties.

e.g.
Public Sub UpdateProperty(PropertyName As String, PropertyValue As Variant)
Select Case PropertyName
...
End Sub

... then call it ...

m_objMetaData.UpdateProperty rs_MetaData.Fields("PropertyName").Value, rs_MetaData.Fields("PropertyValue").Value

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top