JapanFreak
Registered User.
- Local time
- Today, 16:56
- Joined
- Aug 25, 2007
- Messages
- 45
Hi there,
actually I have already asked the following question in an Excel forum (www.excelforum.com) before, but I only got the advice to try to ask this question in this MS Access-related forum. Thus, here it is: I have a problem with regards to retrieving data from an MS Access 2000 database by using ADO in Excel VBA.
If I try my Excel code with very simple SQL codes, the code actually works and delivers the desired results on my Excel sheet starting from Range A1. Hence I assume that there is no problem with my ADO request as such.
However, I have problems (run-time error ‘-2147467259 (80004005)’ because of problems with the open method for object recordset) when I use (not much) more complex SQL requests with sub-queries. I developed those queries in MS Access and they work properly there, i.e. I assume that there is no problem with my SQL code as such either.
Are there any known limitations with regards to the complexity of SQL statements via ADO? At least my google search does not deliver any results... Are there any other mistakes in my code?
This is my code with a very simple SQL-request, that actually works in both Access and Excel:
However, the same VBA code with an only a bit more complex SQL request with a sub-query only produces an error, i.e. the query does work in Access but the Excel request via ADO fails:
What is the reason? Are there any limitations of ADO objects which I do not take into account?
I'd be happy for any comments / help regarding this issue...
Best regards,
JapanFreak
actually I have already asked the following question in an Excel forum (www.excelforum.com) before, but I only got the advice to try to ask this question in this MS Access-related forum. Thus, here it is: I have a problem with regards to retrieving data from an MS Access 2000 database by using ADO in Excel VBA.
If I try my Excel code with very simple SQL codes, the code actually works and delivers the desired results on my Excel sheet starting from Range A1. Hence I assume that there is no problem with my ADO request as such.
However, I have problems (run-time error ‘-2147467259 (80004005)’ because of problems with the open method for object recordset) when I use (not much) more complex SQL requests with sub-queries. I developed those queries in MS Access and they work properly there, i.e. I assume that there is no problem with my SQL code as such either.
Are there any known limitations with regards to the complexity of SQL statements via ADO? At least my google search does not deliver any results... Are there any other mistakes in my code?
This is my code with a very simple SQL-request, that actually works in both Access and Excel:
Code:
Sub ADORequest()
Dim iCounter As Integer
Dim PathAccessFile As String
Dim ReqDate As Date
Dim strSQL As String
Dim TargetRange As Range
Dim CN As ADODB.Connection
Dim rs As ADODB.Recordset
Worksheets("Data").Cells.ClearContents
PathAccessFile = ThisWorkbook.path & "\TEST.mdb"
strSQL = "SELECT [tblTransactions].[Ticker], sum([tblTransactions].[Qty]) AS TotQty" & _
" FROM tblTransactions" & _
" WHERE [tblTransactions].[Date] <= #3/10/2007#" & _
" GROUP BY [tblTransactions].[Ticker]" & _
" HAVING Sum(tblTransactions.Qty) > 0" & _
" ORDER BY [tblTransactions].[Ticker]"
Set TargetRange = Range("a1")
Set CN = New ADODB.Connection
With CN
.cursorlocation = adUseClient
.Mode = adModeShareDenyNone
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" & PathAccessFile
.Open
End With
Set rs = New ADODB.Recordset
With rs
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
.Open Source:=strSQL, _
ActiveConnection:=CN, _
CursorType:=adOpenStatic, _
LockType:=adLockReadOnly, _
Options:=adCmdText
For iCounter = 0 To rs.Fields.Count - 1
TargetRange.Offset(0, iCounter).Value = rs.Fields(iCounter).Name
Next iCounter
TargetRange.Offset(1, 0).CopyFromRecordset rs
End With
rs.Close
CN.Close
Set CN = Nothing
Set rs = Nothing
Set TargetRange = Nothing
End Sub
However, the same VBA code with an only a bit more complex SQL request with a sub-query only produces an error, i.e. the query does work in Access but the Excel request via ADO fails:
Code:
…
strSQL = "SELECT T.Ticker, S.Name, X.Close" & _
" FROM tblTransactions AS T, tblTradedSecurities AS S," & _
" [SELECT Q.qTicker, Q.qCl AS Close" & _
" FROM tblQuotes AS Q" & _
" WHERE Q.qDate=#3/9/07#]. AS X" & _
" WHERE (((T.Ticker) = [S].[Ticker]) And ((S.Ticker) = [Q].[qTicker]))" & _
" GROUP BY T.Ticker, S.Name, X.Close"
…
What is the reason? Are there any limitations of ADO objects which I do not take into account?
I'd be happy for any comments / help regarding this issue...
Best regards,
JapanFreak