Private Sub cmdExecute_Click()
Dim query As String
Dim i As Integer
Dim db_name As String
Dim connect_string As String
Dim rs As ADODB.Recordset
Dim r As Integer
Dim c As Integer
Dim num_cols As Integer
Dim col_wid() As Single
Dim new_wid As Single
' Get the list of fields selected.
For i = 0 To lstFields.ListCount - 1
If lstFields.Selected(i) Then
query = query & lstFields.List(i) & ", "
End If
Next i
' Remove the final comma.
query = Left$(query, Len(query) - 2)
' Build the query.
query = "SELECT " & query & " FROM Books"
' Get the database name.
db_name = App.Path
If Right$(db_name, 1) <> "\" Then db_name = db_name & _
"\"
db_name = db_name & "books.mdb"
' Build the connect string.
connect_string = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db_name & ";" & _
"Persist Security Info=False"
' Open the recordset.
Set rs = New ADODB.Recordset
rs.Open query, connect_string, _
adOpenForwardOnly, adLockReadOnly, _
adCmdText
' Hide the grid.
flxResults.Visible = False
DoEvents
' Display the results.
If rs.EOF Then
flxResults.Rows = 1
flxResults.Cols = 1
flxResults.TextMatrix(0, 0) = "No Records Selected"
num_cols = 1
ReDim col_wid(0 To 0)
col_wid(0) = TextWidth(flxResults.TextMatrix(0, 0))
Else
' Make room for column widths.
num_cols = rs.Fields.Count
ReDim col_wid(0 To num_cols - 1)
' Set column headers.
flxResults.Rows = 2
flxResults.Cols = num_cols
flxResults.FixedCols = 0
flxResults.FixedRows = 1
For c = 0 To num_cols - 1
flxResults.TextMatrix(0, c) = rs.Fields(c).Name
' See if we need to enlarge the column.
new_wid = TextWidth(rs.Fields(c).Name)
If col_wid(c) < new_wid Then col_wid(c) = _
new_wid
Next c
' Display the data.
Do Until rs.EOF
r = r + 1
flxResults.Rows = r + 1
For c = 0 To rs.Fields.Count - 1
flxResults.TextMatrix(r, c) = _
rs.Fields(c).Value
' See if we need to enlarge the column.
new_wid = TextWidth(rs.Fields(c).Value)
If col_wid(c) < new_wid Then col_wid(c) = _
new_wid
Next c
rs.MoveNext
Loop
End If
' Set the grid's column widths.
For c = 0 To num_cols - 1
flxResults.ColWidth(c) = col_wid(c) + 120
Next
' Display the grid.
flxResults.Visible = True
rs.Close
End Sub
|