Solved

dynamically accessing properties of a object

Posted on 2004-10-28
141 Views
Last Modified: 2010-05-02
Hi,

I want the following:

function here(a as object,id as string) as object
  here = a.id
end function

id does not have to be a string, but it needs to be generic.  That is I don't want to have to check types for id.
0
Question by:rmundkowsky
    2 Comments
     
    LVL 32

    Accepted Solution

    by:
    You can use the CallByName function...

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctcallbynamefunction.asp

    e.g.

    property = CallByName(a, "id", vbGet)
    0
     

    Author Comment

    by:rmundkowsky
    Right now, thanks!

    Public Function find(thingToSearch As Collection, keyToFind As Variant, attributeToMatch As String) As Collection
        Dim temp As Object
        Dim tempCollection As Collection
        Dim varB

        Set tempCollection = New Collection
       
        For i = 1 To thingToSearch.Count
            Set temp = thingToSearch.Item(i)
           
           


            varB = CallByName(temp, attributeToMatch, VbGet)
            'if typeof temp is VBA.
            'changing attributeToMatch from a string is hard, but I think it can be done
            '  also we need to have multiple if then's for
            If StrComp(TypeName(varB), "String") = 0 Then
                If StrComp(varB, keyToFind) = 0 Then
                        Set find = tempCollection
                        tempCollection.Add temp
                End If
            Else
                If keyToFind = varB Then
                    Set find = tempCollection
                    tempCollection.Add temp
                End If
            End If
        Next

        If tempCollection.Count = 0 Then
            Set find = Nothing
        End If

    End Function


    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now