Replace DLookup by an equivalent select query.
-Tom.
>Hi Gurus
>
[quoted text clipped - 25 lines]
>
>- Nicolaas
WindAndWaves - 02 Sep 2004 11:02 GMT
Hoi Tom
Basically, Phil in the other answer is recommending the same thing, using
the Elookups. That seems brilliant to me.
Thank you for your reply.
Nicolaas
> Replace DLookup by an equivalent select query.
> -Tom.
[quoted text clipped - 28 lines]
> >
> >- Nicolaas
David W. Fenton - 02 Sep 2004 18:26 GMT
> Basically, Phil in the other answer is recommending the same
> thing, using the Elookups. That seems brilliant to me.
Trevor Best wrote his tLookup functions a long time ago (the code I
have is dated 1996). He recently updated them, and they can be found
here:
http://www.mvps.org/access/modules/mdl0012.htm

Signature
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Here are some useful functions from Allen Browne.
I always use ELookup(........) etc Much faster and does not load the
database so much
HTH
Phil
Function EMin(expr As String, domain As String, Optional Criteria)
On Error GoTo Err_EMin
'Purpose: Faster and more flexible replacement for Emin()
'Arguments: Same as Emin, with additional Order By option.
'Return: Value of the Expr if found, else Null or #Error.
'Author: Allen Browne. abrowne1@bigpond.net.au, Phil S
Dim MyDb As Database
Dim rs As Recordset
Dim SQLStg As String
'Build the SQL string.
SQLStg = "SELECT " & expr & " FROM " & domain
If Not IsMissing(Criteria) Then
SQLStg = SQLStg & " WHERE " & Criteria
End If
SQLStg = SQLStg & " ORDER BY " & expr
SQLStg = SQLStg & ";"
'Lookup the value.
Set MyDb = DBEngine(0)(0)
Set rs = MyDb.OpenRecordset(SQLStg, dbOpenForwardOnly)
If rs.RecordCount = 0 Then
EMin = Null
Else
EMin = rs(0)
End If
rs.Close
Exit_EMin:
Set rs = Nothing
Set MyDb = Nothing
Exit Function
Err_EMin:
' MsgBox Err.Description, vbExclamation, "EMin Error " & Err.number
If Err.Number < 0& Or Err.Number > 65535 Then 'Valid range for
CVErr()
EMin = CVErr(5) 'Out of range.
Else
EMin = CVErr(Err.Number)
End If
Resume Exit_EMin
End Function
Function EMax(expr As String, domain As String, Optional Criteria)
On Error GoTo Err_EMax
'Purpose: Faster and more flexible replacement for Emin()
'Arguments: Same as Emin, with additional Order By option.
'Return: Value of the Expr if found, else Null or #Error.
'Author: Allen Browne. abrowne1@bigpond.net.au, Phil S
Dim MyDb As Database
Dim rs As Recordset
Dim SQLStg As String
'Build the SQL string.
SQLStg = "SELECT " & expr & " FROM " & domain
If Not IsMissing(Criteria) Then
SQLStg = SQLStg & " WHERE " & Criteria
End If
SQLStg = SQLStg & " ORDER BY " & expr & " DESC"
SQLStg = SQLStg & ";"
'Lookup the value.
Set MyDb = DBEngine(0)(0)
Set rs = MyDb.OpenRecordset(SQLStg, dbOpenForwardOnly)
If rs.RecordCount = 0 Then
EMax = Null
Else
EMax = rs(0)
End If
rs.Close
Exit_EMax:
Set rs = Nothing
Set MyDb = Nothing
Exit Function
Err_EMax:
' MsgBox Err.Description, vbExclamation, "EMax Error " & Err.number
If Err.Number < 0& Or Err.Number > 65535 Then 'Valid range for
CVErr()
EMax = CVErr(5) 'Out of range.
Else
EMax = CVErr(Err.Number)
End If
Resume Exit_EMax
End Function
Public Function ELookup(expr As String, domain As String, Optional Criteria,
Optional OrderClause)
On Error GoTo Err_ELookup
'Purpose: Faster and more flexible replacement for DLookup()
'Arguments: Same as DLookup, with additional Order By option.
'Return: Value of the Expr if found, else Null or #Error.
'Author: Allen Browne. abrowne1@bigpond.net.au
'Examples:
'1. To find the last value, include DESC in the OrderClause, e.g.:
' ELookup("[Surname] & [FirstName]", "tblClient", , "ClientID DESC")
'2. To find the lowest non-null value of a field, use the Criteria,
'e.g.:
' ELookup("ClientID", "tblClient", "Surname Is Not Null" , "Surname")
'Note: Requires a reference to the DAO library.
Dim MyDb As Database
Dim rs As Recordset
Dim SQLStg As String
'Build the SQL string.
SQLStg = "SELECT TOP 1 " & expr & " FROM " & domain
If Not IsMissing(Criteria) Then
SQLStg = SQLStg & " WHERE " & Criteria
End If
If Not IsMissing(OrderClause) Then
SQLStg = SQLStg & " ORDER BY " & OrderClause
End If
SQLStg = SQLStg & ";"
'Lookup the value.
Set MyDb = DBEngine(0)(0)
Set rs = MyDb.OpenRecordset(SQLStg, dbOpenForwardOnly)
If rs.RecordCount = 0 Then
ELookup = Null
Else
ELookup = rs(0)
End If
rs.Close
Exit_ELookup:
Set rs = Nothing
Set MyDb = Nothing
Exit Function
Err_ELookup:
' MsgBox Err.Description, vbExclamation, "ELookup Error " & Err.number
If Err.Number < 0& Or Err.Number > 65535 Then 'Valid range for
CVErr()
ELookup = CVErr(5) 'Out of range.
Else
ELookup = CVErr(Err.Number)
End If
Resume Exit_ELookup
End Function
> Hi Gurus
>
[quoted text clipped - 25 lines]
>
> - Nicolaas
WindAndWaves - 02 Sep 2004 11:01 GMT
Brilliant
Thanks a million!
> Here are some useful functions from Allen Browne.
>
[quoted text clipped - 183 lines]
> >
> > - Nicolaas
WindAndWaves - 02 Sep 2004 11:31 GMT
is there also one for ecount or should I work that out myself?!
> Here are some useful functions from Allen Browne.
>
[quoted text clipped - 183 lines]
> >
> > - Nicolaas
WindAndWaves - 02 Sep 2004 11:42 GMT
Anyway, I came up with :
Public Function Ecount(expr As String, domain As String, Optional Criteria)
'replacement for dCount
Const ProEro = 3: 'On Error GoTo err
Dim Dbs As Database
Dim RST As Recordset
Dim SqlS As String
'--- Build the SQL string.
SqlS = "SELECT count(" & expr & ") as C FROM " & domain
If Not IsMissing(Criteria) And Not Criteria = "" Then
SqlS = SqlS & " WHERE " & Criteria
End If
SqlS = SqlS & ";"
'---'Lookup the value.
Set Dbs = DBEngine(0)(0)
Set RST = Dbs.OpenRecordset(SqlS, dbOpenForwardOnly)
Ecount = RST.Fields("C")
RST.close
xit:
Set RST = Nothing
Set Dbs = Nothing
Exit Function
ERR:
' MsgBox Err.Description, vbExclamation, "ELookup Error " & Err.number
Ecount = 0
Resume xit
End Function
> Here are some useful functions from Allen Browne.
>
[quoted text clipped - 183 lines]
> >
> > - Nicolaas
Tom Mitchell - 02 Sep 2004 19:44 GMT
It looks like you have things well in hand, but if not, try Trevor
Best's replacement functions. Located at
http://easyweb.easynet.co.uk/~trevor/AccFAQ/. Look under downloads,
then domain aggregate function replacements.
Tom