Public Function basMode(MyField As String, _
MyTable As String, _
Optional MyRel As String = "", _
Optional MyCriteria As String = "", _
Optional Delim As String = "") As Variant
'Michael Red, 8/15/00
'To Return the Mode of a field within a recordset.
'Note: the optional parameter Delim is NOT implemented
'MyRel is one of the VB relational operators ("=", ">=", "<=", "<>", ">", "<")
'MyCriteria is the normal "Where Clause" (withouth the "Where")
'Sample Usage:
'? basMode("Hr1", "tblAttendance")
'P
'? basMode("Comp", "tblConCatLang")
'c5678
Dim sql As String
Dim rst As DAO.Recordset
sql = "Select " & MyField & ""
sql = sql & ", Count(" & MyField
sql = sql & ") As FieldCount "
sql = sql & "From " & MyTable
If (MyCriteria <> "") Then
sql = sql & " Where " & " " & MyField & " "
sql = sql & MyRel & " " & Chr(34) & MyCriteria & Chr(34)
End If
sql = sql & " Group By " & MyField
sql = sql & " Order By Count(" & MyField & ") Desc;"
Set rst = CurrentDb.OpenRecordset(sql, dbOpenDynaset)
If rst.EOF = True And rst.BOF = True Then
basMode = ""
Else
rst.MoveFirst
basMode = rst(MyField)
End If
End Function