How to load data into Excel Userform Listbox from Access form directly?

半世苍凉 提交于 2019-12-13 22:12:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!