我正在寻找一些关于如何使用VBA从SQL派生的数据填写组合框/列表框的建议。
Sub RunSELECT()
Dim cn As Object, rs As Object, output As String, sql As String
'---Connecting to the Data Source---
Set cn = CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = ""
.Open
End With
'---Run the SQL SELECT Query---
sql = "SELECT DISTINCT i.* FROM tblITEMS i INNER JOIN tblITEM_ATTRIBUTE_VALUES iav1 on i.PART_NO = iav1.PART_NO and i.REV = iav1.rev and iav1.ATTRIBUTE_NAME = 'Item' AND iav1.ATTRIBUTE_VALUE = 'X100' INNER JOIN tblITEM_ATTRIBUTE_VALUES iav2 on i.PART_NO = iav2.PART_NO and i.REV = iav2.rev and iav2.ATTRIBUTE_NAME = 'Item Name' AND iav2.ATTRIBUTE_VALUE = 'Variable' ORDER BY i.PART_NO, i.REV"
Set rs = cn.Execute(sql)
Do
output = output & rs(0) & ";" & rs(1) & ";" & rs(2) & vbNewLine
Debug.Print rs(0); ";" & rs(1) & ";" & rs(2)
rs.MoveNext
Loop Until rs.EOF
MsgBox output
'---Clean up---
rs.Close
cn.Close
Set cn = Nothing
Set rs = Nothing
End Sub
以上创建了一个有效的msgbox,并提供了信息。我可以导出这个'输出'到一个单元格,但单元格内容应该填充多个单元格,而不仅仅是一个单元格。
如何更改我的代码以导出'输出'到多个单元格(需要多少)或组合/列表框?
提前干杯
答案 0 :(得分:0)
要输出到组合框或列表框,请替换此部分:
Do
output = output & rs(0) & ";" & rs(1) & ";" & rs(2) & vbNewLine
Debug.Print rs(0); ";" & rs(1) & ";" & rs(2)
rs.MoveNext
Loop Until rs.EOF
用这个:
listboxname.Column = rs.GetRows