【发布时间】:2016-01-29 15:42:05
【问题描述】:
如何在 MSAccess 中使用 VBA 填充具有多列的列表框控件?我搜索了 StackOverFlow 以找到有限的示例 Here,但没有任何东西可以帮助初级修补程序通过 SQL 语句从另一个表的字段中绘制
【问题讨论】:
-
我想我会发布这个,因为我搜索了这个场景 - 实际上是在网上搜索 - 并且只发现了几个不太有效的稀疏示例。
如何在 MSAccess 中使用 VBA 填充具有多列的列表框控件?我搜索了 StackOverFlow 以找到有限的示例 Here,但没有任何东西可以帮助初级修补程序通过 SQL 语句从另一个表的字段中绘制
【问题讨论】:
这是一个使用 SQL 语句的示例,该语句从具有 3 列的单个表中进行选择
Dim db As DAO.Database
Dim cnxn As Object
Dim rst As DAO.Recordset
Dim strsql As String
Set db = CurrentDb()
Set cnxn = Application.CurrentProject.Connection
strsql = "SELECT [Tbl A].[Fld 1], [Tbl A].[Fld 2],[Tbl A].[Fld 3]" _
& "FROM [Tbl A] INNER JOIN [Tbl A]" _
& "ON [Tbl B].[Fld 1] = [Tbl A].[Fld 1];"
Set rst = db.OpenRecordset(strsql, dbOpenDynaset)
Me.Lstbox.ColumnCount = 3
Me.Lstbox.ColumnWidths = "0.6 in;0.9 in;1 in"
With rst
.MoveFirst
Do Until .EOF
'Fields are listed in ordinal position with (0) as being the 1st
Me.Lstbox.AddItem rst.Fields(0).Value & ";" & rst.Fields(1) & ";" & rst.Fields(2)
rst.MoveNext
Loop
End With
rst.Close
LstVsn.Requery
Set cnxn = nothing
Set db = nothing
End Sub
【讨论】: