Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Autosizing Msflexgrid ?

Status
Not open for further replies.

elquixiote

Technical User
Nov 30, 2002
99
MX
Hi! I developed the following code to load an ado recordset into a msflexgrid control.

It works pretty fine, but the autosizing options does not
works completelly well. It fails when the column contains
letters like M's and I's or W's or dates.

Is there an alternative way to do column autosizing of
Msflexgrid control ?


Public Sub adoLoadGrid(MSFxgrid As MSFlexGrid, _
r_set As ADODB.Recordset, _
mode As String, _
fmtString As String, _
Optional fieldFmts As String = "", _
Optional Autosize As Boolean = True, _
Optional StdCharWidth As Integer = 135)
Dim i As Long
Dim theFmtstring

theFmtstring = fmtString
ReDim fieldfmt(r_set.Fields.Count) As String
ReDim Maxfieldlengths(r_set.Fields.Count) As Integer


For i = 0 To r_set.Fields.Count - 1
fieldfmt(i) = ExtractParameter(",", i + 1, fieldFmts)
Maxfieldlengths(i) = 0
Next i


If UCase(mode) = "NEW" Then
MSFxgrid.Rows = 1
If theFmtstring = "" Then
For i = 0 To r_set.Fields.Count - 1
theFmtstring = theFmtstring & String(Len(r_set.Fields(i).Name) + 2, "X") & IIf(i < r_set.Fields.Count - 1, "|", "")
Next i
MSFxgrid.FormatString = theFmtstring

If MSFxgrid.Cols < r_set.Fields.Count Then
MSFxgrid.Cols = r_set.Fields.Count
End If
For i = 0 To r_set.Fields.Count - 1
MSFxgrid.TextMatrix(0, i) = r_set.Fields(i).Name
Next i
Else
MSFxgrid.FormatString = theFmtstring
End If
End If

On Error GoTo errHandler
While Not r_set.EOF
MSFxgrid.Rows = MSFxgrid.Rows + 1
For i = 0 To r_set.Fields.Count - 1
MSFxgrid.TextMatrix(MSFxgrid.Rows - 1, i) = IIf(IsNull(r_set.Fields(i).Value), _
"", _
IIf(fieldfmt(i) = "", _
r_set.Fields(i).Value, _
Format(r_set.Fields(i).Value, fieldfmt(i)) _
) _
)
If Len(MSFxgrid.TextMatrix(MSFxgrid.Rows - 1, i)) > Maxfieldlengths(i) Then
Maxfieldlengths(i) = Len(MSFxgrid.TextMatrix(MSFxgrid.Rows - 1, i))
End If
Next i
r_set.MoveNext
Wend
If Autosize Then
For i = 0 To r_set.Fields.Count - 1
If Len(MSFxgrid.TextMatrix(0, i)) > Maxfieldlengths(i) Then
MSFxgrid.ColWidth(i) = Len(MSFxgrid.TextMatrix(0, i)) * StdCharWidth
Else
MSFxgrid.ColWidth(i) = Maxfieldlengths(i) * StdCharWidth
End If
Next i
End If
On Error GoTo 0
Exit Sub
errHandler:
Select Case Err.Number
Case 30006
MsgBox "La información del período seleccionado excede la capcidad de manejo" & vbLf & _
"=> Información está incompleta, Intente hacer un análisis fraccionado de la información", vbExclamation
Case Else
MsgBox "Error no esperado " & Err.Number & " " & Err.Description, vbCritical, ActiveForm.Caption
End
End Select
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top