[AccessD] Advanced Unbound Form With Classes and Collections Part 3

DWUTKA at marlow.com DWUTKA at marlow.com
Wed Jun 28 09:32:12 CDT 2006


Dedicated, but that should be obvious JC, how would you get a 'for each'
loop out of it?  Now, you could create a 'super' collection class, where the
class had properties which were other collections, and they could have their
own collection, ie:

For each Person in People

Next

For Each Teacher in People.Teachers

Next

For each Student in People.Students

Next

etc.

Drew

-----Original Message-----
From: JWColby [mailto:jwcolby at colbyconsulting.com]
Sent: Tuesday, June 27, 2006 7:09 PM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Advanced Unbound Form With Classes and
Collections Part 3


Drew,

>Public Function NewEnum() As Iunknown

Is this a dedicated syntax thing or can I do this multiple times for
multiple collections in the same class? 

Public Function NewEnum1() As IUnknown

Set NewEnum1=PeopleByFirst.[_NewEnum1]

End Function

Public Function NewEnum2() As IUnknown

Set NewEnum2=PeopleByLast.[_NewEnum2]

End Function

John W. Colby
Colby Consulting
www.ColbyConsulting.com

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of DWUTKA at marlow.com
Sent: Tuesday, June 27, 2006 2:58 PM
To: accessd at databaseadvisors.com
Subject: [AccessD] Advanced Unbound Form With Classes and Collections Part 3

We have built a 'collection class' in our demo, the People Class.  It is an
object representing all of the people in our database.  We can retrieve
people from it either with their ID, or by their sort order (first or last
name sort).

 

But why can't we do a For Next loop?  I mean, our class represents multiple
Persons, so it contains all of the Person classes, why are we forced to use
an index or position to retrieve a person?

 

Surprise, we're not.  Though to get this capability requires a little
'outside of the box' work.  Here's what we need to do.  First, we need to
add a function to our People class:

 

Public Function NewEnum() As IUnknown

Set NewEnum=PeopleByFirst.[_NewEnum]

End Function

 

The next portion is a little tricky in Access.  We need to set this
procedure's 'id' to -4.  In Visual Basic 6, you can do this by clicking
Tools>Procedure Attributes, and then clicking the Advanced button.  I
looked, couldn't find this in Access, but all's not lost.  We can still do
this.  Right click on the People class in our demo project's code window.
Select Export File.  Save it somewhere.  Now go and open that saved file in
Notepad.  You'll see the code of our class with some extra stuff thrown in.
What we need to do is add a line in our NewEnum function so it looks like
this:

 

Public Function NewEnum() As IUnknown

Attribute NewEnum.VB_UserMemId = -4

Set NewEnum=PeopleByFirst.[_NewEnum]

End Function

 

Once this is done, let's test it.

 

Create a form with a button, and put the following code behind it:

 

Dim ps as Person

Dim ppl as People

Set ppl=New People

For Each ps in ppl

            Debug.print ps.FullName

Next

Set ps=nothing

Set ppl=nothing

 

Now press the button and run our test code.  Whalla!  We can now loop
through the Person objects in our People Collection with a For Next loop!

 

So now that we know about collections, and we have created the same
functionality of a collection with our own 'collection class', let's go a
step further, and make our Person class 'aware' of it's collection class.
We'll do this in the next part.

 

Drew

--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com

-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com



More information about the AccessD mailing list