Listbox display issues

gblack

Registered User.
Local time
Today, 14:17
Joined
Sep 18, 2002
Messages
632
I am populating a listbox from a query, whose criteria (i.e. where clause) comes from the OnClick event of a combobox, within Excel.

Everything works fine when there's more than one record in the query's output. But if there's only one record my listbox wraps or stacks the two columns, instead of listing both side by side. Meaning:

If there are two or more records the listbox output:
E01 E01
M05 M0501
M05 M0502...

But with one record output the listbox displays the value so:
E01
E01

How do I change the code below so that this record will display in the lsitbox like:
E01 E01

Here's the code in the OnClick event procedure of the ComboBox:
Code:
Private Sub ComboBox1_Click()
    Dim vArray As Variant

    Dim cn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    With cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Data Source=" & ActiveWorkbook.FullName & ";" & _
        "Extended Properties=""Excel 8.0;HDR=Yes;"""
        .Open
    End With
    
    rst.Open "SELECT DISTINCT TOA, STOREROOM FROM [Sheet3$] WHERE COMMUNITY = " & Chr(34) & Me.ComboBox1.Value & Chr(34) & ";", _
             cn, adOpenStatic
             
    vArray = rst.GetRows
    
    rst.MoveFirst
    i = 0
    With Me.ListBox1
        .Clear
        .ColumnCount = 2
        .ColumnWidths = "100;100"  'adjust the columns widths
        .List = Application.Transpose(vArray)
        .ListIndex = -1
    End With
    
    rst.Close
    cn.Close
    Set rst = Nothing
    Set cn = Nothing

End Sub
 
I changed up how I loaded it to this and it works like I want:

Code:
Private Sub ComboBox1_Click()
    Dim vArray As Variant
    [B]Dim i As Integer, s As String[/B]   
    Dim cn As New ADODB.Connection
    Dim rst As New ADODB.Recordset
    
    [B]ChDir "s:"[/B]    

    With cn
        .Provider = "Microsoft.Jet.OLEDB.4.0"
        .ConnectionString = "Data Source=" & ActiveWorkbook.FullName & ";" & _
        "Extended Properties=""Excel 8.0;HDR=Yes;"""
        .Open
    End With
    
    rst.Open "SELECT DISTINCT TOA, STOREROOM FROM [Sheet3$] WHERE COMMUNITY = " & Chr(34) & Me.ComboBox1.Value & Chr(34) & ";", _
             cn, adOpenStatic
             
 [B] Me.ListBox1.Clear
             
     While Not rst.EOF
        For i = 0 To 1
            s = s & rst.Fields(i).Value & "         "
        Next i
        Me.ListBox1.AddItem Left(s, Len(s) - 1)
        s = ""
        rst.MoveNext

    Wend[/B]    
    rst.Close
    cn.Close
    Set rst = Nothing
    Set cn = Nothing

End Sub
 

Users who are viewing this thread

Back
Top Bottom