Fast Table Lookup Functions
Fast Table Lookup Functions
In t r odu ct ion
DLook u p syn t ax
If you are looking for the fastest version only, this section
can be skipped without har m ; it descr ibes in steps how
speed is achieved by reducing ver satility. The Úrst attem pt
is quite obvious.
1
Function MyDLookup(Expr As String, Domain As String, Optional Cri
strSQL _
= " SELECT " & Expr _
& " FROM " & Domain _
& " WHERE " + Criteria
End Function
¯¯¯¯¯
If we use an existing database object like DBEngine(0)(0),
then the function will triple in speed and becom e alm ost
50% faster than DLookup.
2
With DBEngine(0)(0).OpenRecordset(strSQL, dbOpenDynaset)
' or, if an open database object exists:
With MyDb.OpenRecordset(strSQL, dbOpenDynaset)
¯¯¯¯¯
By using the sam e technique as DLookup, we achieved an
unexpected sm all im provem ent. Let's try another
approach: opening a recordset and using the .FindFirst
m ethod. Let's also sim plify the problem by looking for a
Úeld and not an expr ession.
3
Function MyDLookup(Field As String, Domain As String, Criteria As
End Function
¯¯¯¯¯
We can't close this exploration without tr ying param etric
queries. This approach is logical for program m ers
switching from m ost other SQL engines: trying to em ulate
stor ed procedures. This m ethod is in fact recom m ended
for "pass-through" quer ies, not very useful for Jet
databases, but let's try.
With qdf
.Parameters(0) = Key
With .OpenRecordset
If .EOF _
Then CompanyNameQdf = Null _
Else CompanyNameQdf = .Fields(0)
End With
End With
End Function
With DBEngine(0)(0)(Table).OpenRecordset(dbOpenTable)
.Index = Index
5 de 9 .Seek "=", Key1, Key2, Key3, Key4, Key5 05/07/2016 01:09 p.m.
Access Techniques: Fast Table Lookup Functions https://www.experts-exchange.com/articles/1921/A...
) As Variant
With DBEngine(0)(0)(Table).OpenRecordset(dbOpenTable)
.Index = Index
.Seek "=", Key1, Key2, Key3, Key4, Key5
If Not .NoMatch Then DSeek = .Fields(Field)
End With
End Function
At this point it's good to read the help Úle on the Seek
m ethod.
End Function
End Function
It turns out that the over head is m uch higher, sim ilar to
the problem when using CurrentDb, so that this attem pt
is only m ar ginally faster than DLookup (about 20%) when
tested on a Jet database. Jum ping ahead to the next
section and function (6), it is possible to create dedicated
lookup functions with ADO, with good results, but the
perform ance will not m atch that of DAO. Only one
exam ple is included in the attached benchm arking dem o
Úle, roughly Úve tim es slower than the equivalent DAO
function.
With srec
.Seek "=", Key
If Not .NoMatch Then CompanyName = !CompanyName
7 de 9 End With 05/07/2016 01:09 p.m.
Access Techniques: Fast Table Lookup Functions https://www.experts-exchange.com/articles/1921/A...
With srec
.Seek "=", Key
If Not .NoMatch Then CompanyName = !CompanyName
End With
End Function
The Úrst tim e the function is called, the static recor dset is
set to a "table-type" r ecordset of the table Com panies (the
"open r ecordset" m ethod defaults to "open table" instead
of "open dynaset"). This object will rem ain open between
function calls -- that's the m eaning of "static". The second
tim e, the function jum ps over the initialization.
For every call, the passed "Key" is sought in the cur rent
index. It doesn't m atter if the key Úeld is num er ic or text,
but the "Key" variable should be of the sam e type as the
key Úeld. See one typical trap and it's solution as Warning
just below the code snippet 7 below.