MS Access过滤器表单打开特定记录的报告

时间:2015-10-24 04:37:11

标签: sql vba ms-access access-vba

我正在尝试创建一个表单,ReportSearch具有多个组合框和文本框,允许用户缩小报告中显示的结果范围。并非所有字段都必须在搜索中使用。

单击以下代码将要求为所使用的特定ID输入参数值。如果我只是单击确定而不在MsgBox中输入任何内容,则将打开报告而不显示任何记录。

Private Sub cmdFilter_Click()
Dim strWhere As String
Dim lngLen As Long

Const conJetDate = "\#mm\/dd\/yy\#"

If Not IsNull(Me.cboSearchJob) Then
   strWhere = strWhere & "(Job.id = " & Me.cboSearchJob & ") AND "
End If

If Not IsNull(Me.cboSearchEmployee) Then
   strWhere = strWhere & "(Employee.ID = " & Me.cboSearchEmployee & ") AND "
End If

If Not IsNull(Me.cboSearchService) Then
   strWhere = strWhere & "(Service.ID = " & Me.cboSearchService & ") AND "
End If

If Not IsNull(Me.tboStartDate) Then
    strWhere = strWhere & "(DateWorked >= " & Format(Me.tboStartDate, conJetDate) & ") AND "
End If
If Not IsNull(Me.tboEndDate) Then
   strWhere = strWhere & "(DateWorked < " & Format(Me.tboEndDate + 1, conJetDate) & ") AND "
End If

lngLen = Len(strWhere) - 5
If lngLen <= 0 Then
   MsgBox "No Results", vbInformation, "No Search Available."
Else
    strWhere = Left$(strWhere, lngLen)

    DoCmd.OpenReport "JobReport", acViewPreview
    Reports!JobReport.Filter = strWhere
    Reports!JobReport.FilterOn = True

   End If


End Sub

对于我正在使用的报告

  SELECT [Employees].[FirstName] & " " & [Employees].[LastName] AS EmployeeName, Jobs.JobName, Equipment.Model, Service.Service, Labor.Labor, EmployeeWorkLog.LaborHours, EmployeeWorkLog.EquipmentHours, EmployeeWorkLog.Notes, EmployeeWorkLog.DateWorked, Service.ID
FROM Service RIGHT JOIN (Labor RIGHT JOIN (Jobs RIGHT JOIN (Equipment RIGHT JOIN (Employees RIGHT JOIN EmployeeWorkLog ON Employees.ID = EmployeeWorkLog.EmployeeID) ON Equipment.ID = EmployeeWorkLog.EquipmentID) ON Jobs.ID = EmployeeWorkLog.JobID) ON Labor.ID = EmployeeWorkLog.LaborID) ON Service.ID = EmployeeWorkLog.ServiceID
ORDER BY [Employees].[FirstName] & " " & [Employees].[LastName];

此时我完全失败了。

显然我在这里缺少一些东西。让我知道我需要以什么方式修改此代码,以便根据我的表单ReportSearch打开我的报告JobReport过滤。

感谢。

1 个答案:

答案 0 :(得分:0)

报表不像表单那样动态,您无法在打开后对其进行过滤。在打印预览中打开报表时,它会读取其数据,进行布局和格式化,然后它是完全静态的。

要打开包含已过滤数据的报告,请使用WhereCondition的{​​{1}}参数:

DoCmd.OpenReport