代码之家  ›  专栏  ›  技术社区  ›  mits

MS Access:如何在按下按钮时从组合框中删除所选项目?

  •  2
  • mits  · 技术社区  · 6 年前

    案例: 我在表单中有一个commandbutton和一个未绑定的组合框(rowsourcetype:值列表-使用vba从另一个表单填充)。

    目标: 当用户单击CommandButton时,组合框中要从此组合框中删除的选定项。

    尝试: 我在commandbutton的click事件中使用了组合框的removietem方法,它需要删除项的索引。 为了得到所选项的索引,我尝试使用组合框的selected属性,遍历所有的组合框项,但是selected属性始终返回0,这是对所选项的无遗憾。

    Private Sub bDelete_Click()
    Dim i As Integer
        For i = 0 To Me.cAnswered.ListCount - 1
            If Me.cAnswered.Selected(i) = True Then
                'MsgBox i
                'Stop
                Me.cAnswered.RemoveItem i
                Exit For
            End If
        Next
        Me.bDelete.Visible = (Me.cAnswered.ListCount > 0)
    End Sub
    

    你能告诉我怎样才能达到这个目标吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Santosh    6 年前

    试试这个

    Private Sub bDelete_Click()
     Dim i As Integer
        For i = 0 To Me.cAnswered.ListCount - 1
            If Me.cAnswered.ItemData(i) = cAnswered.Value Then
                Me.cAnswered.RemoveItem i
                Exit For
            End If
        Next
    
        cAnswered = Null
    End Sub
    
    推荐文章