A
ASchaum
I have a database that was designed for my non-profit a few years ago and I
just discovered that one of the reports doesn't limit the records being
pulled correctly. The designer created a form with assorted options that
then pulls a report based on those options. The field posing the problem is
the CompanyID (my primary key for my company table) - entries for 3 records
(V332, V3320 and V3328) are appearing on a report when I only want the V332
entry.
How can I limit the string to exactly what is selected on the form?
Below is the code from the button that pulls the report:
Private Sub Command13_Click()
Dim MySQL As String, MyCriteria As String, MyRecordSource As String
Dim StrSQL As String, strRestrict As String
Dim lngX As Long
Dim ArgCount As Integer
Dim Tmp As Variant
Dim i As Integer
Dim strCategories As String
Dim intCount As Integer
MySQL = "SELECT tService.ServiceID, tService.CompanyID,
tService.ServiceDate, tService.Staff, tService.Why, tService.ServiceCategory,
tService.ServiceDesc, tService.Priority, tService.ServiceProvider,
tService.Status, tService.NextStep, tService.CallBackDate,
tService.DateServiceCompleted, tService.PotentialServiceCategory,
tService.Referal, tService.[Service Provided], tService.Completed,
tService.ServiceDueDate, tService.TimeInvestment, tService.ProgramArea,
tCompany.CompanyName, tCompany.LDCLoc " _
& "FROM tService INNER JOIN tCompany ON tService.CompanyID =
tCompany.CompanyID " _
& "WHERE "
'Build Criteria
AddToWhere [CDCArea], "[LDCLoc]", MyCriteria, ArgCount
AddToWhere [Staff], "[ServiceProvider]", MyCriteria, ArgCount
AddToWhere [Status], "[Completed]", MyCriteria, ArgCount
AddToWhere [CompanyName], "[tCompany].[CompanyID]", MyCriteria, ArgCount
If MyCriteria = "" Then
MyCriteria = "True"
End If
'Build Date Range
If Me.StartDate <> "" And Me.EndDate <> "" Then
If MyCriteria <> "True" Then
MyCriteria = MyCriteria & " And " & "[ServiceDate] Between " & "#" &
Me.StartDate & "#" & " And " & "#" & Me.EndDate & "# "
Else
MyCriteria = "[ServiceDate] Between " & "#" & Me.StartDate & "#" & "
And " & "#" & Me.EndDate & "# "
End If
End If
'Check to make sure at least one Category is selected
For i = 0 To List0.ListCount - 1
If List0.Selected(i) Then
intCount = intCount + 1
End If
Next i
If intCount >= 1 Then ' Selected at least one
'Built string to hold student id's
strCategories = "IN("
For i = 0 To List0.ListCount - 1
If List0.Selected(i) Then
strCategories = strCategories & "'" & List0.Column(0, i)
& "'" & ","
End If
Next i
If MyCriteria = "TRUE" Then
MyCriteria = "[ServiceCategory] " & Left(strCategories,
Len(strCategories) - 1) & ")"
Else
MyCriteria = MyCriteria & " And [ServiceCategory] " &
Left(strCategories, Len(strCategories) - 1) & ")"
End If
End If
MyRecordSource = MySQL & MyCriteria
Me.Criteria = MyRecordSource
If Me.Frame28 = 1 Then 'Detail Report Based On Selected Criteria
DoCmd.OpenReport "rptServiceRequest", acViewPreview
ElseIf Me.Frame28 = 2 Then 'Summary Report Based On Selected
Criteria, Need to Select Grouping
Select Case Me.Frame36
Case 1 'Grouped by Service Category
DoCmd.OpenReport "rptServiceSummaryByCategory",
acViewPreview
Case 2 'Grouped by Service Provider
DoCmd.OpenReport "rptServiceSummaryByProvider",
acViewPreview
Case 3 'Grouped by CDC Location
DoCmd.OpenReport "rptServiceSummaryByCDC", acViewPreview
End Select
End If
DoCmd.Close acForm, Me.Name
Forms![frmReportSwitchboard].Visible = False
End Sub
just discovered that one of the reports doesn't limit the records being
pulled correctly. The designer created a form with assorted options that
then pulls a report based on those options. The field posing the problem is
the CompanyID (my primary key for my company table) - entries for 3 records
(V332, V3320 and V3328) are appearing on a report when I only want the V332
entry.
How can I limit the string to exactly what is selected on the form?
Below is the code from the button that pulls the report:
Private Sub Command13_Click()
Dim MySQL As String, MyCriteria As String, MyRecordSource As String
Dim StrSQL As String, strRestrict As String
Dim lngX As Long
Dim ArgCount As Integer
Dim Tmp As Variant
Dim i As Integer
Dim strCategories As String
Dim intCount As Integer
MySQL = "SELECT tService.ServiceID, tService.CompanyID,
tService.ServiceDate, tService.Staff, tService.Why, tService.ServiceCategory,
tService.ServiceDesc, tService.Priority, tService.ServiceProvider,
tService.Status, tService.NextStep, tService.CallBackDate,
tService.DateServiceCompleted, tService.PotentialServiceCategory,
tService.Referal, tService.[Service Provided], tService.Completed,
tService.ServiceDueDate, tService.TimeInvestment, tService.ProgramArea,
tCompany.CompanyName, tCompany.LDCLoc " _
& "FROM tService INNER JOIN tCompany ON tService.CompanyID =
tCompany.CompanyID " _
& "WHERE "
'Build Criteria
AddToWhere [CDCArea], "[LDCLoc]", MyCriteria, ArgCount
AddToWhere [Staff], "[ServiceProvider]", MyCriteria, ArgCount
AddToWhere [Status], "[Completed]", MyCriteria, ArgCount
AddToWhere [CompanyName], "[tCompany].[CompanyID]", MyCriteria, ArgCount
If MyCriteria = "" Then
MyCriteria = "True"
End If
'Build Date Range
If Me.StartDate <> "" And Me.EndDate <> "" Then
If MyCriteria <> "True" Then
MyCriteria = MyCriteria & " And " & "[ServiceDate] Between " & "#" &
Me.StartDate & "#" & " And " & "#" & Me.EndDate & "# "
Else
MyCriteria = "[ServiceDate] Between " & "#" & Me.StartDate & "#" & "
And " & "#" & Me.EndDate & "# "
End If
End If
'Check to make sure at least one Category is selected
For i = 0 To List0.ListCount - 1
If List0.Selected(i) Then
intCount = intCount + 1
End If
Next i
If intCount >= 1 Then ' Selected at least one
'Built string to hold student id's
strCategories = "IN("
For i = 0 To List0.ListCount - 1
If List0.Selected(i) Then
strCategories = strCategories & "'" & List0.Column(0, i)
& "'" & ","
End If
Next i
If MyCriteria = "TRUE" Then
MyCriteria = "[ServiceCategory] " & Left(strCategories,
Len(strCategories) - 1) & ")"
Else
MyCriteria = MyCriteria & " And [ServiceCategory] " &
Left(strCategories, Len(strCategories) - 1) & ")"
End If
End If
MyRecordSource = MySQL & MyCriteria
Me.Criteria = MyRecordSource
If Me.Frame28 = 1 Then 'Detail Report Based On Selected Criteria
DoCmd.OpenReport "rptServiceRequest", acViewPreview
ElseIf Me.Frame28 = 2 Then 'Summary Report Based On Selected
Criteria, Need to Select Grouping
Select Case Me.Frame36
Case 1 'Grouped by Service Category
DoCmd.OpenReport "rptServiceSummaryByCategory",
acViewPreview
Case 2 'Grouped by Service Provider
DoCmd.OpenReport "rptServiceSummaryByProvider",
acViewPreview
Case 3 'Grouped by CDC Location
DoCmd.OpenReport "rptServiceSummaryByCDC", acViewPreview
End Select
End If
DoCmd.Close acForm, Me.Name
Forms![frmReportSwitchboard].Visible = False
End Sub