I have some simple code that will bring up a small MSHFlexgrid in a form of its own. I want the form to have the width of the total of the columns in the grid. I have tried a few ways of doing this, but it will not work. Can someone help me, please?

Thanks,
Reaper

VB Code:
  1. Option Explicit
  2. Public DSN_Name As String
  3. Public Table_Name As String
  4. Public adoConn As ADODB.Connection
  5.  
  6. Private Sub Form_Load()
  7. ' Hide the MSHFlexgrid, connect to the database, run the select query, fill the grid with the values, and unhide the grid
  8.    
  9.     With MSHFlexGrid1
  10.         .Visible = False
  11.         adoData.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=" & DSN_Name & ";"
  12.         adoData.CommandType = adCmdText
  13.         adoData.RecordSource = "SELECT LEFT(RTRIM(docid),8) AS [Batch Number], COUNT(docid) AS [Batch Count(s)] from " & Table_Name & " GROUP BY LEFT(RTRIM(docid),8)"
  14.         adoData.Refresh
  15.         .ColWidth(0) = 0
  16.         .Visible = True
  17.     End With
  18.     Call ChangeColumnWidth
  19.  
  20. End Sub
  21.  
  22. Public Sub ChangeColumnWidth()
  23. ' Sets column width to width of longest text, or column header, in column
  24.     Dim Flex As MSHFlexGrid
  25.     Dim lngMaxLength As Long
  26.     Dim intCounter As Integer
  27.     Dim lngLength As Long
  28.     Dim lngWidth As Long
  29.     Dim lngMinWidth As Long
  30.     Dim i As Integer
  31.     Dim Parent As Form
  32.    
  33.     Set Flex = MSHFlexGrid1
  34.     Set Parent = Flex.Parent
  35.  
  36.     Flex.Redraw = False
  37.     Flex.FocusRect = flexFocusNone
  38.    
  39.     ' This For...Loop will check to see if the text of the column header has the largest width _
  40.     ' and set the width of the column to whichever is largest
  41.     For i = 1 To Flex.Cols - 1
  42.         Flex.Row = 0
  43.         Flex.Col = i
  44.         If (i <> Flex.Cols) Then
  45.             intCounter = Flex.TopRow
  46.             With Parent
  47.                 .Font = Flex.FontFixed
  48.                 .FontSize = Flex.CellFontSize
  49.                 .FontBold = Flex.CellFontBold
  50.                 .FontItalic = Flex.CellFontItalic
  51.             End With
  52.            
  53.             ' Get minsize of column from column header
  54.             lngMinWidth = Parent.TextWidth(Flex.TextMatrix(0, i))
  55.             lngMaxLength = 0
  56.             intCounter = 0
  57.            
  58.             ' Check the width of each row
  59.             While intCounter < Flex.Rows - 1
  60.                 Flex.Row = intCounter
  61.                 With Parent
  62.                     .Font = Flex.CellFontName
  63.                     .FontSize = Flex.CellFontSize
  64.                     .FontBold = Flex.CellFontBold
  65.                     .FontItalic = Flex.CellFontItalic
  66.                 End With
  67.                
  68.                 lngLength = Parent.TextWidth(Flex.TextMatrix(intCounter, i))
  69.                
  70.                 ' Compare the column header width with the row width
  71.                 If lngMaxLength < lngLength Then
  72.                     lngMaxLength = lngLength
  73.                 End If
  74.                 intCounter = intCounter + 1
  75.             Wend
  76.  
  77.             lngWidth = lngMaxLength
  78.             If lngWidth < lngMinWidth Then
  79.                 lngWidth = lngMinWidth
  80.             End If
  81.            
  82.             ' Set the column width of each column, but, if the column is longer than 3500 twips _
  83.             ' divide the column width by 4 to fit the width of the MSHFlexgrid
  84.             If lngWidth >= 3500 Then
  85.                 Flex.ColWidth(i) = ((lngWidth / 4) + 100)
  86.             Else
  87.                 Flex.ColWidth(i) = lngWidth + 100
  88.             End If
  89.         End If
  90.     Next i
  91.    
  92.     Flex.FocusRect = flexFocusLight
  93.     Flex.Redraw = True
  94.  
  95. End Sub