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
I have tried removing the Application.Transpose eg ...![]()
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
However the data still displays incorrectly. Can anyone help?![]()
With Me.ListBox2 .Clear .ColumnCount = 3 .List = rcArray .ListIndex = -1 Font.Size = 12 End With
this question has also been placed on Mr Excels forum at: http://www.mrexcel.com/forum/showthread.php?t=403014
Bookmarks