MSFlexGrid & Excel Data Source - RESOLVED
I am creating an ADO recordset from an excel spreadsheet. The recordset
may not have the same amount of columns each time. How would I populate
the MSFlexGrid without knowing the exact number of columns? I have
attached my code below. I would like to use a range for the recordset but I
can't find any sample code for that. Also, will I be able to edit the data in
the grid?
Thanks.
VB Code:
Public Function BuildDataSet()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim xlRange As Excel.Range
Dim strEndCell As String
Dim tmpEndCell As String
Dim dbconn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim strSQL As String
Dim intCol As Integer
Dim intRows As Integer
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open(frmLookup.txtLookupPath)
Set xlWS = xlWB.Sheets(g_worksheet)
xlWB.Sheets(g_worksheet).Activate
tmpEndCell = ActiveCell.SpecialCells(xlLastCell).Address
strEndCell = Replace(tmpEndCell, "$", "")
Dim row As Long
Dim column As Long
row = 1
column = 0
dbconn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & frmLookup.txtLookupPath & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
strSQL = "SELECT * FROM [" & g_worksheet & "$]"
rst.Open strSQL, dbconn, adUseClient, adLockOptimistic
MSFlexGrid1.Rows = rst.RecordCount + 1
MSFlexGrid1.Cols = ActiveCell.SpecialCells(xlLastCell).column
While Not rst.EOF
MSFlexGrid1.TextMatrix(row, 0) = rst.AbsolutePosition
MSFlexGrid1.TextMatrix(row, 1) = rst(0)
MSFlexGrid1.TextMatrix(row, 2) = rst(1)
MSFlexGrid1.TextMatrix(row, 3) = rst(2)
MSFlexGrid1.TextMatrix(row, 4) = rst(3)
MSFlexGrid1.TextMatrix(row, 5) = rst(4)
MSFlexGrid1.TextMatrix(row, 6) = rst(5)
row = row + 1
rst.MoveNext
Wend
MSFlexGrid1.ColWidth(0) = 750
MSFlexGrid1.ColWidth(1) = 2500
xlApp.Quit
Set xlWB = Nothing
Set xlWS = Nothing
End Function