问题
I want to load some data from Access into Excel Userform ListBox. What I am doing now is creating ADODB.Connection
to connect access and create ADODB.Recordset
to store data, firstly. Secondly, I use Range("xx").CopyFromRecordset
to copy the data to excel sheet. Thirdly, name that excel range as "ResultSet
". Fourthly, use Me.ListName.RowSource="ResultSet"
to copy data from excel sheet to ListBox.
As you can see, I use four steps to finish this job. Is there a way to skip step 2 and step 3, copying data from Access to ListBox directly?
Thanks
回答1:
I found one article. Below is the code and this is the link.
With rs
.MoveLast
NoOfRecords = .RecordCount
.MoveFirst
End With
'Set the number of ListBox columns = number of fields in the recordset
ListBox1.ColumnCount = rs.Fields.Count
'Load the listbox with the retrieved records
ListBox1.Column = rs.GetRows(NoOfRecords)
Thanks
回答2:
For a single column listbox
Try to manually edit the list using a loop:
Me.ListName.Clear 'First clear existing list
With Me.ListName
While rs.EOF = False
.AddItem rs.Fields(0).Value
rs.MoveNext
Wend
End With
Swap that code with steps 2-4.
来源:https://stackoverflow.com/questions/39857659/how-to-load-data-into-excel-userform-listbox-from-access-form-directly