I have in a forms module, a sub that is looking to count the instances of use of a book name use within the main table for a particular year.
Main table fields (QSermons): "SDate) string "yyyy.mm.dd"; SBook string
Books table (tblBooks): "BookName" string
Years table (tblSermonYrs): "SermonYear" string
The datatype error occurs with the DCount statement, where I'm looking at records in the main table for instances of use of the current book name in records for the specified year.
Code:
Private Sub cmdImport_Click()
'*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
' Gather some stats
'*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
Dim rsBks As DAO.Recordset
Dim rsYrs As DAO.Recordset
Dim intcount As Integer
Open "c:\TMS\SermonStats.txt" For Output As #1
Print #1, "Below are the stats for the sermons based"
Print #1, "on Bible Books by year of sermon."
Print #1, ""
Set rsYrs = DBEngine(0)(0).OpenRecordset("tblSermonYrs")
With rsYrs
.MoveFirst
While Not .EOF
Print #1, "SERMONS FOR YEAR " & !SermonYear
Set rsBks = DBEngine(0)(0).OpenRecordset("tblbooks")
rsBks.MoveFirst
While Not rsBks.EOF 'Loop through the list of books to see if used in our current year of interest
intcount = DCount("SermonID", "QSermons", "[SBook] = """ & rsBks!BookName & """" And "Left([SDate],4) = """ & !SermonYear & """")
If intcount > 0 Then Print #1, right(" " & intcount, 3) & " - " & rsBks!BookName
rsBks.MoveNext
Wend
rsBks.Close
Set rsBks = Nothing
Print #1, ""
.MoveNext
Wend
End With
rsYrs.Close
Set rsYrs = Nothing
Print #1, "Total sermon files inventory: " & Me.RecordsetClone.RecordCount
Close #1
MsgBox "Done"
End Sub