Results 1 to 2 of 2

Thread: VB - Fill flexgrid with ADO Recordset

Threaded View

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    VB - Fill flexgrid with ADO Recordset

    Let's say you have an MSFlexgrid1 on your form, and an ADODB.Recordset as well. This code will get the column names, put them on the top-"gray" row, and fill in the rest of the flexgrid with values from the recordset.

    Also, it'll adjust the width of the columns and the flexgrid itself. Modify it as you see fit.

    To use this function, call it and pass your recordset to it.

    e.g. fillflexgrid(objrs)


    VB Code:
    1. Public Function fillflexgrid(objtemp As ADODB.Recordset)
    2.  
    3. Dim i As Integer, j As Integer
    4. Dim z As Integer
    5.  
    6. If Not (objtemp.BOF And objtemp.EOF) Then
    7.     MSFlexGrid1.Cols = objtemp.Fields.Count
    8.     'number of columns is the number of fields
    9.     i = 0
    10.  
    11.         Do While Not (objtemp.EOF)
    12.         objtemp.MoveNext
    13.         i = i + 1
    14.         Loop
    15.         'this was to get number of rows
    16.  
    17.     objtemp.MoveFirst
    18.     'get cursor back to original position
    19.  
    20.     MSFlexGrid1.Rows = i + 1
    21.     'number of rows is i plus 1
    22.  
    23.     z = 1
    24.  
    25.         For j = 0 To objtemp.Fields.Count - 1
    26.         MSFlexGrid1.TextMatrix(0, j) = objtemp.Fields(j).Name
    27.         Next
    28.         'here we're getting the column names which are the field names
    29.  
    30.         Do While Not (objtemp.EOF)
    31.         For j = 0 To objtemp.Fields.Count - 1
    32.             If Not (IsNull(objtemp.Fields(j).Value)) Then
    33.             MSFlexGrid1.TextMatrix(z, j) = objtemp.Fields(j).Value
    34.             Else
    35.             MSFlexGrid1.TextMatrix(z, j) = ""
    36.             End If
    37.  
    38.         Next
    39.  
    40.         z = z + 1
    41.         objtemp.MoveNext
    42.  
    43.         Loop
    44.  
    45.     objtemp.MoveFirst
    46.     'back to the first position...
    47.  
    48.  
    49. 'This part is to adjust the width of the columns so you
    50. 'can see the ENTIRE text!!
    51. With MSFlexGrid1
    52. For intColCounter = 1 To .Cols - 1
    53.                 sngLongestWidth = 0
    54.                 For intCounter = 0 To .Rows - 1
    55.                     If sngLongestWidth < Me.TextWidth(.TextMatrix(intCounter, intColCounter)) Then
    56.                         sngLongestWidth = Me.TextWidth(.TextMatrix(intCounter, intColCounter))
    57.                     End If
    58.                 Next intCounter
    59.                 .ColWidth(intColCounter) = CLng(sngLongestWidth + 200)
    60.                Next intColCounter
    61.  
    62. 'now set the flexgrids width to the total width
    63.  
    64. Dim intwidthcounter As Integer, colcounter As Integer
    65.  
    66.  
    67. intwidthcounter = 0
    68.  
    69. For colcounter = 0 To MSFlexGrid1.Cols - 1
    70. intwidthcounter = intwidthcounter + MSFlexGrid1.ColWidth(colcounter)
    71. Next
    72.  
    73. MSFlexGrid1.Width = intwidthcounter + 120
    74.  
    75.  
    76.  
    77. End With
    78.  
    79. Else
    80. MSFlexGrid1.Clear
    81.  
    82.  
    83.  
    84. End If
    85.  
    86.  
    87. End Function


    PS: I know it doesn't follow proper coding conventions in terms of naming, etc, but hey, it gets the job done
    Last edited by mendhak; Oct 15th, 2003 at 10:54 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width