I have a suggestion, you can place all Your DCount function in the Form_Load Event.
Remove from the text box ControlSource and try in Form_Load Event.
just try
Main Topics
Browse All TopicsHi
I'm in tearing hurry (thus the points). I have a number of textboxes that count where certain conditions are true. Thery are all located in a form which is bound to a query called Diabetes_Master which contains all the fields I need to query. As a novice I jumped to the first thing that worked which is DCount but it takes far too long. Can someone tell me what I can put in the Control Source of the fields to speed up performance. An example of one field is included below:
=DCount("*","Diabetes_Mast
cheers
Verdy
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Not sure if this will help. You can place this in a public module and then call it like DCount. It may not speed things up but then again I've always found it to be quite fast. You can tailor it to filter the the parameters but generally just follow the DCount rules. Hope it helps.
Use:
MyDCount("*", "Diabetes_Master", "Total_C<4.0")
'*************************
Public Function MyDCount(MyField As String, MyTable As String, MyCriteria As String) as long
Dim MyDataBase As Database, MyTable As Recordset
Dim strSQL As String
strSQL = "Select " & MyField & " from " & MyTable & " WHERE ” & MyCriteria
Set MyDataBase = CurrentDb()
Set MyTable = MyDataBase.OpenRecordset("
MyTable.MoveLast
MyDCount = MyTable.RecordCount
MyTable.Close
Set MyTable = Nothing
MyDataBase.Close
set mydatabase = nothing
End Function
You can check out:
http://www.mvps.org/access
Do some or all of the text box counts come from the same table? You may be able to combine a few of the above answers to speed things up even more.
for example if you have 5 text boxes to fill with counts from different fields in the same table. Leave the text boxes unbound, open a recordset with the 5 fields and every record. Loop thru the recordset, filling the 5 variables with the count, put the counts in the text boxes at the end.
You would only open one recordset, and only loop thru it once.
eddiej
Oops! I found a small error in my function. I created a duplicate definition with 'MyTable as String'. Should Be:
Public Function MyDCount(MyField As String, MySearchTable As String, MyCriteria As String) as long
Dim MyDataBase As Database, MyTable As Recordset
Dim strSQL As String
strSQL = "Select " & MyField & " from " & MySearchTable & " WHERE ” & MyCriteria
Set MyDataBase = CurrentDb()
Set MyTable = MyDataBase.OpenRecordset("
MyTable.MoveLast
MyDCount = MyTable.RecordCount
MyTable.Close
Set MyTable = Nothing
MyDataBase.Close
set mydatabase = nothing
End Function
I also found something very weird while testing the function. You can't use an upper case 'WHERE' when concactenating the strSQL statement. Also, the function is very fast:
Change this:0
strSQL = "Select " & MyField & " from " & MySearchTable & " WHERE ” & MyCriteria
To this:
strSQL = "Select " & MyField & " from " & MySearchTable & " where ” & MyCriteria
It's weird but Access keeps appending a (") at the of the first statement where 'Where' is uppercase. Very strange.
**************************
Get a count of Customer that start with the letter C:
MyDCount("*", "tblCustomers", "fldConsignee Like '*C*'")
**************************
Get a count of Customer named Joe Blo:
MyDCount("*", "tblCustomers", "fldConsignee='Joe Blo'")
**************************
Here's another improvement. Make the MyCriteria Optional so it behaves more like DCount. Leaving out the criteria will count all records in the recordset.
Get a count of all records in Diabetes_Master:
=MyDCount("*","Diabetes_Ma
**************************
Public Function MyDCount(MyField As String, MySearchTable As String, Optional MyCriteria As String) As Long
Dim MyDataBase As Database, MyTable As Recordset
Dim strSQL As String
If IsNull(MyCriteria) Then
strSQL = "Select " & MyField & " from " & MySearchTable & " where " & MyCriteria
Else
strSQL = "Select " & MyField & " from " & MySearchTable
End If
Set MyDataBase = CurrentDb()
Set MyTable = MyDataBase.OpenRecordset(s
MyTable.MoveLast
MyDCount = MyTable.RecordCount
MyTable.Close
Set MyTable = Nothing
MyDataBase.Close
Set MyDataBase = Nothing
End Function
Just less processing to keep it as fast as possible. Your Set rstLookup = .... statements will slow things down. Instead of using a Count field and then reading the results, I simply read a property of the recordset(RecordCount). It's much faster because there is no field processing(Count). I just open the recordset and grab the recordcount. DCount is still probably faster but it's always worth a try. It also depends on whether the criteria field is indexed or not. That is probably the only way to speed things up significantly.
Business Accounts
Answer for Membership
by: mgrattanPosted on 2005-12-01 at 16:27:50ID: 15401566
Here's a custom function that takes the same parameters and is much faster since it uses recordsets.
elect Count(*) From " & pstrTable, dbOpenSnapshot) elect Count(" & pstrField & ") From " & pstrTable, dbOpenSnapshot) elect Count(*) From " & pstrTable & " Where " & pstrCriteria, dbOpenSnapshot) elect Count(" & pstrField & ") From " & pstrTable & " Where " & pstrCriteria, dbOpenSnapshot)
Function tCount(pstrField As String, pstrTable As String, pstrCriteria As String) As Long
On Error GoTo tCount_Err
' Function tCount
' Purpose: Replace DCount, which is slow on attached tables
Dim dbCurrent As DAO.Database
Dim rstLookup As DAO.Recordset
Dim lngCount As Long
Set dbCurrent = DBEngine(0)(0)
If pstrCriteria = "" Then
If pstrField = "*" Or pstrField = "" Then
Set rstLookup = dbCurrent.OpenRecordset("S
Else
Set rstLookup = dbCurrent.OpenRecordset("S
End If
Else
If pstrField = "*" Or pstrField = "" Then
Set rstLookup = dbCurrent.OpenRecordset("S
Else
Set rstLookup = dbCurrent.OpenRecordset("S
End If
End If
If Not rstLookup.BOF Then
rstLookup.MoveFirst
lngCount = rstLookup(0)
Else
lngCount = 0
End If
rstLookup.Close
tCount = lngCount
tCount_Exit:
On Error Resume Next
rstLookup.Close
Exit Function
tCount_Err:
Select Case Err
Case Else
End Select
' Retry/Abort/Ignore
Select Case MsgBox(Error, MB_ABORTRETRYIGNORE Or MB_ICONEXCLAMATION, "Error " & Err)
Case IDABORT
Resume tCount_Exit
Case IDRETRY
Resume
Case IDIGNORE
Resume Next
End Select
Exit Function
End Function