如何在Microsoft Access窗体中创建带复选框的ListBox?

时间:2015-11-25 09:26:58

标签: ms-access checkbox listbox ms-access-2010

我是Microsoft Access的新手,想要创建一个带复选框的ListBox(或ListView),但我无法找到任何原生方式。

我的目的是显示一个值列表,并根据在表单上的ComboBox中选择的值来检查一些值。

请注意,我需要对表单而不是表格进行此类控制(对于此"多值查找字段")。 (此外,如果有一种方法可以创建一个子表单,其中只有一个表格,其中只有多值列,它对ComboBox中选择的内容做出反应。)

一个普通的列表框,带有" Multi Select"属性设置为"简单"不显示复选框。
我也看不到" ListStyle"财产描述here 也许它可以以某种方式在ListBox中显示两列,其中第一列呈现为复选框?

3 个答案:

答案 0 :(得分:5)

您可以使用ListView控件。它位于 ActiveX控件下,全名为Microsoft ListView Control, version 6.0

它有一组单独的属性:右键单击 - > ListViewCtrl对象 - >属性,有Checkboxes属性。

要使用数据填充列表视图,请参阅例如ACC: Sample Function to Fill a ListView Control

更多信息:Using the ListView Control

修改
要轻松使用Listview对象模型,请在我的Windows7 64位上设置对Microsoft Windows Common Controls 6.0 = C:\Windows\SysWOW64\MSCOMCTL.OCX的引用。

修改2

我使用带有复选框的TreeView。每个Node都有Checked属性,用于检查或取消选中其复选框。 Treeview有节点的地方,Listview有ListItems,但它们也有Checked属性。

Treeview的简化代码(没有层次结构):

Dim oTree As TreeView
Dim oNode As Node
Dim RS As Recordset

Set oTree = Me.myTreeView.Object
oTree.Nodes.Clear

Set RS = DB.OpenRecordset("My query to fill the treeview")  
Do While Not RS.EOF
    Set oNode = oTree.Nodes.Add(key:=RS!foo, Text:=RS!bar)
    oNode.Checked = (RS!someValue > 0)
    RS.MoveNext
Loop
RS.Close

答案 1 :(得分:0)

您不能像这样修改Access的列表框,但您可以在数据表视图中自定义子窗体以模仿这样的列表框。

要显示更多或更少的固定值,请创建一个由表单绑定的小型本地表,并用您需要的值填充它。

答案 2 :(得分:0)

现在,在安德烈回答的帮助下,让它工作了:

首先,由于ListView依赖于当前所选的表项,因此我通过表的 Form_Current 事件填充它。 (只需Call Forms.Item("MainForm").PopulateListView

这是有效的PopulateListView方法(请注意,您需要先引用 Microsoft Windows Common Controls 6.0 ):

Public Sub PopulateListView()
    On Error GoTo ErrorHandler
    Dim intToCount As Integer
    Dim intCount1 As Integer
    Dim intCount2 As Integer
    Dim intToCount2 As Integer
    Dim intCount12 As Integer
    Dim intCount22 As Integer
    Dim NewLine As Object
    Dim db As Database
    Dim rs As Recordset
    Dim colNew As Object
    Dim s As String

    ' Clear the ListView control.
    Forms![MainForm].[SubForm].Form.[ctlListView].ListItems.Clear
    Forms![MainForm].[SubForm].Form.[ctlListView].ColumnHeaders.Clear

    ' Set Variables.
    Set db = CurrentDb    
    Set rs = db.OpenRecordset("SELECT A, B, IsChecked . . .")

    ' Set Column Headers.
    Set colNew = Forms![MainForm].[SubForm].Form.[ctlListView].ColumnHeaders.Add(, , "A", 2000)
    Set colNew = Forms![MainForm].[SubForm].Form.[ctlListView].ColumnHeaders.Add(, , "B", 4000)

    ' Set Total Records Counter.
    rs.MoveLast
    intToCount = rs.RecordCount
    rs.MoveFirst

    ' Loop through recordset and add Items to the control. Twice as a workaround to sort by checkbox.
    For intCount1 = 1 To intToCount
        If (rs(2).value = 1) Then
            If IsNumeric(rs(0)) Then
                s = Trim(Str(rs(0).value))
            Else
                s = Trim(rs(0).value)
            End If

            Set NewLine = Forms![MainForm].[SubForm].Form.[ctlListView].ListItems.Add(, , s)
            If IsNull(rs(1)) Then
                NewLine.ListSubItems.Add Text:=""
            Else
                NewLine.ListSubItems.Add Text:=rs(1).value
            End If
            NewLine.Checked = True
        End If
        rs.MoveNext
    Next intCount1

    ' Set Total Records Counter.
    rs.MoveLast
    intToCount2 = rs.RecordCount
    rs.MoveFirst

    For intCount12 = 1 To intToCount2
        If (rs(2).value = 0) Then
            If IsNumeric(rs(0)) Then
                s = Trim(Str(rs(0).value))
            Else
                s = Trim(rs(0).value)
            End If

            Set NewLine = Forms![MainForm].[SubForm].Form.[ctlListView].ListItems.Add(, , s)
            If IsNull(rs(1)) Then
                NewLine.ListSubItems.Add Text:=""
            Else
                NewLine.ListSubItems.Add Text:=rs(1).value
            End If
        End If
        rs.MoveNext
    Next intCount12
Exit Sub
ErrorHandler:
    ' Err 3021 = no current record. Err 2455 = happens at necessary first call of method and couldn't catch in code.
    If Err = 91 Or Err = 3021 Or Err = 2455 Then
       Resume Next
    Else
        If Err <> 94 Then
            ' Otherwise display the error message.
            MsgBox "Error: " & Err.Number & Chr(13) & Chr(10) & Err.Description & vbCrLf & "(PopulateListView)"
        End If
    End If
End Sub

然后保存我正在使用它:

For Each Item In Forms![MainForm].[SubForm].Form.[ctlListView].Object.ListItems
   If Item.Checked = True Then
     'Use Item here
   End If
Next