【问题标题】:Access VBA: Compare two listboxesAccess VBA:比较两个列表框
【发布时间】:2017-01-23 16:24:20
【问题描述】:

我在 Access VBA 中有两个列表框。 我想比较这两个列表框,如果第一个列表框中没有列出相同的项目,我想从第二个列表框中删除项目。

例如: 列表框 1 值:“项目 1”、“项目 3” 列表框 2 值:“第 1 项”、“第 2 项”、“第 3 项”

现在我想要一个比较这两个列表框并从列表框 2 中删除“项目 2”的函数,因为它没有在列表框 1 中列出。

我尝试了一些代码,但我唯一得到的就是这个:

If BR_TeamReport.ListCount > 0 Then
    For i = 0 To BR_TeamReport.ListCount - 1
        For y = 0 To BR_Team.ListCount - 1
            If BR_TeamReport.ItemData(i) = BR_Team.ItemData(y) Then
                MsgBox ("Don't Delete")
            Else
                MsgBox ("Delete")
            End If
        Next y
    Next i
End If

【问题讨论】:

  • 我假设 Listbox 1 还具有其他值,例如“Item 4”,因此每个列表框都包含不同的值。

标签: vba ms-access listbox compare


【解决方案1】:

考虑一下:

If BR_TeamReport.ListCount > 0 Then
    For i = 0 To BR_TeamReport.ListCount - 1
        FoundItem = False
        For y = 0 To BR_Team.ListCount - 1
            If BR_TeamReport.ItemData(i) = BR_Team.ItemData(y) Then
                FoundItem = True
            End If
        Next y
        If Not FoundItem Then
            ' Delete item as it was not found in the first listbox
            BR_Team.RemoveItem (y)
        End If
    Next i
End If

我认为这将实现您想要的。

【讨论】:

  • 请将我的答案标记为答案并向上箭头。谢谢
  • 抱歉,刚才是这样做的:)
【解决方案2】:

为什么不清除第二个列表框,然后用第一个重新填充?

For i = 0 To LstBox2.ListCount - 1
   LstBox2.RemoveItem(i)
next i 

For i = 0 TO LstBox1.ListCount - 1
   lstBox2.AddItem LstBox1.ItemData(i)
Next i

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    相关资源
    最近更新 更多