Option Explicit

Sub ClearGrid (ctlGrid As Grid)
' Clears the query grid, erasing any query currently displayed.  To clear
' the grid we use RemoveItem on all indexes 1 and greater.  Don't remove
' index 0 as it is the fixed title row.  Because we can't remove the last
' row I instead clear out the last row's (Row = 1) column text.
'
  
    Dim iRow As Integer, iCol As Integer

    For iRow = (ctlGrid.Rows - 1) To 2 Step -1
        ctlGrid.RemoveItem iRow
    Next iRow
    
    ctlGrid.Row = 1
    
    For iCol = 0 To ctlGrid.Cols - 1
        ctlGrid.Col = iCol
        ctlGrid.Text = ""
    Next iCol

    ctlGrid.Col = 1
End Sub

Sub FitColumn (ctlGrid As Grid, iCol As Integer)
' Fits the columns of the grid to the data within.  Does so by iterating
' through each cell in a column, saving the largest text width and setting
' the width to it.  We also add a some space for clipping.
'

    Dim iRow As Integer, twipsBiggest As Integer
    Dim strTemp As String, iLead As Integer
    Dim iRowOld As Integer
    Dim iColOld As Integer
    Dim MAX_COL_WIDTH As Integer

    iRowOld = ctlGrid.Row
    iColOld = ctlGrid.Col

    iLead = 90                                  ' 1/16" = 90 * 1/(72*20)
    MAX_COL_WIDTH = (ctlGrid.Width / 2) - iLead

    ctlGrid.Col = iCol                          ' Set Next Column
    twipsBiggest = 1                            ' Reset For Each Column

    For iRow = 0 To (ctlGrid.Rows - 1)          ' For Every Row In Column...
        ctlGrid.Row = iRow                      '   Set Next Row
        strTemp = ctlGrid.Text                  '   Get Cell Text

        twipsBiggest = Max(frmMain.TextWidth(strTemp), twipsBiggest)
        If twipsBiggest >= MAX_COL_WIDTH Then
            twipsBiggest = MAX_COL_WIDTH
            Exit For
        End If
    Next iRow

    ctlGrid.ColWidth(iCol) = twipsBiggest + iLead
    ctlGrid.Row = iRowOld
    ctlGrid.Col = iColOld
End Sub

Sub FitRow (ctlGrid As Grid, iRow As Integer)
' Fits the row of a grid to the data within.  Does so by iterating
' through each cell in the row and, using the column's width, making the row
' height enough to fit the text.
'

    Dim iCol As Integer, twipsBiggest As Integer
    Dim iTextWidth As Integer
    Dim strTemp As String, iLead As Integer
    Dim iRowOld As Integer
    Dim iColOld As Integer
    Dim dRows As Double
    Dim iMaxColWidth As Integer
    Dim iRowHeight As Integer

    iRowOld = ctlGrid.Row
    iColOld = ctlGrid.Col

    iLead = 30                                      ' 1/16" = 90 * 1/(72*20)
    iRowHeight = frmMain.TextHeight("QMW")          ' Height of one row
    twipsBiggest = iRowHeight + iLead               ' Reset For Each Column

    ctlGrid.Row = iRow                          ' Set Next Column

    For iCol = 0 To (ctlGrid.Cols - 1)          ' For Every Row In Column...
        ctlGrid.Col = iCol                      '   Set Next Row
        strTemp = ctlGrid.Text                  '   Get Cell Text
        iMaxColWidth = ctlGrid.ColWidth(iCol) - 90
        
        If iMaxColWidth > frmMain.TextWidth("WXYZ") Then
            dRows = frmMain.TextWidth(strTemp) / iMaxColWidth
            
            If (1 > dRows) Or (Fix(dRows) < dRows) Then
                dRows = Fix(dRows) + 1
            Else
                dRows = Fix(dRows)
            End If
    
            twipsBiggest = Max((dRows * iRowHeight) + iLead, twipsBiggest)
        End If
    Next iCol

    ctlGrid.RowHeight(iRow) = twipsBiggest + iLead

    ctlGrid.Row = iRowOld
    ctlGrid.Col = iColOld
End Sub

Function Max (X, Y)
    If X < Y Then
        Max = Y
    Else
        Max = X
    End If
End Function

Sub SetupColumns (ctlGrid As Grid)
' Handles displaying the column headings for the query grid by setting the
' grid width to the number of fields and placing the field names into each
' column header.
'

    Const TOTAL_COLS = 4

    Dim iCol As Integer
    ReDim strColNames(1 To TOTAL_COLS) As String
  
    strColNames(1) = "Name"
    strColNames(2) = "Value"
    strColNames(3) = "Default Value"
    strColNames(4) = "Prompt"

    ctlGrid.Cols = TOTAL_COLS + 1
    ctlGrid.Row = 0
  
    ctlGrid.ColWidth(0) = (ctlGrid.ColWidth(0) / 2)

    For iCol = 1 To TOTAL_COLS
        ctlGrid.Col = iCol
        ctlGrid.Text = strColNames(iCol)
        FitColumn ctlGrid, iCol
    Next iCol
End Sub

