Mile-O
Back once again...
- Local time
- Today, 18:41
- Joined
- Dec 10, 2002
- Messages
- 11,316
Can somebody test my new MCount() function?
I got annoyed at the time DCount() was taking on a few of my queries so decided to try creating my own version.
It's DAO so, a reference must be set if there isn't one as default.
My initial tests have it being about 1 second faster than DCount - that's without criteria which I haven't included yet.
It won't work with queries that ask the user for a parameter but it can refer to a form.
I got annoyed at the time DCount() was taking on a few of my queries so decided to try creating my own version.
It's DAO so, a reference must be set if there isn't one as default.
My initial tests have it being about 1 second faster than DCount - that's without criteria which I haven't included yet.
It won't work with queries that ask the user for a parameter but it can refer to a form.
Code:
Public Function MCount(strField As String, strTable As String, Optional strCriteria As String) As Long
On Error GoTo Err_MCount
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim rsTemp As DAO.Recordset
Dim strParams() As String
Dim intCounter As Integer
Dim strForm As String
Dim strControl As String
Dim intPosition As Integer
Set qdf = DBEngine(0)(0).QueryDefs(strTable)
If qdf.Parameters.Count <> 0 Then
For intCounter = 0 To qdf.Parameters.Count - 1
ReDim Preserve strParams(intCounter)
strParams(intCounter) = Mid(qdf.Parameters(intCounter).Name, 9)
Next intCounter
For intCounter = LBound(strParams()) To UBound(strParams())
intPosition = InStr(strParams(intCounter), "!")
strForm = Left(strParams(intCounter), intPosition - 1)
strControl = Mid(strParams(intCounter), intPosition + 1)
qdf.Parameters(intCounter) = Forms(strForm)(strControl)
Next intCounter
End If
Set rs = qdf.OpenRecordset
If rs.EOF Then
MCount = 0
Else
Set rsTemp = rs
rsTemp.Filter = strCriteria
Set rsTemp = rs.OpenRecordset
rsTemp.MoveLast
MCount = rsTemp.RecordCount
End If
Exit_MCount:
Set qdf = Nothing
Set rs = Nothing
Set rsTemp = Nothing
Exit Function
Err_MCount:
MsgBox Err.Description, vbExclamation, "Error #" & Err.Number
Resume Exit_MCount
End Function
Last edited: