By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,493 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,493 IT Pros & Developers. It's quick & easy.

How to get Object property using EVAL()?

P: 6
Can you figure out how to do this in Access2002/VBA?

I want to get the value of an object property. The trick is that the name of the property to retrieve is stored in a table.

Here's how I've set it up:

I have a class module "objPerson" which has a property "FirstName".
This works:
Expand|Select|Wrap|Line Numbers
  1. objPerson.FirstName = "John Smith"
  2. strFirstName = objPerson.FirstName
and it will correctly set
strFirstName = "John Smith"

So far, so good, right?

Next, in "MyTable" I have a field "MyField" which contains the text string "objPerson.FirstName"

I want to get the value of the item specified by the string in MyField. In other words, I want the program to determine the value of whatever object property that I have stored in MyField. In this case, I have opened the object objPerson and I want the program to give me the value of "objPerson.FirstName".

I've tried using Eval(), but so far it doesn't work:

Expand|Select|Wrap|Line Numbers
  1. strFirstName = Eval(rst.Fields("MyField"))
The result is: "Error 2482: Microsoft Access can't find the name 'objTenant' you entered in the expression."

MY OBJECTIVE

My objective is to do string replacement where the REPLACEMENT text is set in a table, like this:

Expand|Select|Wrap|Line Numbers
  1. In "MyTable":
  2.  
  3. Field:KEY                     Field:REPLACE_WITH
  4. -------------------------     -------------------------------------
  5. Tenant.FirstName              objTenant.FirstName
  6. Owner.FirstName               objOwner.FirstName
  7. Balance                       objInvoice.BalanceAmount
  8. (etc...)
  9.  
  10. strMsg = "Hello [Tenant.FirstName]. Your invoice balance is [Invoice.Balance]"
  11. strMsg = MySpecializedReplacementFunction(strMsg)
  12. strMsg: "Hello John. Your invoice balance is $10"
Nov 20 '06 #1
Share this Question
Share on Google+
13 Replies


100+
P: 1,646
Hi. What I would do is use the value in the table to activate a section of code in a Select Case statement;
Expand|Select|Wrap|Line Numbers
  1. Select Case rs.Fields("Field1")
  2.    Case "objPerson.FirstName"
  3.       strText = objPerson.FirstName
  4.  
  5.    Case "something else"
  6.       'peform whatever it says here
  7. End Select
  8.  
Hope this helps
Nov 20 '06 #2

P: 6
Thank you, Willakawill. That is a workable alternative.

The only disadvantage is that the fields would have to be hardcoded in the SELECT-CASE statement. Ideally, updating the list of string Keys and the Replacement text would be maintainable via a table.
Nov 20 '06 #3

nico5038
Expert 2.5K+
P: 3,072
For just the property this should work:

dim arr

arr = split(rs!Replace_with,".")

select case arr(0)
Case "objTenant"
strX = objTenant.properties(arr(1))
Case .... etc ...

The Split will fill an array (zero based!) with the two parts and arr(0) will hold the object and arr(1) the property.

Getting the idea ?

Nic;o)
Nov 20 '06 #4

P: 6
Hi nico5038,

I really like your idea. It allows the substitutions to be solved dynamically and that's important to me.
Nov 20 '06 #5

P: 6
Nico,

I've added the code but it doesn't like the .Properties in objTenant.Properties. I get an error:

Expand|Select|Wrap|Line Numbers
  1. Dim objTenant As pm_Tenant
  2. Set objTenant = New pm_Tenant
  3. objTenant.Load (TenantID)
  4.  
  5. Debug.Print "name1=" & objTenant.FullName
  6. ' ^ up to here it works. It correctly displays: Name1=Joe Smith
  7.  
  8. ' The following lines return an error:  "Compile error: Method or data member not found"
  9. Debug.Print "name2=" & objTenant.Properties("FullName")   
  10. Debug.Print "name3=" & objTenant.Properties!FullName
  11.  
Of course, I'm using the hard-coded "FullName" just to test it. Once it's working, I'll change to using your suggestion, .Properties(arr(1))
Nov 20 '06 #6

nico5038
Expert 2.5K+
P: 3,072
Hmm, strange. Are you sure the properties have been defined correctly ?

To test use:

objTenant.properties.count

This will get the number of properties and check the available by using:

objTenant.properties(0).Name

You can see the property names there are.

Nic;o)
Nov 21 '06 #7

P: 6
I tried and both statements give the error "method or data member not found". For some reason, my Access VBA doesn't understand ".properties":

objTenant.properties.count
objTenant.properties(0).FirstName

