Results 1 to 1 of 1

Thread: MSFlexGrid & Excel Data Source - RESOLVED

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2003
    Location
    Georgia
    Posts
    32

    Resolved 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:
    1. Public Function BuildDataSet()
    2.        
    3.     Dim xlApp As Excel.Application
    4.     Dim xlWB As Excel.Workbook
    5.     Dim xlWS As Excel.Worksheet
    6.     Dim xlRange As Excel.Range
    7.     Dim strEndCell As String
    8.     Dim tmpEndCell As String
    9.     Dim dbconn As New ADODB.Connection
    10.     Dim rst As New ADODB.Recordset
    11.     Dim strSQL As String
    12.    
    13.    
    14.     Dim intCol As Integer
    15.     Dim intRows As Integer
    16.    
    17.    
    18.     Set xlApp = New Excel.Application
    19.     Set xlWB = xlApp.Workbooks.Open(frmLookup.txtLookupPath)
    20.     Set xlWS = xlWB.Sheets(g_worksheet)
    21.     xlWB.Sheets(g_worksheet).Activate
    22.     tmpEndCell = ActiveCell.SpecialCells(xlLastCell).Address
    23.     strEndCell = Replace(tmpEndCell, "$", "")
    24.    
    25.     Dim row As Long
    26.     Dim column As Long
    27.     row = 1
    28.     column = 0
    29.    
    30.     dbconn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & frmLookup.txtLookupPath & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
    31.     strSQL = "SELECT * FROM [" & g_worksheet & "$]"
    32.     rst.Open strSQL, dbconn, adUseClient, adLockOptimistic
    33.     MSFlexGrid1.Rows = rst.RecordCount + 1
    34.     MSFlexGrid1.Cols = ActiveCell.SpecialCells(xlLastCell).column
    35.  
    36.     While Not rst.EOF
    37.         MSFlexGrid1.TextMatrix(row, 0) = rst.AbsolutePosition
    38.         MSFlexGrid1.TextMatrix(row, 1) = rst(0)
    39.         MSFlexGrid1.TextMatrix(row, 2) = rst(1)
    40.         MSFlexGrid1.TextMatrix(row, 3) = rst(2)
    41.         MSFlexGrid1.TextMatrix(row, 4) = rst(3)
    42.         MSFlexGrid1.TextMatrix(row, 5) = rst(4)
    43.         MSFlexGrid1.TextMatrix(row, 6) = rst(5)
    44.                
    45.         row = row + 1
    46.         rst.MoveNext
    47.     Wend
    48.    
    49. MSFlexGrid1.ColWidth(0) = 750
    50. MSFlexGrid1.ColWidth(1) = 2500
    51. xlApp.Quit
    52. Set xlWB = Nothing
    53. Set xlWS = Nothing
    54.  
    55.    
    56. End Function
    Last edited by odamsr; May 27th, 2005 at 10:35 AM. Reason: Resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width