AccEPT - Access Error Prevention Table

D15 Aggregatfunktionen

Anweisung

Statt den Aggregatfunktionen für Domänen Recordset-Funktionen verwenden.

Beschreibung

Aggregatfunktionen sind sehr praktisch in ihrer Anwendung, da man schnell zum gewünschten Ergebnis kommt.
 
Sie sind aber meist langsamer im Vergleich zur Datenermittlung über SQL und Recordset-Objekten.
 
Lösung: Ersatzfunktionen für Dcount, Dmax, …
 
Beispiel RstLookup statt Dlookup:

Public Function RstLookup( _
           ByVal sFieldName As String, _
           ByVal sSource As String, _
  Optional ByVal sCriteria As String = "" _
) As Variant
 
   Dim db As DAO.Database
   Dim rst As DAO.Recordset
   Dim strSQL As String
 
On Error GoTo HandleErr
 
   strSQL = "SELECT " & sFieldName & _
            " FROM (" & sSource & ")"
   If len(sCriteria) > 0 Then
       strSQL = strSQL & " WHERE " & sCriteria
   End If
 
   Set db = CurrentDB
   ' oder Methode aus D13
   ' Set db = CurrentDAODB
   Set rst = db.OpenRecordset(strSQL)
   With rst
       If .EOF Then
           RstLookup = Null
       Else
           'Rückgabe des 1. Feldes
           RstLookup = .Fields(0)
       End If
   End With
 
ExitHere:
On Error Resume Next
   rst.Close
   Set rst = Nothing
   set db = Nothing
   Exit Function
 
HandleErr:
   Select Case Err.Number
   Case Else
       ' Fehlerbehandlung
       ' HandleError Err.Number, "RstLookup"
   End Select
   RstLookup = Null
   Resume ExitHere
 
End Function

 
RstMax:
Code ist ähnlich RstLookup mit folgender SQL-Syntax:

strSQL = _
   "SELECT Max(" & sFieldName & ") As DSMax" & _
   " FROM  (" & sSource & ")"
If sCriteria > vbNullString Then
   strSQL = strSQL & " WHERE " & sCriteria
End If

 
Weitere Beispiele sind in der AccEPT-DB im Modul modDAO enthalten. (DB steht im Download-Bereich zur Verfügung.)