Class Module returning ADODB recordset that can be assigned to forms or controls.

A

Arookie

Hi,

Is it possible to use OOP in Access VBA. I am trying to build an application
with Business Logic Layer using class modules for example GetProducts is a
public function in Products Class module that can return all products in the
products table as a ADODB recordset. Then I can assign this recordset to a
form or control.

Thanks.
Arookie
 
S

Stefan Hoffmann

hi,
Is it possible to use OOP in Access VBA. I am trying to build an application
with Business Logic Layer using class modules for example GetProducts is a
public function in Products Class module that can return all products in the
products table as a ADODB recordset. Then I can assign this recordset to a
form or control.
Yes, it is possible. As a simple function can return it:

Public Function GetProducts() As ADODB.Recordset
End function


mfG
--> stefan <--
 
D

Douglas J. Steele

Stefan Hoffmann said:
hi,

Yes, it is possible. As a simple function can return it:

Public Function GetProducts() As ADODB.Recordset
End function

And remember you need to use the Set keyword when calling that function:

Dim rs As ADODB.Recordset

Set rs = GetProducts()
 
A

Arookie

Thanks for replying. I am not sure what I was doing yesterday but it seems to
work today.

Could you please provide me further guidance. Should I clone the recordset
object used in RecordsetWrapper class as following in by business layer logic
class or should I use pointer to recordset.

The following code I believe is pointing to the recordeset object in
RecordsetWrapper class which I think is similar to By Ref.
Set GetCourseList = rsw.Recordset

The following code is getting the clone of the recordset object in
RecordsetWrapper class which is I think similar to By Value after closing the
source recordset.
If rsw.SetClone(GetCourseList) Then
End If

or is there any other way to get recordset similar to the datareader or
dataset implementation in .Net


RecordsetWrapper: Class Module
===================================================
Option Compare Database
Option Explicit

Private m_rs As ADODB.Recordset

Public Function OpenRecordset(Domain As String, _
Optional Criteria As String = "1=1", _
Optional OrderBy As String, _
Optional RecordsetCursorType As
ADODB.CursorTypeEnum = adOpenForwardOnly, _
Optional RecordsetLockType As
ADODB.LockTypeEnum = adLockReadOnly, _
Optional RecordsetOption As Long = adCmdText, _
) As Boolean


If Not m_rs Is Nothing Then
' Close the recordset so it can be re-used
CloseRecordset
End If

m_rs.Open strSQL, CnxnAIMS, RecordsetCursorType, RecordsetLockType,
RecordsetOption

OpenRecordset = True

Exit Function

ErrorHandler:
' verify the private Recordset object was not set
Debug.Assert m_rs Is Nothing
End Function

Public Property Get Recordset() As ADODB.Recordset
Set Recordset = m_rs
End Property

Public Function SetClone(ByRef rs As ADODB.Recordset) As Boolean
On Error GoTo ErrorHandler
Set rs = m_rs.Clone(adLockReadOnly)
m_rs.Close
SetClone = True
Done:
Set m_rs = Nothing
Exit Function
ErrorHandler:
If eh.LogError("RecordsetWrapper.SetClone") Then Resume
End Function

Private Sub Class_Terminate()
If Not m_rs Is Nothing Then
m_rs.Close
Set m_rs = Nothing
End If
End Sub

===================================================


clsProducts: Class Module
===================================================
Option Compare Database
Option Explicit

Private Const coTABLE As String = "PRODUCTS"

Public Function GetProducts() As ADODB.Recordset
On Error GoTo HandleError

If rsw.OpenRecordset(coTABLE) Then
Set GetCourseList = rsw.Recordset
OR
If rsw.SetClone(GetCourseList) Then
End If
End If
Done:
Exit Function
HandleError:
Err.Raise err.description
End Function
====================================================

Class Implementation in Forms

Dim prd As New clsProducts
Set Me.Recordset = prd.GetProducts()
 
S

Stefan Hoffmann

hi,
Could you please provide me further guidance. Should I clone the recordset
object used in RecordsetWrapper class as following in by business layer logic
class or should I use pointer to recordset.
In OOP you need a clone for changing data. btw, where is the business layer?
The following code I believe is pointing to the recordeset object in
or is there any other way to get recordset similar to the datareader or
dataset implementation in .Net
No, but why would you do that? Access is built to consume tables and
queries directly.
imho, what you have so far is overkill, because it gives you no benefits.


mfG
--> stefan <--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top