【问题标题】:How to filter an array in MS Access如何在 MS Access 中过滤数组
【发布时间】:2017-11-14 17:19:11
【问题描述】:

我正在编写一个可以与我的数据库(查询)进行比较的数组。我可以知道如何制作它,以便它可以在我的数组中包含“或”逻辑吗?因为我想得到像 Filter = myArray(0) 或 myArray(1) 或 myArray(2) 这样的输出......因为我没有我的数组的数量,因为它取决于用户有多少输入类型,所以我只有一个 myArray(x)。 谢谢。

Private Sub TestButton_Click()
Dim myArray As Variant
Dim Status_Input As Variant
Dim x As Variant
Dim i As Variant
Dim Status_Filter As Variant

Status_Input = Forms![try2].StatusInput

myArray = Split(Status_Input, ";")

For x = LBound(myArray) To UBound(myArray)
MsgBox " " & myArray(x) & " "             'MsgBox = 03 20 27 (3 MsgBoxes)

Status_Filter = _
"[Status] Like " & myArray(x)

Debug.Print Status_Filter
Me.Filter = Status_Filter
Me.FilterOn = True

Next x

结束子

【问题讨论】:

    标签: arrays ms-access


    【解决方案1】:

    您可以相互附加过滤器,用OR 分隔它们。另外,我建议您仅在需要使用通配符时才使用Like

    如果你不使用Like,你可以使用[Status] IN(1, 2, 3)而不是[Status] Like 1 OR [Status] Like 2 OR [Status] Like 3来优化代码。

    Status_Filter = "[Status] Like " & myArray(LBound(myArray)) 'Note: fails if myArray is empty
    For x = LBound(myArray) + 1 To UBound(myArray)
        Status_Filter = Status_Filter & " OR [Status] Like " & myArray(x)
    Next x
    Me.Filter = Status_Filter
    Me.FilterOn = True
    

    【讨论】:

      【解决方案2】:

      阅读您的代码,您最终可以尝试另一条路径。

      status_filter = "status IN ( " & replace(status_input,";",",") & ")"
      

      这应该评估为:

      status_filter = "status IN (2, 4, 6)"
      

      由于 status_input 已经是一个列表,如果值是数字,上面应该可以工作,并且是有效的。

      【讨论】:

      • 感谢您的建议。它们不是数字怎么样?
      • 因为我发现我必须为我的输入文本添加一个''才能使过滤器工作。例如'ABCD';'EDKN';'JTS'
      • @BeckoChiu 对,但我认为您应该使用 'ABCD'、'EDKN'、'JTS' 代替(使用 ,,而不是 ;
      猜你喜欢
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 2015-05-27
      相关资源
      最近更新 更多