I am using a form to basically display monthly and fiscal year to date figures.
I use the following code in the submit button (combo boxes populare year, month, department and incident type):
Expand|Select|Wrap|Line Numbers
- Private Sub Command89_Click()
- On Error GoTo jumpout
- Me.txtCountAccidents = CountingIncidents(cmboMonth, cmboYear, Me.cmboDept, 1)
- Me.txtCountSafetyConcerns = CountingIncidents(cmboMonth, cmboYear, Me.cmboDept, 3)
- Me.txtRatio = Me.txtCountSafetyConcerns / Me.txtCountAccidents
- Me.txtFiscalAccidents = FiscalCountingIncidents(Me.cmboMonth, Me.cmboYear, Me.cmboDept, 1)
- Me.txtFiscalSafetyConcerns = FiscalCountingIncidents(Me.cmboMonth, Me.cmboYear, Me.cmboDept, 3)
- Me.txtFiscalRatio = Me.txtFiscalSafetyConcerns / Me.txtFiscalAccidents
- completedyo:
- Exit Sub
- jumpout:
- MsgBox Err.Description & " - " & Err.Number
- Resume completedyo
- End Sub
First function to count current month stats
Expand|Select|Wrap|Line Numbers
- 'Function used to count the number of incidents per month based on month/year/department and incident type
- Public Function CountingIncidents(cmboMonth As String, cmboYear As Integer, cmboDept As Integer, IncidentTyping As Integer)
- On Error GoTo jumpout
- Dim db As DAO.Database
- Dim rs As DAO.Recordset
- Dim strsql As String
- Dim counter As Integer
- Set db = CurrentDb
- strsql = "SELECT Count(tblhselog.HSEID) AS CountOfHSEID "
- strsql = strsql & "FROM tblhselog "
- strsql = strsql & "WHERE (((Format([Incident_date],""mmm""))='" & cmboMonth & "') AND ((Format([Incident_date],""yyyy""))=" & cmboYear & ") AND ((tblhselog.Department)=" & cmboDept & ") AND ((tblhselog.Incident_type)=" & IncidentTyping & "));"
- 'Debug.Print strsql
- Set rs = db.OpenRecordset(strsql, dbOpenSnapshot)
- If rs.RecordCount <> 0 Then
- counter = rs("CountOfHSEID")
- Else
- counter = 0
- End If
- 'Debug.Print counter
- CountingIncidents = counter
- completedyo:
- rs.Close
- db.Close
- Set rs = Nothing
- Set db = Nothing
- Exit Function
- jumpout:
- MsgBox Err.Description & " - " & Err.Number
- Resume completedyo
- End Function
Second function to count fiscal year to date figures:
Expand|Select|Wrap|Line Numbers
- 'Function to return the total number of a type of incident in current fiscal year selected
- Public Function FiscalCountingIncidents(countMonth As String, countYear As Integer, countDept As Integer, countIncident As Integer)
- On Error GoTo jumpout
- Dim dbz As DAO.Database
- Dim rsz As DAO.Recordset
- Dim strsqlz As String
- Dim counterz As Integer
- Set dbz = CurrentDb
- strsqlz = "SELECT Count(tblhselog.HSEID) AS CountOfHSEID"
- strsqlz = strsqlz & " FROM tblhselog"
- strsqlz = strsqlz & " WHERE (((tblhselog.Incident_date) Between fiscalStartDate('" & countMonth & "'," & countYear & ") And fiscalEndDate('" & countMonth & "'," & countYear & ")) AND ((tblhselog.Department)=" & countDept & "))"
- strsqlz = strsqlz & " GROUP BY tblhselog.Incident_type"
- strsqlz = strsqlz & " HAVING (((tblhselog.Incident_type)=" & countIncident & "));"
- 'Debug.Print strsqlz
- Set rsz = dbz.OpenRecordset(strsqlz, dbOpenSnapshot)
- If rsz.RecordCount <> 0 Then
- counterz = rsz("CountOfHSEID")
- Else
- counterz = 0
- End If
- 'Debug.Print counterz
- FiscalCountingIncidents = counterz
- completedyo:
- rsz.Close
- dbz.Close
- Set rsz = Nothing
- Set dbz = Nothing
- Exit Function
- jumpout:
- MsgBox Err.Description & " - " & Err.Number
- Resume completedyo
- End Function
Here is an example of one of my functions to calculate the fiscal start date which as I stated sits in my query to use a BETWEEN statement, my end date is similar so no need to spam the thread with that also:
Expand|Select|Wrap|Line Numbers
- 'The following function is used to calculate the fiscal year end date given the month and year
- Function fiscalEndDate(monthvalue As String, yearvalue As Integer) As String
- Select Case monthvalue
- Case "Jan"
- monthvalue = 1
- Case "Feb"
- monthvalue = 2
- Case "Mar"
- monthvalue = 3
- Case "Apr"
- monthvalue = 4
- Case "May"
- monthvalue = 5
- Case "Jun"
- monthvalue = 6
- Case "Jul"
- monthvalue = 7
- Case "Aug"
- monthvalue = 8
- Case "Sep"
- monthvalue = 9
- Case "Oct"
- monthvalue = 10
- Case "Nov"
- monthvalue = 11
- Case "Dec"
- monthvalue = 12
- Case Else
- MsgBox "month value must be supplied"
- Exit Function
- End Select
- If (monthvalue >= 10) Then
- fiscalEndDate = "30/09/" & yearvalue + 1
- Else
- fiscalEndDate = "30/09/" & yearvalue
- End If
- End Function
Any clues what this is? I have googled and not got anything too descriptive back.