Hi Forum,
Could someone point me in the right direction here, I need to understand this once and for all.
I want to run a series of Access queries from Excel.
I have created an Access db with about 12 stored queries which produces what I need, in Access.
The first one takes a command line parameter (a customer number) this is used to return customer data and as the queries are run one the last one produces an table with the customers prices which I need in Excel.
I am using the following for the first query, this passes the customer number, it works fine and I get my data into Excel. It does not however save the query in Access with the changed customer number, which I am guessing is where the problem lies.
I used the same construction to run the last query, thinking that this would run the intermediate queries and pass the results I needed to Excel - it did not work as I expected (but I guess you know that ;-)
What do I need to do to achieve that?
Do I need to explicitly run all the queries from Excel, if so how?
Or do I just need to use a method which saves the first query and then run the last one?
Thanks for your consideration ..... Nick
Here is the Excel Vba.
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Set ShDest = Sheets("Tabelle2")
sSQL = "SELECT dbo_ADRESSEN.KDNR, dbo_ADRESSEN.ADRNR, dbo_ADRESSEN.KURZNAME, dbo_ADRESSEN.VORNAME, dbo_ADRESSEN.NAME, dbo_ADRESSEN.NAME2, dbo_ADRESSEN.NAME3, dbo_ADRESSEN.STRASSE, dbo_ADRESSEN.PLZ, dbo_ADRESSEN.ORT, dbo_ADRESSEN.LAENDERKZ, dbo_ADRKUNDEN.KDGRUPPE, dbo_ADRKUNDEN.PREISKZ, dbo_ADRKUNDEN.VERBAND, dbo_ADRLB.BEZEICHNUNG AS LB, dbo_ADRZB.KURZBEZEICHNUNG AS ZB " & _
"FROM ((dbo_ADRESSEN INNER JOIN dbo_ADRKUNDEN ON dbo_ADRESSEN.ROWADRESSEN=dbo_ADRKUNDEN.ROWADRESSEN) INNER JOIN dbo_ADRLB ON dbo_ADRKUNDEN.LBNR=dbo_ADRLB.LBNUMMER) INNER JOIN dbo_ADRZB ON dbo_ADRKUNDEN.ZBNR=dbo_ADRZB.ZBNUMMER " & _
"WHERE (((dbo_ADRESSEN.KDNR)=" & KdNr & "));"
Set cnn = New ADODB.Connection
'MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
MyConn = "\\STATION6\PasstProReloaded\Excel.mdb"
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open Source:=sSQL, _
ActiveConnection:=cnn, _
CursorType:=adOpenForwardOnly, _
LockType:=adLockOptimistic, _
Options:=adCmdText
'clear existing data on the sheet
ShDest.Activate
'Range("A1").CurrentRegion.Offset(1, 0).ClearContents
Cells.Select
Selection.ClearContents
'create field headers
i = 0
With Range("A1")
For Each fld In rst.Fields
.Offset(0, i).Value = fld.Name
i = i + 1
Next fld
End With
'transfer data to Excel
Range("A2").CopyFromRecordset rst
' Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
-----------------------------------------------------------------------------------------------------------------------------------------------------
This