【问题标题】:Build search filter on form to find records在表单上构建搜索过滤器以查找记录
【发布时间】:2015-09-25 03:31:56
【问题描述】:

我有一个表单 TimeCardSplit,它是一个拆分表单。我希望能够在其记录源 EmployeeWorkLog 中搜索基于特定字段的记录以填充表单以进行更正。

我有两个组合框,一个文本框和一个列表框,我想用它们来过滤我的结果。它们是 cboSearchEmployee、cboSearchJob、tboSearchDate 和 lstSearch。我想让 lstSearch 显示 EmployeeWorkLog 中的所有记录,并能够使用 cboSearchEmployee、cboSearchJob 和 tboSearchDate 对其进行过滤。一旦记录被过滤得足够多,我就可以在 lstSearch 中双击它们并以 TimeCardSplit 的形式显示它们。

感谢您提供的任何建议。

感谢收看。

我进行了更多搜索,并决定使用子表单而不是使用拆分表单。我发现我可以使用以下代码过滤子表单。

Private Sub cboSearchJob_AfterUpdate()
 If IsNull(Me.cboSearchJob) Then
    Me.cldTimeCard.Form.Filter = ""
    Me.cldTimeCard.Form.FilterOn = False
Else
    Me.cldTimeCard.Form.Filter = "[JobID]=" & Me.cboSearchJob
    Me.cldTimeCard.Form.FilterOn = True
End If
 Exit Sub
  End Sub

现在我正在尝试找到一个函数来双击过滤后的行以将其带入表单。目前我正在使用...

Private Sub cboJob_DblClick(Cancel As Integer)
    DoCmd.OpenForm "Try", , , "[JobID] = " & Me!JobID
End Sub

这当然只在我的子表单中的一个组合框中。虽然这可行,但它只会将我的过滤结果的第一个实例带入表单,而不是我双击的特定行。

所以这是我的新问题。有没有更好的地方放置此代码或类似代码以使用我双击的行填充表单?

再次感谢您的关注。

【问题讨论】:

  • 你写了什么?你有什么问题?当手头没有问题时,我不相信 StackOverflow 会在这里提供完整的解决方案。
  • 请先做一些研究。您已经拥有正确的关键字作为标签或标题,您可以在 StackOverflow 中找到帖子。如果您有特定问题,您可以通过显示代码等方式提出问题。

标签: database ms-access search filter vba


【解决方案1】:

这是我找到的解决方案,效果很好。 June7 here 给了我解决方案。非常感谢您的帮助。

enter 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 & "([JobID] = " & Me.cboSearchJob & ") AND "
End If

If Not IsNull(Me.cboSearchEmployee) Then
    strWhere = strWhere & "([EmployeeID] = " & Me.cboSearchEmployee & ") AND "
End If

If Not IsNull(Me.cboSearchService) Then
    strWhere = strWhere & "([ServiceID] = " & 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 criteria", vbInformation, "Nothing to do."
Else                    
    strWhere = Left$(strWhere, lngLen)  

    Me.cldTimeCard.Form.Filter = strWhere
    Me.cldTimeCard.Form.FilterOn = True
End If

在此结束子代码

感谢大家观看并帮助我完成这项工作。现在进入下一个...

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 2017-05-21
    • 2014-06-29
    • 1970-01-01
    相关资源
    最近更新 更多