• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

information from an array to a ListBox2

Ana Luna

New Member
Dear all,
As you can see in the code below, I execute a SQL connection and retrieve some rows into the array named aDataasisc(), which consists of several rows with 8 columns. After that, I want to display the rows in ListBox2, but only from column number one to column number 7 (excluding column number 8). The MsgBox named output shows the information of aDataasisc() well-formed with the 7 columns, but in ListBox2, only 6 columns are displayed.

I have been trying to figure it out all day but with no result. Could you help me, please?
Thank you so much, your help is much appreciated.
Regards,

The output Msgbox:
1697654816453.png

The listbox2:
1697654918667.png


The code is:

>>> use code - tags <<<
Code:
' Create a recordset
Dim rs As Object
Set rs = CreateObject("ADODB.Recordset")

' Execute the command and retrieve the results
Set rs = cmd.Execute

If Not rs.EOF Then  
       ' Clear the current content of ListBox2 on the "detail" form   
       detail.ListBox2.Clear      
       ' Get the number of columns directly from the recordset   
       Dim numColsasisc As Long   
       numColsasisc = rs.Fields.Count      

      ' Create an array to store the data   
       Dim aDataasisc() As Variant       
       Dim numRowsasisc As Long   
       numRowsasisc = 0 ' Row counter      

      ' Count the number of rows   
      Do While Not rs.EOF       
            numRowsasisc = numRowsasisc + 1      
            rs.MoveNext   
      Loop       

      ' Resize the array to match the number of rows and columns   
       ReDim aDataasisc(1 To numRowsasisc, 1 To numColsasisc - 1) ' Exclude the last column       

      ' Return to the first record   
       rs.MoveFirst      
 
      ' Populate the array with data from the recordset (excluding the last column)  
       Dim fila As Long    fila = 1   
       Do While Not rs.EOF       
            For col = 1 To numColsasisc - 1 ' Exclude the last column           
                      aDataasisc(fila, col) = rs.Fields(col - 1).Value       
           Next col           

          fila = fila + 1       
          rs.MoveNext   
       Loop       

     ' Ensure that ListBox2 has the same number of columns as the array   
     detail.ListBox2.ColumnCount = numColsasisc - 1 ' Exclude the last column              

     ' Add a MsgBox to print aDataasisc  
      Dim output As String 
 
      For fila = 1 To numRowsasisc       
            For col = 1 To numColsasisc - 1 ' Exclude the last column           
                   output = output & aDataasisc(fila, col) & vbTab      
           Next col       
           output = output & vbNewLine  
     Next fila       

    ' Assign the array to ListBox2   
    detail.ListBox2.List = aDataasisc      

    ' Display the output in a MsgBox   
      MsgBox output
End If
 
Last edited by a moderator:
That's a very long-winded method of populating the listbox. Have you set the Columnwidths property of the listbox anywhere?
 
Back
Top