JumboCactuar
Well-known Member
- Joined
- Nov 16, 2016
- Messages
- 788
- Office Version
- 365
- Platform
- Windows
Hi,
i found this great code which allows you to query the current workbook, and also closed workbooks. Allows you to SQL query which i am finding a better way of "advanced filtering" data in the same workbook. Are there any limitations to this?
Also if using ADO on an external workbook (closed or open) , does it open it read only? Or would it show as in use if another user opened the workbook whilst you run this code
Source is here if anyone wants to try out the example files provided there: https://analysistabs.com/excel-vba/ado-sql-macros-connecting-database/
i found this great code which allows you to query the current workbook, and also closed workbooks. Allows you to SQL query which i am finding a better way of "advanced filtering" data in the same workbook. Are there any limitations to this?
Also if using ADO on an external workbook (closed or open) , does it open it read only? Or would it show as in use if another user opened the workbook whilst you run this code
Code:
'Add reference for Microsoft Activex Data Objects Library
Sub sbADO()
Dim sSQLQry As String
Dim ReturnArray
Dim Conn As New ADODB.Connection
Dim mrs As New ADODB.Recordset
Dim DBPath As String, sconnect As String
DBPath = ThisWorkbook.FullName
'You can provide the full path of your external file as shown below
'DBPath ="C:\InputData.xlsx"
'Using MSDASQL Provider
'sconnect = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"
'Using Microsoft.Jet.OLEDB Provider - If you get an issue with Jet OLEDN Provider try MSDASQL Provider (above statement)
sconnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath _
& ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
Conn.Open sconnect
sSQLSting = "SELECT * From [DataSheet$] WHERE Sales >3000" ' Your SQL Statement (Table Name= Sheet Name=[DataSheet$])
mrs.Open sSQLSting, Conn
'=>Load the Data into an array
'ReturnArray = mrs.GetRows
''OR''
'=>Paste the data into a sheet
ActiveSheet.Range("A2").CopyFromRecordset mrs
'Close Recordset
mrs.Close
'Close Connection
Conn.Close
End Sub
Source is here if anyone wants to try out the example files provided there: https://analysistabs.com/excel-vba/ado-sql-macros-connecting-database/
Last edited: