Exporting from Access 2000 to Excel via script

M

McGeeky

Hi. I want to be able to export the results of an Access 2000 query (passing
in filter parameters) to an Excel worksheet automatically.

E.g. a user opens an Access 2000 form, then would enter in a destination
file name for Excel, some filter parameters, then click a button. The export
would happen automatically.

Is it possible to do this?

Many thanks.

McGeeky.
 
A

Allen Browne

Create a query to use for the export.
Modify the SQL property of the QueryDef, so it contains your criteria.
Use TransferSpreadsheet to export the query.

This kind of thing:

Dim strWhere As String
Dim strSql As String
strWhere = " WHERE (City = """ & Me.txtCity & """)"
strSql = "SELECT * FROM Table1 " & strWhere & " ORDER BY [SomeField];"
CurrentDb.QueryDefs("Query1").SQL = strSql
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"Query1", "C:\MyFolder\MyFile.xls", True
 
M

McGeeky

Thanks for that Allen, exactly what I need!!


Allen Browne said:
Create a query to use for the export.
Modify the SQL property of the QueryDef, so it contains your criteria.
Use TransferSpreadsheet to export the query.

This kind of thing:

Dim strWhere As String
Dim strSql As String
strWhere = " WHERE (City = """ & Me.txtCity & """)"
strSql = "SELECT * FROM Table1 " & strWhere & " ORDER BY [SomeField];"
CurrentDb.QueryDefs("Query1").SQL = strSql
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"Query1", "C:\MyFolder\MyFile.xls", True
--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

McGeeky said:
Hi. I want to be able to export the results of an Access 2000 query
(passing in filter parameters) to an Excel worksheet automatically.

E.g. a user opens an Access 2000 form, then would enter in a destination
file name for Excel, some filter parameters, then click a button. The
export would happen automatically.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top