Teri Bridges
Member
- Local time
- Yesterday, 18:21
- Joined
- Feb 21, 2022
- Messages
- 187
I have written a function that counts my events using an SQL statement. I did this by watching various codes being written and putting it together.
I would like advice on whether I have written the code to best practice.
The code does work and gives the correct results.
I would like advice on whether I have written the code to best practice.
The code does work and gives the correct results.
Code:
'Used for Course Report to count the total events for the entire project
Function CntAllEvents() As Integer
Dim SQL As String
Dim dbs As Database, rst As Recordset
SQL = "SELECT Count(Events_tbl.EventID) AS [Event Count] " & _
"FROM (ListModule_tbl INNER JOIN ((Course_tbl INNER JOIN Lessons_tbl ON Course_tbl.CourseID = Lessons_tbl.CourseID) " & _
"INNER JOIN Topics_tbl ON Lessons_tbl.LessonID = Topics_tbl.LessonID) ON ListModule_tbl.ModuleID = Course_tbl.ModuleID) " & _
"INNER JOIN Events_tbl ON Topics_tbl.TopicID = Events_tbl.TopicID;"
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(SQL)
'Skips any modules with no events
If rst.RecordCount = 0 Then
CntAllEvents = 0
Else
rst.MoveLast