Hello All,
I am relatively new to the boards, although I have been lurking around gleaning insights for some time. I have searched for this topic quit a bit and found a few threads on it. Those healped in designing my current workbook, but I've hit a specific snag that is confusing me.
Let me first explain my purpose:
I am simply trying to have some code that would allow me to run relatively simple SQL Queries on Excel Data. This data however will be in a table format within Excel and I will only be needing to query on table at a time; meaning that I don't need relational database features or SQL Joins at all. At most, the SQL Statements will involve WHERE and SORT statements. So, my method is relatively simple:
1. Select an Excel Range (The table being Queried)
2. Provide an SQL Statement
3. Provide an Output range to print the results
After my recent research, I concluded that ODBC/ADODB was the best route. I am very new to ODBC Connections though. Basically, I just set up an ODBC Connection and DataSource using the Control Panel/Administrative Tools. After that, following some templates of others I constructed the following code:
The Connection String I got from the DataSource when I set up the ODBC Connection through the control panel.
Now, I got this working on my Laptop (running Excel 2007, however the workbook is an Excel 2002 Document). It was working incredibly beautiful. Exactly as I hoped for.
However, when I moved the Workbook over to my Work PC, it freaked out. I am getting this error: System Error &H8000FFFF (-2147418113). Catastrophic Failure. Now of course, I reset the Connection String and made a new ODBC Data Source file for the Work PC. But I must have messed something up somewhere. It should also be noted that the workbook is located on a drive which I access through a network connection, not sure if that matters.
My thoughts are that I either do not have the right Drivers, or my Connection wasn't set up right, or My Connection String in the code is in error.
Any thoughts? Please let me know if I can provide anything to make my question clearer.
I am relatively new to the boards, although I have been lurking around gleaning insights for some time. I have searched for this topic quit a bit and found a few threads on it. Those healped in designing my current workbook, but I've hit a specific snag that is confusing me.
Let me first explain my purpose:
I am simply trying to have some code that would allow me to run relatively simple SQL Queries on Excel Data. This data however will be in a table format within Excel and I will only be needing to query on table at a time; meaning that I don't need relational database features or SQL Joins at all. At most, the SQL Statements will involve WHERE and SORT statements. So, my method is relatively simple:
1. Select an Excel Range (The table being Queried)
2. Provide an SQL Statement
3. Provide an Output range to print the results
After my recent research, I concluded that ODBC/ADODB was the best route. I am very new to ODBC Connections though. Basically, I just set up an ODBC Connection and DataSource using the Control Panel/Administrative Tools. After that, following some templates of others I constructed the following code:
Sub getData(SQL As String, outRange As Range)
Dim conn As Variant
Dim rs As Variant
Dim cs As String
Dim outCell As Range
Set outCell = outRange.Range("a1")
Dim row As Integer
Dim col As Integer
Set conn = CreateObject("adodb.connection")
Set rs = CreateObject("adodb.recordset")
cs = "DRIVER=Microsoft Excel Driver (*.xls);UID = admin;UserCommitSync = Yes;Threads = 3;SafeTransactions = 0;ReadOnly = 1;PageTimeout = 5;MaxScanRows = 8;MaxBufferSize = 2048;FIL=excel 8.0;DriverId = 790;DefaultDir=F:\2008\Cell Phone\Call Details;DBQ=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name
conn.Open cs
rs.Open SQL, conn
row = 0
For col = 0 To rs.Fields.Count - 1
outCell.Offset(row, col).Value = rs.Fields(col).Name
Next
row = row + 1
Do Until rs.EOF
For col = 0 To rs.Fields.Count - 1
outCell.Offset(row, col).Value = rs.Fields(col).Value
Next
row = row + 1
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
End Sub
The Connection String I got from the DataSource when I set up the ODBC Connection through the control panel.
Now, I got this working on my Laptop (running Excel 2007, however the workbook is an Excel 2002 Document). It was working incredibly beautiful. Exactly as I hoped for.
However, when I moved the Workbook over to my Work PC, it freaked out. I am getting this error: System Error &H8000FFFF (-2147418113). Catastrophic Failure. Now of course, I reset the Connection String and made a new ODBC Data Source file for the Work PC. But I must have messed something up somewhere. It should also be noted that the workbook is located on a drive which I access through a network connection, not sure if that matters.
My thoughts are that I either do not have the right Drivers, or my Connection wasn't set up right, or My Connection String in the code is in error.
Any thoughts? Please let me know if I can provide anything to make my question clearer.