Hi, here is an example of my code. I couldn't put all of it in here as it is huge.
Private Sub cmdSearch_Click()
'Query Type Info
Dim strQuery As String
Dim StrQueryInfo As String
Dim strQueryName As String
StrQueryInfo = cmbQuery.Value
Select Case (StrQueryInfo)
'Number of Accidents Reported
Case "Accidents Reported"
strQueryName = "Number_Of_Accidents_Reported"
strQuery = "SELECT Count([CF663:CF663s_Table].[REPORT TYPE]) AS [Number Of Accidents]" & _
"FROM [CF663:CF663s_Table];"
Call AccidentsReported
'Number of Days Off
Case "Days Off"
strQueryName = "Number of Days Off"
strQuery = "SELECT Sum([CF663:CF663s_Table].[DAYS OFF DUTY]) AS [DaysOffDuty]" & _
"FROM [CF663:CF663s_Table];"
Call AccidentsReported
End Select
Set qrySearch = CurrentDb.CreateQueryDef(strQueryName, strQuery)
DoCmd.OpenQuery qrySearch.Name, acViewNormal, acReadOnly
db.QueryDefs.Delete qrySearch.Name
End Sub
Function AccidentsReported(strQuery As String, strQueryName As String, Optional strYearInfo As String, Optional strUnitInfo As String, _
Optional strFormation As String, Optional strPersonnelStatusInfo As String, Optional strSexofVictim As String, Optional strAccidentType As String, _
Optional strAgeOfVictim As String, Optional strHour As String, Optional strBodyPart As String, Optional strInjuryClass As String, _
Optional strDutyStatus As String, Optional strInjurySource As String, Optional strInjuryNature As String, Optional strDaysLightDuty As String, _
Optional strDaysOffDuty As String, Optional strSubunit As String, Optional strUIC As String, Optional db As DATABASE, Optional qrySearch As QueryDef, _
Optional Where As Variant) As String
AccidentsReported
Set db = CurrentDb
strYearInfo = DatePart("yyyy", [CF663:CF663s_Table].Date)
strUnitInfo = [CF663:CF663s_Table].[Unit Ser No]
strSubunit = [CF663:CF663s_Table].[Sub Unit]
strUIC = [CF663:CF663s_Table].UIC
strFormation = [CF663:CF663s_Table].Command
strPersonnelStatusInfo = [CF663:CF663s_Table].[Personal Status]
strSexofVictim = [CF663:CF663s_Table].SEX
strAccidentType = [CF663:CF663s_Table].[Accident Type]
strAgeOfVictim = [CF663:CF663s_Table].AGE
strHour = [CF663:CF663s_Table].HOUR
strBodyPart = [CF663:CF663s_Table].[Body Part]
strInjuryClass = [CF663:CF663s_Table].[Injury Classification]
strDutyStatus = [CF663:CF663s_Table].[Duty Status]
strInjurySource = [CF663:CF663s_Table].[Injury Source]
strInjuryNature = [CF663:CF663s_Table].[Injury Nature]
strDaysLightDuty = [CF663:CF663s_Table].[Days Light Duty]
strDaysOffDuty = [CF663:CF663s_Table].[Days Off Duty]
'Set Where equal to null before starting a new where clause
Where = Null
' Everything all combined
If optAllYearsCombined.Value And optAllUnitsCOmbined.Value And optAllInjuries.Value And _
optAllPersonnel.Value And optAllAges.Value And optBothLightAndOffDuty.Value And optAllDutyStatuses.Value And _
optBothSexes.Value And optAllHours.Value And optAllInjuryClasses.Value And optAllBodyParts.Value And _
optAllInjuryNatures.Value Then
strQuery = strQuery
' All years divided, all units combined, and everything else combined.
ElseIf optAllYearsDivided.Value And optAllUnitsCOmbined.Value And optAllInjuries.Value And _
optAllPersonnel.Value And optAllAges.Value And optBothLightAndOffDuty.Value And optAllDutyStatuses.Value And _
optBothSexes.Value And optAllHours.Value And optAllInjuryClasses.Value And optAllBodyParts.Value And _
optAllInjuryNatures.Value Then
strQuery = strQuery & "[Year([DATE])] AS Year" & _
"GROUP BY Year([DATE]);"
End If
End Function
I have just shown two of the statistics, and two of the if statements. There are lots more really. I hope this gives you an idea of what I am doing and that you can help me.
Tracy