hello!
First post, so please don't bite!
I'm needing to write some code which sits in an excel file, which pulls data from another excel file using SQL on an ADO connection. (this excel file stays closed)
That, is all ok, and have it working.
The bit that I can't seem to get to work, is that I want the results of the query to be downloaded to a '3rd' excel file (which is open).
When I do put this third sheet as the target address, I get an error saying Run-time error '-2147217904 (80040e10)':
No value given for one or more required parameters.
The Code I'm using is this...
Sub TopLineSQL2()
Dim SQL2 As String
Dim rngTarget2 As Range
SQL2 = Workbooks(wbk).Sheets("+SQL").Range("TOPLINE2SQL")
Set rngTarget2 = Sheets("test").Range("A1") <-- this one works OK
'Set rngTarget2 = Workbooks(sTarget2).Sheets("TopLine2").Range("B3") <-- this one doesn't work
Call RetrieveEXCEL2(SQL2, rngTarget2)
End Sub
Private Sub RetrieveEXCEL2(strSQL2 As String, clTrgt2 As Range)
Dim cnt2 As New ADODB.Connection
Dim rst2 As New ADODB.Recordset
Dim lFields2 As Long
Dim DBPath2 As String
Dim sConnect2 As String
DBPath2 = "c:/test.mdb"
sConnect2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath2 & "; Extended Properties=Excel 8.0;"
cnt2.Open sConnect2
rst2.Open strSQL2, cnt2 <-- this is where I get the error
lFields2 = rst2.Fields.Count
On Error Resume Next
clTrgt2.CopyFromRecordset rst2
If Err.Number <> 0 Then GoTo EarlyExit
EarlyExit:
rst2.Close
cnt2.Close
Set rst2 = Nothing
Set cnt2 = Nothing
On Error GoTo 0
End Sub
So the error certainly seems to relate to the target address. But the odd bit is that at the point I get the error, it's not even looked at the target address.
It's also (probably) worth me noting that if I set the target address as
Set rngTarget2 = workbooks("Book1").Sheets("test").Range("A1")
ie, reference the workbook, but still use the same workbook / destination as the target that works ok, I also get the error.
Can any shed any light on what I'm doing wrong here?
Oh, one other question;
does anyone know how I can get the query to download the column names at the time of running the query? I read that it can't be done with ADO, but does anyone here know otherwise?
Thanks for your help - really appreciate it!
Edit:
Just to add, the value of sTarget2 is "TRADING_CC_CUSTOM.XLS", and this is definitely correct, as it uses this variable to create & name the file I'm trying to download the data to!
Bookmarks