+ Reply to Thread
Results 1 to 13 of 13

Problem displaying single record in userform listbox from recordset

  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
    Please Login or Register  to view this content.
    I have tried removing the Application.Transpose eg ...
    Please Login or Register  to view this content.
    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 Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Problem displaying single record in userform listbox from recordset

    Try using this function instead of Application.Transpose:
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  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 Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    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 Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    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?

  7. #7
    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

    the column widths are set at 100pt;49.95pt;49.95pt in the properties window. havent changed anything in the code except the reference to your function

    Please Login or Register  to view this content.
    its wierd ?!? I appreciate all your help and effort. I will try changing the column widths and font size and see if it makes a difference

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Problem displaying single record in userform listbox from recordset

    Try using:
    Please Login or Register  to view this content.
    so that you can examine in the Locals window exactly what gets put into rcArray.

  9. #9
    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, this is what is happening with the arrays.

    When the recordset is put into rcArray (rcArray = Recordset.Getrows)
    rcArray(0,0) = "test1"
    rcArray(1,0) = "1"
    rcArray(2,0) = "01/01/1900"

    The transposegetrows function then puts the "rcArray" array into vardata
    vardata(0,0) = "test1"
    vardata(1,0) = "1"
    vardata(2,0) = "01/01/1900"

    the function then transposes the array as such
    transposegetrows(1,1) = "test1"
    transposegetrows(1,2) = "1"
    transposegetrows(1,3) = "01/01/1900"

    and such
    varout(1,1) = "test1"
    varout(1,1) = "1"
    varout(1,1) = "01/01/1900"

    could the problem not perhaps be that the array should be ...
    transposegetrows(0,1) = "test1"
    transposegetrows(0,2) = "1"
    transposegetrows(0,3) = "01/01/1900"
    since the original rcArray has values in row = 0 and not row = 1

  10. #10
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Problem displaying single record in userform listbox from recordset

    Quote Originally Posted by carpking View Post
    Hi, this is what is happening with the arrays.

    ...
    and such
    varout(1,1) = "test1"
    varout(1,1) = "1"
    varout(1,1) = "01/01/1900"
    I presume you meant:
    varout(1,1) = "test1"
    varout(1,2) = "1"
    varout(1,3) = "01/01/1900"
    ? The output array is 1 based, so there is no 0 element. It shouldn't be a problem (otherwise you wouldn't get any data from that row). I am still puzzled as to why the 1 doesn't make it into the listbox though, when it is definitely in the array.

  11. #11
    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

    yes you are correct! ... sorry copy and paste mistake.

    Yep its got me stumped. I am going to try and pull through more coloumns of data to see if the second column is the only one that is left out if there are numerous columns, also i will apply it to a select query returning more than 1 result to see if it occurs for all results.

  12. #12
    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 ...ok this is what has happened. I have played with the sql query changing the order of the cols. The other information always appears and the "qtyreqd" data "1" is always not displayed, no matter where in the order of cols it is. Also I repeated the "qtyreqd" col in the sql query and increased the no of cols in total to 5 and ensured that 2 results are returned ... and surprise surprise the "qtyreqd" col is always blank everytime and for every record! The next step was to make sure that there was actually data in the array before and ofter the transpose function ... and i have attached a pic showing the Local window at the point just after transposing the array. Does this help any ?
    Attached Images Attached Images

  13. #13
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Problem displaying single record in userform listbox from recordset

    The only thing I notice is that that column is of type Decimal, which isn't really a native Excel type. If you convert it to Double (either in the SQL or in the code) does it come through OK?

+ 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