从数据表复制到ListBox

时间:2018-05-23 11:39:44

标签: ms-access datasheet

我在复制数据时遇到问题。我有一个显示为数据表的子表单,有8列和动态行数。我有一个代码,如果您在数据表中选择一个单元格并按下按钮,它会将该行复制到ListBox。但我不知道如何制作一个循环,将所有行(或选定的一对)复制到列表框。如果选择了多个行,则仅复制第一行。

Private Sub Command_Click()
 Dim item(1 To 8) As String
  With frmPresentationList

    item(1) = ![Col1]
    item(2) = ![Col2]
    item(3) = ![Col3]
    item(4) = ![Col4]
    item(5) = ![Col5]
    item(6) = ![Col6]
    item(7) = ![Co87]
    item(8) = ![Col1]
 End With

 With Me.selectedItems
    .AddItem Join(item, ";")
 End With
End Sub

谢谢!

1 个答案:

答案 0 :(得分:0)

示例代码。我总是将子表单/子报表容器控件命名为与它所拥有的对象不同。子窗体容器控件名为ctrRates。

Option Compare Database
Option Explicit

Dim intHeight As Integer    'stores value for number of records selected
Dim intTop As Integer   'stores value for position of first selected record

Private Sub ctrRates_Exit(Cancel As Integer)
    intHeight = Me.ctrRates.Form.SelHeight
    intTop = Me.ctrRates.Form.SelTop
End Sub

Private Sub btnFillListbox_Click()
    Dim N As Integer
    If intHeight > 0 Then
        Me.SelectedItems.RowSource = ""
        With Me.ctrRates.Form.RecordsetClone
            .MoveFirst
            'AbsolutePosition property is 0 based counter so must -1 to set RecordsetClone position
            .AbsolutePosition = intTop - 1
            For N = 1 To intHeight
                Me.SelectedItems.AddItem (!RateID)
                .MoveNext
            Next
        End With
    End If
End Sub