I have some code that writes to a storage table information about the prevailing language of service for clients in a social services agency. It looks at each client within a prescribed date range (not in the code yet since I have hard-coded the dates in a setup query for testing purposes). For each client, it counts the number of meetings in any given language. If there is a clear majority, it takes the language of service and writes it to the storage table. If there isn’t it cycles through the choices available and chooses the most appropriate one based on some pre-determined criteria.
I have the code working fine, but it is SLOW. It currently takes about 7 minutes to work through the 432 records in my test data. In the field it will have to work through between 1500 and 2000 records in a reporting period. It seems to be particularly slow in the line bolded where there is a new recordset created. I tried fooling around with querydefs and using copyquerydef in a function as outlined in DAO help, but it doesn’t save an awful lot of time. This is the version of code before the querydef attempt. Various bist of code like Dims and error/exit handling have been omitted.
Set dbs = CurrentDb()
strSQLClnt = “SELECT DISTINCTROW qryLangPrevailFinal.ISAPClient, Max(qryLangPrevailFinal.Cnt) ” & _
“AS MaxOfCnt ” & _
“FROM qryLangPrevailFinal ” & _
“GROUP BY qryLangPrevailFinal.ISAPClient;”
Set rstClient = dbs.OpenRecordset(strSQLClnt, dbOpenDynaset)
rstClient.MoveLast
rstClient.MoveFirst
‘start the loop
Do While Not rstClient.EOF
With rstClient
strID = rstClient.Fields(“ISAPClient”).Value
‘get the higest count from the current client
rc = rstClient.Fields(“maxOfCnt”).Value
‘select all the records that have that count
strSQL = “SELECT qryLangPrevailSetup.ISAPClient, qryLangPrevailSetup.Lang, ” & _
“Count(qryLangPrevailSetup.ISAPMeetingID) AS Cnt ” & _
“FROM qryLangPrevailSetup ” & _
“GROUP BY qryLangPrevailSetup.ISAPClient, qryLangPrevailSetup.Lang ” & _
Set rstIn = dbs.OpenRecordset(strSQL, dbOpenDynaset)
Set rstOut = dbs.OpenRecordset(“tblPrevail”, dbOpenDynaset)
If rstIn.BOF = False Then
rstIn.MoveLast
rstIn.MoveFirst
intCount = rstIn.RecordCount
End If
‘see how many there are
If intCount = 1 Then
strLang = rstIn.Fields(“Lang”).Value
*************
Large chunk of code that makes choices when the count is > 1 removed.
*************
End If
With rstOut
.AddNew
![ClientID] = strID
![PrevailLang] = strLang
.Update
End With
.MoveNext
End With
Loop