从数据表复制到 ListBox
Posted
技术标签:
【中文标题】从数据表复制到 ListBox【英文标题】:Copy from datasheet to ListBox 【发布时间】:2018-05-23 11:39:44 【问题描述】:我在复制数据时遇到问题。我有一个显示为数据表的子表单,有 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】:示例代码。我总是将子表单/子报表容器控件命名为与其所拥有的对象不同的名称。子窗体容器控件名为 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
【讨论】:
以上是关于从数据表复制到 ListBox的主要内容,如果未能解决你的问题,请参考以下文章