我有一个包含多个组合框的表单,可以根据我的组合框中的值打开报表。这有效,但我被要求提供生效日期字段的选项,以便输入日期范围。
我为开头和结尾添加了两个文本框(txtReqCreationStartDate
和txtReqCreationEndDate
)。
我尝试在...和查询之间进行操作,但后来我的组合框不起作用。
我从未使用过SQL但是在网上发现了这个并在点击按钮上的Apply Filter的代码中输入了它,但是我得到了所有的记录。其他人试图帮助,但我总是得到所有的记录。
以下是On Click事件的所有代码。
Private Sub cmdVacanciesWithNoRequisitionParameters_Click()
On Error GoTo cmdVacanciesWithNoRequisitionParameters_Click_Err
strSQL = "SELECT * FROM qryVacanciesWithNoRequisition WHERE " _
& "DateValue([Effective Date]) Between #" & Format([txtReqCreationStartDate], "yyyy-mm-dd") & "# And #" & Format([txtReqCreationEndDate], "yyyy-mm-dd") & "#;"
'Person Number
If Not IsNull(Me.cboPersonNumber) Then
strFilter = strFilter & " AND [Person Number] Like """ & Me.cboPersonNumber & """ "
End If
'Person Name
If Not IsNull(Me.cboPersonName) Then
strFilter = strFilter & " AND [Person Name] Like """ & Me.cboPersonName & """ "
End If
'Job Code
If Not IsNull(Me.cboJobCode) Then
strFilter = strFilter & " AND [Job Code] Like """ & Me.cboJobCode & """ "
End If
'Person Number
If Not IsNull(Me.cboBusinessUnit) Then
strFilter = strFilter & " AND [Business Unit] Like """ & Me.cboBusinessUnit & """ "
End If
'Department
If Not IsNull(Me.cboDepartment) Then
strFilter = strFilter & " AND [Department] Like """ & Me.cboDepartment & """ "
End If
'Supervisor
If Not IsNull(Me.cboSupervisor) Then
strFilter = strFilter & " AND [Supervisor] Like """ & Me.cboSupervisor & """ "
End If
'Job Title
If Not IsNull(Me.cboJobTitle) Then
strFilter = strFilter & " AND [Job Title] Like """ & Me.cboJobTitle & """ "
End If
'If the report is closed, open the report
If SysCmd(acSysCmdGetObjectState, acReport, "rptVacanciesWithNoRequisition") <> acObjStateOpen Then
DoCmd.OpenReport "rptVacanciesWithNoRequisition", acPreview, qryVacanciesWithNoRequisition
End If
'if report was open, use filter
With Reports![rptVacanciesWithNoRequisition]
.Filter = Mid(strFilter, 6)
.FilterOn = True
End With
cmdVacanciesWithNoRequisitionParameters_Click_Exit:
Exit Sub
cmdVacanciesWithNoRequisitionParameters_Click_Err:
MsgBox Error$
Resume cmdVacanciesWithNoRequisitionParameters_Click_Exit
End Sub
提前感谢您的帮助。
答案 0 :(得分:0)
它应该适合您 - 您只需要使用与过滤器使用的相同变量名称进行复制/粘贴。
更改强>
strSQL = "SELECT * FROM qryVacanciesWithNoRequisition WHERE " _
& "DateValue([Effective Date]) Between #" & Format(Me.txtReqCreationStartDate, "yyyy-mm-dd") & "# And #" & Format(Me.txtReqCreationEndDate, "yyyy-mm-dd") & "#;"
编辑 - 现在知道生效日期是文本字段
将其更改为:
If (IsDate(Me.txtReqCreationStartDate) AND IsDate(Me.txtReqCreationEndDate)) then
strFilter = strFilter & " CDate([Effective Date]) Between #" &
CDate(Me.txtReqCreationStartDate) & "# And #" & _
CDate(Me.txtReqCreationEndDate) & "#"
End If