将多个列表框项加载到数据库

Loading Multiple Listbox Items to database

我有一个包含多个列表框的表单,我想知道如何将列表框中的项目加载到我的访问数据库中。

dim db as dao.database
dim rs as dao.recordset
dim strsql as string
dim item as variant    

set db = currentdb()

strsql = "SELECT * FROM TBL WHERE ID = '"& a & "'"

set rs = db.openrecordset(strsql)

for i = 0 to me.lst1.listcount -1)
    '"stuck here or I may be using the wrong code..."
next

欢迎任何帮助...

如果我没记错的话,你的问题可以这样解决:

Sub Sample()

Dim ctl as Control
Dim idx As Long
Dim db as Database
Dim rec as Recordset

Set db = CurrentDB
Set rec = db.OpenRecordset("Select * from MyTable")

    'Find each listbox control on the form, and get its contents
    For Each ctl In me.Controls
        If ctl.ControlType = acListBox Then
            'If it's a listbox, write the contents to a table
            For idx = 0 To ctl.ListCount - 1
                rec.AddNew
                rec("MyDataField") = ctl.ItemData(idx)
                rec.Update
            Next
        End If
    Next

End Sub

除了 JOhnny Bones 发布的代码之外,如果您想跟踪数据源,您还可以添加对名称的检查。

属性是ctl.Name

再见,

维兹