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:
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