Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Selecting rows from Excel
Message
 
To
23/11/2000 16:26:40
Christian Cote
Les Logiciels Onoma Inc.
Longueuil, Quebec, Canada
General information
Forum:
Visual Basic
Category:
Database DAO/RDO/ODBC/ADO
Miscellaneous
Thread ID:
00445068
Message ID:
00445085
Views:
20
>HI,
>I have a spreadsheet that contains data. I need to import these data into an ADO Recordset in order to manipulate them. I use a DSN-LESS connection such as :
>
>oConn.Open "Driver={Microsoft Excel Driver (*.xls)};" & _
> "DriverId=790;" & _
> "Dbq=" & Txtpath.Text & ";"
>
>The connection seems to be ok but when I try to retreive some rows form the spreadsheet :
>
>oRs.Open "SELECT * FROM" & txpath.text, oConn, adOpenStatic, adLockReadOnly, adCmdText
>
>I get an error that tells me that it cannot find the database or datasource.
>
>How can I retreive the rows form my spreadsheet ?
>
>Thank you,
>Christian

I got this code about a month ago and I never give it a try. Maybe you'll find something in it:
A sample program.. that shows ALL Worksheets in an Excel File bound to DataGrids on a Form. 

----------------------------------------------------------- 
#1) Start a New Standard.Exe Project. 
#2) Add a reference to "Microsoft ActiveX Data Objects 2.x Library". 
#3) Add a "Microsoft DataGrid Control to Form1 and SET IT'S INDEX PROPERTY TO 0. 
#4) Copy/Paste the following into the Form1 code window. 
#5) CHANGE the strExcel variable to an Excel Spreadsheet on your system. 
#6) Press F5 to run. All Worksheets in the Excel file will appear in separate DataGrids on Form1. 


Option Explicit 

Dim m_strWorkSheet() As String 
Dim m_rsWorkSheets() As ADODB.Recordset 

Private Sub Form_Load() 

'  Intitialize 
   ReDim m_strWorkSheet(0) 
   ReDim m_rsWorkSheets(0) 
    
'  Load Spreadsheet 
   Dim strExcel As String: strExcel = "c:\MyPath\MyExcel.XLS" ' <-- CHANGE ME 
   If Not x_Load_Excel(strExcel) Then 
      MsgBox "Excel SpreadSheet Load Error " & strExcel 
      Exit Sub 
   End If 
    
'  Screen Formatting 
   DataGrid1(0).Visible = False 
   Form1.Move _ 
      Screen.Width * 0.1, _ 
      Screen.Height * 0.1, _ 
      Screen.Width * 0.8, _ 
      Screen.Height * 0.8 
    
   Dim lngIndex As Long 
   Dim sngLeft As Single 
   Dim sngTop As Single 
   Dim sngWidth As Single 
   Dim sngHeight As Single 
   lngIndex = UBound(m_strWorkSheet) + 1 
   If lngIndex = 0 Then 
      sngWidth = Form1.ScaleWidth / 1 
   Else 
      sngWidth = Form1.ScaleWidth / 2 
   End If 
   If lngIndex Mod 2 = 0 Then 
      sngHeight = Form1.ScaleHeight / (lngIndex \ 2) 
   Else 
      sngHeight = Form1.ScaleHeight / ((lngIndex \ 2) + 1) 
   End If 
    
   For lngIndex = LBound(m_strWorkSheet) To UBound(m_strWorkSheet) 
      If lngIndex > 0 Then 
         Load DataGrid1(lngIndex) 
      End If 
      With DataGrid1(lngIndex) 
         .Caption = m_strWorkSheet(lngIndex) 
         Set .DataSource = m_rsWorkSheets(lngIndex) 
         .Visible = True 
         .Move sngLeft, sngTop, sngWidth, sngHeight 
         If (lngIndex + 1) Mod 2 = 0 Then 
            sngLeft = 0 
            sngTop = sngTop + sngHeight 
         Else 
            sngLeft = sngWidth 
         End If 
      End With 
   Next lngIndex 

End Sub 

Public Function x_Load_Excel(ByVal i_strFilePath As String) As Boolean 
      
   If Dir(i_strFilePath) = "" Then 
      MsgBox "FILE " & LCase(i_strFilePath) & " NOT FOUND!!!" 
      Exit Function 
   End If 
    
   Dim cn As ADODB.Connection: Set cn = New ADODB.Connection 
   With cn 
      .Provider = "MSDASQL.1" 
      .ConnectionString = "Extended Properties=""DSN=Excel Files;DBQ=" & Trim(i_strFilePath) & ";""" 
      .Open 
   End With 
    
   Dim lngIndex As Long 
   Dim rsWorkBooks As ADODB.Recordset: Set rsWorkBooks = New ADODB.Recordset 
   Dim rsWorkSheets As ADODB.Recordset: Set rsWorkSheets = New ADODB.Recordset 
   Set rsWorkBooks = cn.OpenSchema(adSchemaTables) 
   With rsWorkBooks 
      Do Until .EOF 
         If m_strWorkSheet(lngIndex) <> "" Then 
            lngIndex = UBound(m_strWorkSheet) + 1 
            ReDim Preserve m_strWorkSheet(lngIndex) 
            ReDim Preserve m_rsWorkSheets(lngIndex) 
         End If 
         m_strWorkSheet(lngIndex) = rsWorkBooks.Fields(2) 
         Set m_rsWorkSheets(lngIndex) = New ADODB.Recordset 
         With m_rsWorkSheets(lngIndex) 
            .ActiveConnection = cn 
            .CursorLocation = adUseClient 
            .Source = "SELECT * FROM " & "[" & m_strWorkSheet(lngIndex) & "] " 
            .Open 
            Set .ActiveConnection = Nothing ' DISCONNECT 
         End With 
         .MoveNext 
      Loop 
      .Close 
   End With 
   cn.Close 
    
   Set rsWorkBooks = Nothing 
   Set rsWorkSheets = Nothing 
   Set cn = Nothing 

   x_Load_Excel = True 

End Function 
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform