Consulting

Results 1 to 2 of 2

Thread: Import recordset to a listbox

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Import recordset to a listbox

    Hi all,
    I have the following code to import from a recordset (Access) to a single column listbox. How do I need to change this to import to a two column listbox or combobox?
    Regards
    MD
    [VBA]Set rst = dbs.OpenRecordset(Sql)
    Do While Not rst.EOF
    Me.ListBox1.AddItem Format(rst("Job No"), "0000") & " - " & rst("Project Title")
    rst.MoveNext
    Loop[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    VBAX Mentor XL-Dennis's Avatar
    Joined
    May 2004
    Location
    ?stersund, Sweden
    Posts
    499
    Location
    Hi

    Hm, why do You use DAO in the first place?

    Below is an example that use ADO and that populate a combobox with data in three columns:

    [vba]
    Option Explicit
    Dim cnt As ADODB.Connection
    Dim rst As ADODB.Recordset
    Const stSQL As String = "SELECT * FROM Shippers;"
    Const stCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\Northwind.mdb;" & _
    "Persist Security Info=False"
    Private Sub UserForm_Initialize()
    Dim vaData As Variant
    Set cnt = New ADODB.Connection
    With cnt
    .Open stCon
    Set rst = .Execute(stSQL)
    End With
    'Populate the variant array with the recordset.
    vaData = rst.GetRows
    With Me.ComboBox1
    'This refer to number of columns
    .ColumnCount = 3
    .Clear
    'Populate the list with the variant arrays's data
    .List = Application.Transpose(vaData)
    .ListIndex = -1
    End With

    Set cnt = Nothing: Set rst = Nothing
    End Sub
    [/vba]

    Kind regards,
    Dennis
    Kind regards,
    Dennis

    ExcelKB | .NET & Excel | 2nd edition PED


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •