Private Sub Form_Load()
Dim db_file As String
Dim statement As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim c As Integer
Dim r As Integer
Dim col_wid() As Single
Dim field_wid As Single
' Получаем данные.
db_file = App.Path
If Right$(db_file, 1) <> "\" Then db_file = db_file & _
"\"
db_file = db_file & "books.mdb"
' Открываем соединение.
Set conn = New ADODB.Connection
conn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db_file & ";" & _
"Persist Security Info=False"
conn.Open
' Выбираем данные.
statement = "SELECT * FROM Books ORDER BY Title"
' Получаем записи.
Set rs = conn.Execute(statement, , adCmdText)
' Фиксируем одну строку и не фиксируем колонки.
MSFlexGrid1.Rows = 2
MSFlexGrid1.FixedRows = 1
MSFlexGrid1.FixedCols = 0
' Отображаем заголовки колонок.
MSFlexGrid1.Rows = 1
MSFlexGrid1.Cols = rs.Fields.Count
ReDim col_wid(0 To rs.Fields.Count - 1)
For c = 0 To rs.Fields.Count - 1
MSFlexGrid1.TextMatrix(0, c) = rs.Fields(c).Name
col_wid(c) = TextWidth(rs.Fields(c).Name)
Next c
' Отображаем значения для каждой строки.
r = 1
Do While Not rs.EOF
MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
For c = 0 To rs.Fields.Count - 1
MSFlexGrid1.TextMatrix(r, c) = _
rs.Fields(c).Value
' Смотрим, насколько велико значение.
field_wid = TextWidth(rs.Fields(c).Value)
If col_wid(c) < field_wid Then col_wid(c) = _
field_wid
Next c
rs.MoveNext
r = r + 1
Loop
' Закрываем записи и соединение.
rs.Close
conn.Close
' Устанавливаем ширину колонок.
For c = 0 To MSFlexGrid1.Cols - 1
MSFlexGrid1.ColWidth(c) = col_wid(c) + 240
Next c
End Sub
|