+ Reply to Thread
Results 1 to 13 of 13

Problem displaying single record in userform listbox from recordset

Hybrid View

carpking Problem displaying single... 07-20-2009, 09:11 AM
romperstomper Re: Problem displaying single... 07-20-2009, 09:33 AM
carpking Re: Problem displaying single... 07-20-2009, 09:54 AM
romperstomper Re: Problem displaying single... 07-20-2009, 10:04 AM
carpking Re: Problem displaying single... 07-20-2009, 10:15 AM
  1. #1
    Registered User
    Join Date
    07-20-2009
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2003
    Posts
    10

    Problem displaying single record in userform listbox from recordset

    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
    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 ...
    With Me.ListBox2
        .Clear
        .ColumnCount = 3
        .List = rcArray
        .ListIndex = -1
        Font.Size = 12
    End With
    However the data still displays incorrectly. Can anyone help?

    this question has also been placed on Mr Excels forum at: http://www.mrexcel.com/forum/showthread.php?t=403014

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: Problem displaying single record in userform listbox from recordset

    Try using this function instead of Application.Transpose:
    Function TransposeGetRows(varData) As Variant
       Dim lngRow As Long, lngCol As Long
       Dim varOut()
       ReDim varOut(1 To UBound(varData, 2) + 1, 1 To UBound(varData, 1) + 1)
       For lngRow = LBound(varData, 2) To UBound(varData, 2)
          For lngCol = LBound(varData, 1) To UBound(varData, 1)
             varOut(lngRow + 1, lngCol + 1) = varData(lngCol, lngRow)
          Next lngCol
       Next lngRow
      TransposeGetRows = varOut
    End Function
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    07-20-2009
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Problem displaying single record in userform listbox from recordset

    Wow, thanks romperstomper for the amazingly quick reply!!! your function has done it, the single record is now displaying horizontally. Just one problem the second piece of data "Qty Reqd" is now missing ... so the display is now

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

    any idea how to fill it in? Thanks again for the help

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: Problem displaying single record in userform listbox from recordset

    I can't replicate that. Have you tried stepping through the function to see what's happening?

  5. #5
    Registered User
    Join Date
    07-20-2009
    Location
    Jhb, South Africa
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Problem displaying single record in userform listbox from recordset

    Hi romper, from what i can see when stepping through the code the following is happening ...

    when lngcol =0 varOut = test1
    when lngcol =1 varOut = 1
    when lngcol =2 varOut = 01/01/1900
    when lngcol =3 varOut = <Subscript out of range>

    ... so it seems that the for loop that increments lngcol is perhaps looping 1 time too many ??? could that be the problem ?

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: Problem displaying single record in userform listbox from recordset

    Shouldn't do otherwise you'd be getting an error at runtime and it wouldn't explain why the 1 is missing. You haven't done anything with the column widths, have you?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1