Controls to hold class objects

hbrems

has no clue...
Local time
Tomorrow, 00:26
Joined
Nov 2, 2006
Messages
181
Suppose I create a number of clsEmployee objects from a recordset. Great! Now I can loop through the employees.

Code:
Dim employee As clsEmployee
dim employees as Collection

... recordset loads employees in collection ...

For Each employee In employees
     listbox.AddItem employee.ID & ";" & employee.Name
Next

As you can see I've stored the employee property values in a listbox. However, are there any controls in Access that can hold actual objects iso just the values?
 
1. Set a new collection
2. Add the instance of clsEmployee to the collection (after adding to the properties of clsEmployee of course)

I would imagine there's some more information about this on the net
 
As you can see I've stored the employee property values in a listbox. However, are there any controls in Access that can hold actual objects iso just the values?
I assume you mean make a collection the source for the control(?) I'm not aware of any standard controls where you can do this.

I can understand why you would want to do this. However, I think technically it would be quite challenging (at least in Access). The reason I think this is because when using a recordset or query as a source, the source structure is very defined. However, if you were to use a collection of objects, how would the control know the accessor methods for the objects or which methods to use/not use? I suppose it's possible though. But Access is a database front end so it would then beg the question why use an object approach?

Chris
 
Why use an object approach?

Why not use an object oriented approach? :) A database is a structured way of storing data, OOP is a structured way of programming. VBA is limited on the OOP front but its classes still allow me to create a more natural feel for the data I'm working with.

Personally I don't like to work with tables or queries as rowsource for my forms or controls. Here's the approach I'm currently using (which may be totally crazy to most Access programmers :)):

1. A form requests from information from the database
2. A repository class will read the records from the table and create objects
3. These objects become available to the form

An example:

Code:
Public Sub btnPrintPayChecks_Clicked()

Dim employee As clsEmployee
Dim employees As Collection
Dim rep As clsEmployeeRepository

     set col = rep.ReadAllEmployees()
     
     for each employee in employees
          employee.PrintPayCheck
     next

End sub
 
I see what you're doing.

Disadvantage: In a multi-user environment, when changes are made to that record on table-level it will not reflect on what the class contains.
 
In a multi-user environment, when changes are made to that record on table-level it will not reflect on what the class contains.

That's a valid point. In cases where the most recent information is needed I'll need to refresh the data contained in the object.
 
I suppose naturally you would want the most recent records from a domain. Having to refresh a large set of records will be double work when you can have recordsets and bound forms that already do this for you.
 

Users who are viewing this thread

Back
Top Bottom