Problem displaying single record in listbox from recordset

carpking

New Member
Joined
Jul 6, 2009
Messages
21
I am trying to populate a 3 column listbox in a userform from SQL Server via ADO. When the result set consists of more than one record, there is no problem and the data is displayed properly (ie each piece of data is in its appropriate column) eg...

StockCode..........QtyReqd.........JobDeliveryDate
test1...................1................. 01/01/1900
test2...................1..................31/12/1900
test3...................3..................18/02/1900

however when the recordset returns only a single record, the data does not transpose and views as below (ie each piece of data in the record is on a different line in the first column

StockCode...........QtyReqd.........JobDeliveryDate
test1
1
01/01/1900

here is my code
Code:
Private Sub ListBox1_Click()
Dim db As String
Dim cnct As String
Dim src As String
Dim conn As ADODB.Connection
Dim rs As Recordset
Dim col As Integer
Dim i As Integer
Dim sSQL As String
Dim rcArray As Variant
'connection string
Set Connection = New ADODB.Connection
cnct = "Provider=SQLOLEDB.1;"
cnct = cnct & "Persist Security Info=False;"
cnct = cnct & "User ID=sa;"
cnct = cnct & "Password=$upR3m3sa;"
cnct = cnct & "Initial Catalog=SysproCompanyA;"
cnct = cnct & "Data Source=asms"
Connection.Open ConnectionString:=cnct
'create recordset
Set Recordset = New ADODB.Recordset
With Recordset
src = "Select Job, QtyReqd, JobDeliveryDate from qryAllocForStockMonitor where StockCode = '" & selectedid & "'"
.Open Source:=src, ActiveConnection:=Connection, CursorType:=adOpenStatic
'Recordset.MoveLast
Count = Recordset.RecordCount
'Open recordset and copy to an array
'Recordset.Open sSQL, conn
rcArray = Recordset.GetRows
'Place data in the listbox
With Me.ListBox2
    .Clear
    .ColumnCount = 3
    .List = Application.Transpose(rcArray)
    .ListIndex = -1
    Font.Size = 12
End With
End With
'Close ADO objects
Recordset.Close
Connection.Close
Set Recordset = Nothing
Set Connection = Nothing
End Sub

I have tried removing the Application.Transpose eg ...
Code:
With Me.ListBox2
    .Clear
    .ColumnCount = 3
    .List = rcArray
    .ListIndex = -1
    Font.Size = 12
End With

However the data still displays incorrectly. Can anyone help?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top