填充多列列表框控件MSAccess VBA

时间:2016-01-29 15:42:06

标签: vba ms-access listbox dao

如何在具有多个VBA列的MSAccess中填充列表框控件?我已经搜索了StackOverFlow来查找有限的示例Here,但没有什么可以帮助初级级别的修补程序通过SQL语句从另一个表的字段中绘制

1 个答案:

答案 0 :(得分:0)

以下是使用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