Microsoft Access - 无需选择即可将列表框中的所有项目添加到表中?

时间:2016-03-04 11:34:18

标签: vba ms-access listbox

我正在创建一个数据输入表单,允许我在将它们导入各自的表之前添加该业务的新业务和详细信息。在大多数情况下,我已经完成了所有这些。

但是,有些信息需要多个输入。例如,企业可能有多个电话号码/文件/工作人员等。

我已经设置了一个文本框和一个添加/删除按钮,用于将文本框中的文本添加/删除到列表框中。我希望能够导入列表框中的所有项目,而无需将它们选择到表格中。这可能吗?我在网上找到的大部分答案都要求您选择这些项目。

我在导入按钮上的示例代码如下所示。此代码将业务的地址详细信息添加到“业务地址”表中。

'Set Table to 'Business Address' then add the fields to the table
Set RST = CurrentDb.OpenRecordset("Business Address", dbOpenTable)
RST.AddNew
RST![Business Name] = Me.txtBusinessName
RST![Address] = Me.txtAddress1 & ", " & Me.txtAddress2
RST![Town/City] = Me.txtTownCity
RST![Postal Code] = Me.txtPostalCode
RST.Update
RST.Close

我在想某种for循环将列表框中的所有项目添加到表中?

从逻辑上讲(这不是工作代码的真实示例,我认为如果可能的话,这可能会是什么样子?):

Set RST = CurrentDb.OpernRecordset("Business Telephone", dbOpenTable)
For each item in TelephoneListBox
    Rst.AddNew
    Rst![Business Name] = Me.txtBusinessName
    Rst![Telephone] = Me.TelephoneListBox.Column(0)

我不确定如何去做,如果没有选择项目可以实际完成?有什么想法吗?

2 个答案:

答案 0 :(得分:1)

Dim l As Long

For l = 0 To Me.List0.ListCount - 1

    '   Debug.Print Me.List0.ItemData(l) or me.list0.column(0,l)

Next l

这将遍历列表中的项目。

答案 1 :(得分:0)

改善Nathan_Sav的答案:

Dim i As Long
Set RST = CurrentDb.OpenRecordset("Business Telephone", dbOpenTable)

For i = 0 To Me.TelephoneListBox.ListCount - 1
        RST.AddNew
        Rst![Business Name] = Me.txtBusinessName
        RST![Telephone] = Me.TelephoneListBox.Column(0, i)
        RST.Update

Next i

'txtBusinessName'的值添加到指定字段(商家名称)下的表'商务电话'中列表框中的项目。

还会将列表框中的所有项目( TelephoneListBox )添加到指定字段(电话)下的同一个表格中。