How to get Object property using EVAL()?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Genki
    New Member
    • Nov 2006
    • 6

    How to get Object property using EVAL()?

    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:
    Code:
    objPerson.FirstName = "John Smith"
    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.Firs tName"

    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.Firs tName".

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

    Code:
    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:

    Code:
    In "MyTable":
    
    Field:KEY                     Field:REPLACE_WITH
    -------------------------     -------------------------------------
    Tenant.FirstName              objTenant.FirstName
    Owner.FirstName               objOwner.FirstName
    Balance                       objInvoice.BalanceAmount
    (etc...)
    
    strMsg = "Hello [Tenant.FirstName]. Your invoice balance is [Invoice.Balance]"
    strMsg = MySpecializedReplacementFunction(strMsg)
    strMsg: "Hello John. Your invoice balance is $10"
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Hi. What I would do is use the value in the table to activate a section of code in a Select Case statement;
    Code:
    Select Case rs.Fields("Field1")
       Case "objPerson.FirstName"
          strText = objPerson.FirstName
    
       Case "something else"
          'peform whatever it says here
    End Select
    Hope this helps

    Comment

    • Genki
      New Member
      • Nov 2006
      • 6

      #3
      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.

      Comment

      • nico5038
        Recognized Expert Specialist
        • Nov 2006
        • 3080

        #4
        For just the property this should work:

        dim arr

        arr = split(rs!Replac e_with,".")

        select case arr(0)
        Case "objTenant"
        strX = objTenant.prope rties(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)

        Comment

        • Genki
          New Member
          • Nov 2006
          • 6

          #5
          Hi nico5038,

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

          Comment

          • Genki
            New Member
            • Nov 2006
            • 6

            #6
            Nico,

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

            Code:
            Dim objTenant As pm_Tenant
            Set objTenant = New pm_Tenant
            objTenant.Load (TenantID)
            
            Debug.Print "name1=" & objTenant.FullName
            ' ^ up to here it works. It correctly displays: Name1=Joe Smith
            
            ' The following lines return an error:  "Compile error: Method or data member not found"
            Debug.Print "name2=" & objTenant.Properties("FullName")   
            Debug.Print "name3=" & objTenant.Properties!FullName
            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))

            Comment

            • nico5038
              Recognized Expert Specialist
              • Nov 2006
              • 3080

              #7
              Hmm, strange. Are you sure the properties have been defined correctly ?

              To test use:

              objTenant.prope rties.count

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

              objTenant.prope rties(0).Name

              You can see the property names there are.

              Nic;o)

              Comment

              • Genki
                New Member
                • Nov 2006
                • 6

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

                objTenant.prope rties.count
                objTenant.prope rties(0).FirstN ame

                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.

                Code:
                Public Property Get FirstName() As String
                    FirstName = mstrFirstName
                End Property
                
                Public Property Let FirstName(NewValue As String)
                    mstrFirstName = NewValue
                End Property

                Comment

                • nico5038
                  Recognized Expert Specialist
                  • Nov 2006
                  • 3080

                  #9
                  I'm startled, but have asked another expert to join.

                  Nic;o)

                  Comment

                  • willakawill
                    Top Contributor
                    • Oct 2006
                    • 1646

                    #10
                    Originally posted by Genki

                    Code:
                    Public Property Get FirstName() As String
                        FirstName = mstrFirstName
                    End Property
                    
                    Public Property Let FirstName(NewValue As String)
                        mstrFirstName = NewValue
                    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 :)
                    Code:
                    'MyDog.cls class module
                    'local variable(s) to hold property value(s)
                    Private mvarDogName As String 'local copy
                    Private mvarTailWags As Integer 'local copy
                    Public Sub FeedDog(Optional NumCookies As Integer = 1)
                        mvarTailWags = mvarTailWags + Abs(NumCookies * 5)
                    End Sub
                    
                    Public Property Let TailWags(ByVal vData As Integer)
                    'used when assigning a value to the property, on the left side of an assignment.
                    'Syntax: X.TailWags = 5
                        mvarTailWags = vData
                    End Property
                    
                    
                    Public Property Get TailWags() As Integer
                    'used when retrieving value of a property, on the right side of an assignment.
                    'Syntax: Debug.Print X.TailWags
                        TailWags = mvarTailWags
                    End Property
                    
                    
                    
                    Public Property Let DogName(ByVal vData As String)
                    'used when assigning a value to the property, on the left side of an assignment.
                    'Syntax: X.DogName = 5
                        mvarDogName = vData
                    End Property
                    
                    
                    Public Property Get DogName() As String
                    'used when retrieving value of a property, on the right side of an assignment.
                    'Syntax: Debug.Print X.DogName
                        DogName = mvarDogName
                    End Property
                    
                    Public Property Get NewValue(stValue As String) As String
                        Select Case stValue
                            Case "DogName"
                                NewValue = mvarDogName
                            Case "TailWags"
                                NewValue = CStr(mvarTailWags)
                        End Select
                    End Property
                    then in a form module:
                    Code:
                    Private Sub cmdFeedTheDog_Click()
                        Dim MyNewDog As MyDog
                        Set MyNewDog = New MyDog
                        MyNewDog.DogName = "Fido"
                        MyNewDog.FeedDog 5
                        MsgBox CInt(MyNewDog.NewValue("TailWags"))
                    End Sub

                    Comment

                    • pks00
                      Recognized Expert Contributor
                      • Oct 2006
                      • 280

                      #11
                      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.

                      Comment

                      • willakawill
                        Top Contributor
                        • Oct 2006
                        • 1646

                        #12
                        Originally posted by pks00
                        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

                        Comment

                        • Genki
                          New Member
                          • Nov 2006
                          • 6

                          #13
                          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.

                          Comment

                          • willakawill
                            Top Contributor
                            • Oct 2006
                            • 1646

                            #14
                            Originally posted by Genki
                            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

                            Comment

                            Working...