View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Sean Connolly[_3_] Sean Connolly[_3_] is offline
external usenet poster
 
Posts: 25
Default Exporting Access Queries to sepcific ranges in an Excel workbook

Hi,

Since you want to display the query results in specific ranges in Excel, why
don't you import the query results into Excel, rather than exporting from
Access. You can use Excel QueryTable objects to achieve this. Try the
following as an example...

Sub CreateQueryTables()
Dim strCnn As String, strCmdTxt As String
' Set up your connection string.
' Can be either ODBC or OLEDB (if you have the relevant provider/s)
' My personal preference is OLEDB (both seem to work equally as well
against Access).
strCnn = "OLEDB;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program
Files\Microsoft Office\OFFICE11\SAMPLES\Northwind.mdb;Persist Security
Info=True"
' strCnn = "ODBC;DBQ=C:\Program Files\Microsoft
Office\OFFICE11\SAMPLES\Northwind.mdb;" & _
"Driver={Microsoft Access Driver (*.mdb)};DriverId=25;FIL=MS
Access;PageTimeout=15"
' strCnn = strCnn & ";Password=<pwd;User ID=<userID" ' If a password
and userID are required.
' Enter your own specific user and environment info as necessary
strCmdTxt = Empty
strCmdTxt = "SELECT [Current Product List].* FROM [Current Product List]"
' Create the QueryTable on the ActiveSheet at the range stated.
' Replace "A1" as desired.
With ActiveSheet.QueryTables.Add(Connection:=strCnn,
Destination:=Range("A1"))
If .QueryType = xlOLEDBQuery Then .CommandType = xlCmdDefault
.CommandText = strCmdTxt
.RefreshStyle = xlOverwriteCells
.HasAutoFormat = False
.RefreshOnFileOpen = False
.Refresh
End With
End Sub

Note: The above assumes that you have the MS Access Northwind.mdb sample
database installed. Ensure that the full path to this database for your
installation is shown correctly in the connection string.

(I'm using Office Pro 2K3 on WinXP Pro).

HTH and let me know how you get on.

Cheers, Sean.

"MWACardsfan" wrote:

I have some queries in access which need to be exported into specific
reanges in an excel workbook. Apparently,you cannot use the
Transferspreadsheet function to accomplich this. Can someone help me on
my way with some code that would accomplish this?

Thanks much.