Maybe a reference needs to be set in Tools > References?
Am I setting the properties correctly? As you can see below, I have "Property Let" and "Property Get" procedures and they are working just fine. If I type "? objTenant.Name" I get the correct result.

Expand|Select|Wrap|Line Numbers
  1. Public Property Get FirstName() As String
  2.     FirstName = mstrFirstName
  3. End Property
  4.  
  5. Public Property Let FirstName(NewValue As String)
  6.     mstrFirstName = NewValue
  7. End Property
Nov 21 '06 #8

nico5038
Expert 2.5K+
P: 3,072
I'm startled, but have asked another expert to join.

Nic;o)
Nov 22 '06 #9

100+
P: 1,646

Expand|Select|Wrap|Line Numbers
  1. Public Property Get FirstName() As String
  2.     FirstName = mstrFirstName
  3. End Property
  4.  
  5. Public Property Let FirstName(NewValue As String)
  6.     mstrFirstName = NewValue
  7. End Property
OK here is another way you can do this using vb6 and the following class module. I have tested this and it works :)
Expand|Select|Wrap|Line Numbers
  1. 'MyDog.cls class module
  2. 'local variable(s) to hold property value(s)
  3. Private mvarDogName As String 'local copy
  4. Private mvarTailWags As Integer 'local copy
  5. Public Sub FeedDog(Optional NumCookies As Integer = 1)
  6.     mvarTailWags = mvarTailWags + Abs(NumCookies * 5)
  7. End Sub
  8.  
  9. Public Property Let TailWags(ByVal vData As Integer)
  10. 'used when assigning a value to the property, on the left side of an assignment.
  11. 'Syntax: X.TailWags = 5
  12.     mvarTailWags = vData
  13. End Property
  14.  
  15.  
  16. Public Property Get TailWags() As Integer
  17. 'used when retrieving value of a property, on the right side of an assignment.
  18. 'Syntax: Debug.Print X.TailWags
  19.     TailWags = mvarTailWags
  20. End Property
  21.  
  22.  
  23.  
  24. Public Property Let DogName(ByVal vData As String)
  25. 'used when assigning a value to the property, on the left side of an assignment.
  26. 'Syntax: X.DogName = 5
  27.     mvarDogName = vData
  28. End Property
  29.  
  30.  
  31. Public Property Get DogName() As String
  32. 'used when retrieving value of a property, on the right side of an assignment.
  33. 'Syntax: Debug.Print X.DogName
  34.     DogName = mvarDogName
  35. End Property
  36.  
  37. Public Property Get NewValue(stValue As String) As String
  38.     Select Case stValue
  39.         Case "DogName"
  40.             NewValue = mvarDogName
  41.         Case "TailWags"
  42.             NewValue = CStr(mvarTailWags)
  43.     End Select
  44. End Property
  45.  
then in a form module:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdFeedTheDog_Click()
  2.     Dim MyNewDog As MyDog
  3.     Set MyNewDog = New MyDog
  4.     MyNewDog.DogName = "Fido"
  5.     MyNewDog.FeedDog 5
  6.     MsgBox CInt(MyNewDog.NewValue("TailWags"))
  7. End Sub
  8.  
Nov 22 '06 #10

pks00
Expert 100+
P: 280
This code

Dim objTenant As pm_Tenant
Set objTenant = New pm_Tenant


is this referring to pm_Tenant as a class object?
And FullName is a variable inside your class?

If that is the case, then u cant access it using Properties as this is not a valid method for classes. U need to write public functions or create the public property functions like willakawill has shown/

or am I thinking this wrong.
Nov 22 '06 #11

100+
P: 1,646
This code

Dim objTenant As pm_Tenant
Set objTenant = New pm_Tenant


is this referring to pm_Tenant as a class object?
And FullName is a variable inside your class?

If that is the case, then u cant access it using Properties as this is not a valid method for classes. U need to write public functions or create the public property functions like willakawill has shown/

or am I thinking this wrong.
You are right. This is a confusion between user defined classes and inherent system objects
Nov 22 '06 #12

P: 6
Yes, pm_Tenant is a class module and .FullName is a property (a variable) of the class.

I think you are saying to create a function within the class module that returns the value, rather than using a property to return the value.

Thank you for your replies... I'll digest your example and see if it works for me.
Nov 22 '06 #13

100+
P: 1,646
Yes, pm_Tenant is a class module and .FullName is a property (a variable) of the class.

I think you are saying to create a function within the class module that returns the value, rather than using a property to return the value.

Thank you for your replies... I'll digest your example and see if it works for me.
Property and variable are not interchangeable. They are not the same thing
Nov 22 '06 #14

Post your reply

Sign in to post your reply or Sign up for a free account.