我正在尝试创建一个具有多个组合框和文本框的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。
谢谢。
报表不像表单那样动态,您无法在打开报表后对其进行过滤。在打印预览中打开报表时,它会读取其数据,进行布局和格式设置,然后完全是静态的。
要打开包含过滤数据的报告,请使用WhereCondition
参数DoCmd.OpenReport
:
DoCmd.OpenReport "JobReport", acViewPreview, WhereCondition:=strWhere
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句