Class code, generated with the Class builder

The code below was generated from a table whose purpose it is to store system variables. The class below gives programmatic access hiding the details of where the variables are stored.

Generated code characteristics are:

  • Each table field is interpreted as a class property
  • Record set managed (created and terminated) within the class
  • Find record: using FindFirst automatically picks up the records values in class private variables
  • FindFirst without argument is interpreted as move and load the first record. This is useful for parameter tables with only one record.
  • Update saves the current value of the class' properties back into the record
  • Create new record: use AddNew. AddNew tells the class that Update should be interpreted as creating a new record

Note: if during calling Load error you get error 93 "invalid use of null", you can change the generated code either by using Nz() ...

        Me.UploadWeb = Nz(.Fields("UploadWeb").value)
... or by changing the property type to Variant.

Option Explicit

' Module: Singleton
' DateTime: 9/7/2007 10:34:20 AM
' Author: Uildriks
' Description:
'---------------------------------------------------------------------------------------
    
Private mdtUploadWeb As Date
Private miSupplyForm274 As Integer
Private mrstRecordset As Recordset
Private mbooLoaded As Boolean
Public Property Get UploadWeb() As Date
    UploadWeb = mdtUploadWeb
End Property

Public Property Let UploadWeb(rData As Date)
    mdtUploadWeb = rData
End Property

Public Property Get SupplyForm274() As Integer
    SupplyForm274 = miSupplyForm274
End Property

Public Property Let SupplyForm274(rData As Integer)
    miSupplyForm274 = rData
End Property

Private Property Get Recordset() As Recordset
    Set Recordset = mrstRecordset
End Property

Private Property Set Recordset(rData As Recordset)
    Set mrstRecordset = rData
End Property

Private Sub Load()
    With Recordset
        Me.UploadWeb = .Fields("UploadWeb").value
        Me.SupplyForm274 = .Fields("Voorraad Aanvraagformulieren").value
    End With
    mbooLoaded = True
End Sub


Public Sub Update()
    With Recordset
        If mbooLoaded = True Then
            .Edit
        Else
            .AddNew
        End If
        .Fields("UploadWeb").value = Me.UploadWeb
        .Fields("Voorraad Aanvraagformulieren").value = Me.SupplyForm274
        .Update
    End With
    mbooLoaded = True
End Sub


Public Sub AddNew()
    mbooLoaded = False
End Sub
Public Function FindFirst(Optional Criteria As VariantAs Boolean
    If IsMissing(Criteria) Then
        Recordset.MoveFirst
        FindFirst = Not Recordset.EOF
    Else
        Recordset.FindFirst Criteria
        FindFirst = Not Recordset.NoMatch
    End If
    If FindFirst Then Load
End Function
Private Sub Class_Initialize()
    Set Recordset = CurrentDb.OpenRecordset("tblSingleton")
End Sub
Private Sub Class_Terminate()
    Recordset.Close
    Set Recordset = Nothing
End Sub

Generate class from table