@@ -21,7 +21,6 @@ Attribute VB_Exposed = False
2121'---------------------------------------------------------------------------------------
2222'<codelib>
2323' <file>data/dao/DaoHandler.cls</file>
24- ' <description>DAO data connection methods</description>
2524' <license>_codelib/license.bas</license>
2625' <ref><name>DAO</name><major>5</major><minor>0</minor><guid>{00025E01-0000-0000-C000-000000000046}</guid></ref>
2726' <test>_test/data/dao/DaoHandlerTests.cls</test>
@@ -781,21 +780,21 @@ Public Function LookupSql(ByVal SqlText As String, _
781780 Optional ByVal Index As Variant = 0 &, _
782781 Optional ByVal ValueIfNull As Variant = Null ) As Variant
783782
784- Dim rst As DAO .Recordset
783+ Dim rst As DAO .Recordset
785784
786785On Error GoTo HandleErr
787786
788- Set rst = Me.OpenRecordset(SqlText, dbOpenForwardOnly, dbSeeChanges, dbReadOnly)
789- With rst
790- If .EOF Then
791- LookupSql = ValueIfNull
792- Else
793- LookupSql = Nz(.Fields(Index), ValueIfNull)
794- End If
795- .Close
796- End With
797- Set rst = Nothing
798-
787+ Set rst = Me.OpenRecordset(SqlText, dbOpenForwardOnly, dbSeeChanges, dbReadOnly)
788+ With rst
789+ If .EOF Then
790+ LookupSql = ValueIfNull
791+ Else
792+ LookupSql = Nz(.Fields(Index), ValueIfNull)
793+ End If
794+ .Close
795+ End With
796+ Set rst = Nothing
797+
799798ExitHere:
800799 Exit Function
801800
@@ -804,7 +803,7 @@ HandleErr:
804803 rst.Close
805804 Set rst = Nothing
806805 End If
807-
806+
808807 Err.Raise Err.Number, "LookupSQL:" & Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
809808
810809End Function
@@ -829,15 +828,30 @@ Public Function Lookup(ByVal Expr As String, ByVal Domain As String, _
829828 Optional ByVal Criteria As Variant , _
830829 Optional ByVal ValueIfNull As Variant = Null ) As Variant
831830
832- Dim SelectSql As String
831+ Dim SelectSql As String
833832
834- SelectSql = "SELECT " & Expr & " FROM (" & Domain & ")"
835- If Not (VarType(Criteria) = vbError) Then
836- If Len(Criteria) > 0 Then
837- SelectSql = SelectSql & " WHERE " & Criteria
838- End If
839- End If
840- Lookup = LookupSql(SelectSql, , ValueIfNull)
833+ SelectSql = BuildSelectSql(Expr, Domain, Criteria, False )
834+ Lookup = LookupSql(SelectSql, , ValueIfNull)
835+
836+ End Function
837+
838+ Private Function BuildSelectSql (ByVal Expr As String , ByVal Domain As String , Optional ByVal Criteria As Variant , _
839+ Optional ByVal Distinct As Boolean = False )
840+
841+ Dim SelectSql As String
842+
843+ SelectSql = "SELECT "
844+ If Distinct Then
845+ SelectSql = SelectSql & "Distinct "
846+ End If
847+ SelectSql = SelectSql & Expr & " FROM (" & Domain & ")"
848+ If Not (VarType(Criteria) = vbError) Then
849+ If Len(Criteria) > 0 Then
850+ SelectSql = SelectSql & " WHERE " & Criteria
851+ End If
852+ End If
853+
854+ BuildSelectSql = SelectSql
841855
842856End Function
843857
@@ -856,8 +870,18 @@ End Function
856870' Long
857871'
858872'---------------------------------------------------------------------------------------
859- Public Function Count (ByVal Expr As String , ByVal Domain As String , Optional ByVal Criteria As Variant ) As Long
860- Count = Nz(Me.Lookup("Count(" & Expr & ")" , Domain, Criteria), 0 )
873+ Public Function Count (ByVal Expr As String , ByVal Domain As String , Optional ByVal Criteria As Variant , _
874+ Optional ByVal Distinct As Boolean = False ) As Long
875+
876+ If Distinct Then
877+ If Expr <> "*" Then
878+ Domain = "(" & BuildSelectSql(Expr, Domain, Criteria, True ) & ")"
879+ Criteria = vbNullString
880+ End If
881+ End If
882+
883+ Count = Nz(Me.Lookup("Count(" & Expr & ")" , Domain, Criteria), 0 )
884+
861885End Function
862886
863887'---------------------------------------------------------------------------------------
@@ -960,7 +984,7 @@ End Function
960984' Works for Jet only from Jet 4.0 (Access 2000), IdentityTable is only applicable for MSSQL, under MySQL the IDENT_CURRENT function does not exist.
961985' (<data.adodb.AdodbHandler::InsertIdentityReturn> is more suitable regarding use for active DBMS.)
962986'---------------------------------------------------------------------------------------
963- Public Function InsertIdentityReturn (ByVal InsertSql As String ) As Variant
987+ Public Function InsertIdentityReturn (ByVal InsertSQL As String ) As Variant
964988
965989 Dim db As DAO .Database
966990 Dim rst As DAO .Recordset
@@ -969,7 +993,7 @@ Public Function InsertIdentityReturn(ByVal InsertSql As String) As Variant
969993On Error GoTo HandleErr
970994
971995 Set db = Me.CurrentDb
972- db.Execute InsertSql
996+ db.Execute InsertSQL
973997 RecordsAffected = db.RecordsAffected
974998
975999 If RecordsAffected = 0 Then
0 commit comments