473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to get Object property using EVAL()?

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
13 9254
willakawill
1,646 1GB
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
Genki
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
3,080 Expert 2GB
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
Genki
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
Genki
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
3,080 Expert 2GB
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
Genki
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
3,080 Expert 2GB
I'm startled, but have asked another expert to join.

Nic;o)
Nov 22 '06 #9
willakawill
1,646 1GB

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
280 Expert 100+
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
willakawill
1,646 1GB
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
Genki
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
willakawill
1,646 1GB
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

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

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
2
by: Simon | last post by:
Am using the following code. <script language="JavaScript1.2"> function setquantity(productindex,productquantity) { //create the reference object irefname_none = eval("document." +...
5
by: fred | last post by:
I don't know if I'm doing this correctly. I have a little programming experience in python, c++ and some others but this is my first time with javascript. I'm trying have my website detect the...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
16
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
4
by: jm.suresh | last post by:
Hi, Is there any standard text format for storing data of object oriented nature. The text file should be readable. That is, Is there any better way than having to write out a file like this...
2
by: Daz | last post by:
Hi everyone. Sorry for the confusing subject, I couldn't think how best to word it. What I would like to know, is if there is an equivilant to this code, in using JSON. <script...
8
by: =?Utf-8?B?QXNo?= | last post by:
Hi, I have an object, for example User. User contains various properties which i have been able to bind to successfully using wizards and the form view. However if the class User has a property...
7
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I access a property of an object using a string?...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.