RonPaii
Active member
- Local time
- Today, 06:18
- Joined
- Jul 16, 2025
- Messages
- 150
Simpler and faster then a bunch of dLookups.Unless you're going to be using each of those functions multiple times, seems like overkill.
Code:
Option Compare Database
Option Explicit
' Sample Class to search recordset and return values
Private Type MyTableType
qd As DAO.QueryDef
rs As DAO.Recordset
End Type
Private This As MyTableType
Private Sub Class_Initialize()
' Do nothing
End Sub
Private Sub Class_Terminate()
With This
If Not .rs Is Nothing Then
.rs.Close
Set .rs = Nothing
End If
If Not .qd Is Nothing Then
Set .qd = Nothing
End If
End With
End Sub
Public Property Let LookupID(ByVal PrimayKey As Long)
With This
If Not .rs Is Nothing Then
.rs.Close
Set .rs = Nothing
ElseIf .qd Is Nothing Then
Set .qd = CurrentDb.QueryDefs("MyTableQuery")
End If
.qd.Parameters("ID") = PrimayKey
Set .rs = .qd.OpenRecordset()
If .rs.RecordCount = 0 Then
.rs.Close
Set .rs = Nothing
Else
.rs.MoveFirst
End If
End With
End Property
Public Property Get Description() As String
If Not This.rs Is Nothing Then
Description = This.rs.Fields("Description")
End If
End Property
Public Property Get Location() As String
If Not This.rs Is Nothing Then
Location = This.rs.Fields("Location")
End If
End Property
Public Property Get SomeOtherField() As String
If Not This.rs Is Nothing Then
SomeOtherField = This.rs.Fields("SomeOtherField")
End If
End Property