Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Store records in class

Status
Not open for further replies.

KiaruB

Technical User
Apr 5, 2005
35
DE
Hello,

I am working with classes in my dbase to handle the data.

Storing one record into a class is no problem but i cant figure out how to store multiple records into a class (as an array/collection) :

lets say i have a table Employees :

ID Name Function Salary
1 Dirk Manager 3500
2 Eric Executive 2800
3 Marc Officer 2300

if i do "SELECT * FROM tblEMPLOYEES WHERE ID = 1"
i get one result to store in my class wich is no problem

if i do "SELECT * FROM tblEMPLOYEES" i get at least 3 results wich i want to store in my class
(to populate a combo or list or ...)

can anyone point me into the right direction or does anyone have any sample code ?

many thanks

Dirk.
 
Why not using a Recordset ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

Let's say i want to move to the next level in VBA

but mainly your code looks more elegant when using classes.

Anyway, i found a solution.

For anyone who's interested,
Put this in a class called CEmployee :

Option Compare Database
Option Explicit

Private pName As String
Private pAddress As String
Private pID As Long

Private Employees As Collection

Public Sub PrintPaycheck()

Debug.Print pName, pAddress

End Sub

Sub LoadEmployees()

Dim Employee As CEmployee
Dim rs As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT * FROM tblEmployee"

Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)

rs.MoveFirst

Set Employees = New Collection

Do Until rs.EOF

Set Employee = New CEmployee

Employee.ID = rs!Employee_ID
Employee.Name = rs!Employee_Name
Employee.Address = rs!Employee_Address
Employees.Add Employee

rs.MoveNext

Loop

rs.Close
Set rs = Nothing

End Sub

Sub PrintChecks()

Dim Employee As CEmployee

For Each Employee In Employees
Employee.PrintPaycheck
Next Employee

End Sub

Property Let ID(Value As String)
pID = Value
End Property
Property Get ID() As String
ID = pID
End Property
Property Let Name(Value As String)
pName = Value
End Property
Property Get Name() As String
Name = pName
End Property
Property Let Address(Value As String)
pAddress = Value
End Property
Property Get Address() As String
Address = pAddress
End Property


Using this class from a module or form :

dim TestThisClass as CEmployee
set TestThisClass = new CEmployee

TestThisClass.LoadEmployees
TestThisClass.PrintChecks

Greets,
Kiaru.
 
>your code looks more elegant when using classes

A recordset IS a class ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top