I don't have specific numbers to give but I am sure that I heard that DCount is much more efficient than DLookup.if its a large recordset, then both dcount and a recordset open will take time
can you not just try and read a single example. ie dlookup - that would be quicker, surely
I would like to see timed comparisons. I was under the impression that Domain functions opened a recordset to perform the task.
for a newb post, they don't need to know this. it's simply irrelevant at this level of skill and desire.
Sub DCountVsRecordset()
Const TABLE = "yourtablehere"
Const MAX = 400
Dim clock As Single
Dim tmp As String
Dim i As Integer
clock = Timer
For i = 1 To MAX
tmp = DCount("*", TABLE)
Next
Debug.Print "DCount(): " & Round(Timer - clock, 4)
clock = Timer
For i = 1 To MAX
tmp = CurrentDb.OpenRecordset("SELECT Count(*) FROM " & TABLE).Fields(0)
Next
Debug.Print "Recordset: " & Round(Timer - clock, 4)
Beep
Debug.Print "***"
End Sub
I guess I should have made it clear that I was actually commenting on your post that claimed the DCount was "MUCH faster" than the recordset, rather than answering the original question.
I posted my comment because implicit in your post was the idea that "a single line of code" would automatically be faster, which is not necessarily the case. DCount is a function. Functions call other code and I strongly suspect the DCount function opens a recordset.
Inexperienced developers often confuse the simplicity and convenience of the domain functions with processing efficiency. Comments that suggest a single line of code would undoubtely be faster contribute to that misunderstanding. How often have we seen posters asking why their form takes too long to open only to discover they have used a plethora of domain functions to populate individual textboxes?
In any case I was not aware of any policy here that suggests the discussion in a thread be strictly limited to precisely the original question. Some of the best stuff I have learned here is from the extra comments.
Indeed I believe the comment was completely appropriate. I know I am not alone in thinking this is the way Domain functions work and if we are right then the claim of the superior speed of the DCount would be incorrect. Hence it is a reasonable and intelligent comment that timed results would be interesting. This is just the place where someone could pop up and say they have made such a comparative measurement.
Moreover I don't think it is up to anyone other than the poster to determine (or rather, make assumptions) about their "level of skill and desire". They can take or leave the comments as they wish.
Remember this site is not only used to answer the question but is an ongoing resource used by many beyond our own membership.
When someone makes a claim it is fair, reasonable and appropriate to comment that it might not be as cut and dried as they suggest. I would advise those who cannot bear others questioning their received wisdom should avoid posting here.
Another piece of importent informationOne comment to make on this subject. When building a recordset to retrieve data or obtain a record count it is far quicker to Select * from table instead of selecting specific fields even if there are a lot of fields in the table. Access has to filter out selected fields prior to building the recordset. Whereas selecting all records this operation is excluded and as such becomes more efficient.
just to let you know - I'm using Access for the last 15 years, since Access 2 though my programming level is not as good as many other members here, I'm larning fastfor a newb post, they don't need to know this. it's simply irrelevant at this level of skill and desire.
When building a recordset to retrieve data or obtain a record count it is far quicker to Select * from table instead of selecting specific fields even if there are a lot of fields in the table. Access has to filter out selected fields prior to building the recordset. Whereas selecting all records this operation is excluded and as such becomes more efficient.
For a large domain I will go for Recordset as long as you use Count(*) in the sql of the OpenRecordset command. If you were going to loop through the recordset and count each line then this would be slower than a DCount().what is better to count the number of records, using DCount or OpenRecordset ?
Again, I will go for Recordset as it doesn't get populated until you MoveLast. Which is why it is advised that to get a full record count you must MoveLast. Hence, if we don't move last, it buffers only the first couple of records (depending on the size of the domain). I think it buffers up to a max of 2000 (without MoveLast).and what is better when I only need to check if there are any records in the query? for this I now use If .RecordCount > 0
Function DCountVsRecordset()
Const iTABLE = "yourtablehere"
Const iMAX = 8000
Dim db As DAO.Database
Dim clock As Single
Dim tmp As String
Dim i As Integer
clock = Timer
For i = 1 To iMAX
tmp = DCount("*", iTABLE)
Next
Debug.Print "DCount(): " & Round(Timer - clock, 4)
clock = Timer
[COLOR=Red][B] Set db = CurrentDb[/B][/COLOR]
For i = 1 To iMAX
tmp = db.OpenRecordset("SELECT Count(*) As CountOf FROM " & iTABLE[COLOR=Red][B], dbOpenSnapshot[/B][/COLOR]).Fields(0)
Next
Debug.Print "Recordset: " & Round(Timer - clock, 4)
Beep
Debug.Print "***"
End Function
Function DCountVsRecordset()
Const iTABLE = "yourtablehere"
CONST iFIELD = "yourfieldhere"
Const iMAX = 8000
Dim db As DAO.Database
Dim clock As Single
Dim tmp As String
Dim i As Integer
clock = Timer
For i = 1 To iMAX
tmp = DLookup(iFIELD, iTABLE)
Next
Debug.Print "DLookup(): " & Round(Timer - clock, 4)
clock = Timer
Set db = CurrentDb
For i = 1 To iMAX
tmp = db.OpenRecordset("SELECT " & iFIELD & " FROM " & iTABLE, dbOpenSnapshot).Fields(0)
Next
Debug.Print "Recordset: " & Round(Timer - clock, 4)
Beep
Debug.Print "***"
End Function
That seems potentially counter intuitive. I love this site for exactly that kind of surprise.
I wonder if it makes a difference if the recordset source is a table with an index. The index could potentially know the record count better than the table.
Perhaps Microsoft could even use some undocumented interaction with the index for the domain functions.
Are you basing that on the test in post #16?I imagine something like DLookup(TblKey, Tbl) should be fast as it only need the first record it find.
though using DLookup doesn't seems to be intuitive