Microsoft Access Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft Access Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 25th, 2005, 03:45 AM
BBJo BBJo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 21 BBJo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 10 m 15 sec
Reputation Power: 0
Open up New Excel workbook from Access

Hi all
I am trying to open up a new Excel workbook from Access query. Want to open the query up in Excel and give the user the option to save it there if they want to keep the information. I am currently creating a file and saving the query from the database there. Is it possible to execute a query and open up excel with the results of the query?

My code is:
'SQL SELECT all from table
strSql = strSql & strSqlSign

Set qdf = db.CreateQueryDef("tmpExcelOut", strSql)
'Set qdf = db.CreateQueryDef("qryTmpExport", strSql)
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel7, ("tmpExcelOut"), fPath, True
'Delete the object
DoCmd.SetWarnings False
DoCmd.DeleteObject acQuery, "tmpExcelOut"
DoCmd.SetWarnings True

Set qdf = Nothing
Set db = Nothing

Thanks

Reply With Quote
  #2  
Old January 25th, 2005, 10:04 AM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 4
What I do is use a macro. In the macro you can specify the file path or leave it blank and access will prompt the user where to save the Excel file. Then in Auto Start select Yes. In your code above use the DoCmd.RunMacro "NameOfMacro".

This will create your Excel spreadsheet from your query results and then open the Excel file automatically for viewing.

Was this what you were looking for?

lwells

Reply With Quote
  #3  
Old January 26th, 2005, 02:17 AM
BBJo BBJo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 21 BBJo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 10 m 15 sec
Reputation Power: 0
Quote:
Originally Posted by lwells
What I do is use a macro. In the macro you can specify the file path or leave it blank and access will prompt the user where to save the Excel file. Then in Auto Start select Yes. In your code above use the DoCmd.RunMacro "NameOfMacro".

This will create your Excel spreadsheet from your query results and then open the Excel file automatically for viewing.

Was this what you were looking for?

lwells


Yes this is exactly what I want to do.

Few questions:
Is the object type in the macro Query (makes sense since I am exporting a query results)?

Should the action in the macro be OutputTo?

Where should the query parameter be in the DoCmd.RunMacro be? The code is:

strSql = strSql & strSqlSign
Set qdf = db.CreateQueryDef("tmpExcelOut", strSql)
DoCmd.RunMacro "SaveToExcel", qdf

Not sure where I should put the QueryDef

Thanks for the help

Reply With Quote
  #4  
Old January 26th, 2005, 06:36 AM
lwells lwells is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 632 lwells User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 21 h 59 m 38 sec
Reputation Power: 4
Yes,

It would be

OutputTo
Query------------>tmpExcelOut
OutputFormat ---->Microsoft Excel 97-2003 (*.xls)
Output File-------> (optional)Type the file path here or leave blank
AutoStart-------->Yes

I gather you named the macro SaveToExcel which is great.

In your code this is how it would look


strSql = strSql & strSqlSign

Set qdf = db.CreateQueryDef("tmpExcelOut", strSql)

DoCmd.RunMacro "SaveToExcel"

'Delete the object
DoCmd.SetWarnings False
DoCmd.DeleteObject acQuery, "tmpExcelOut"
DoCmd.SetWarnings True

Set qdf = Nothing
Set db = Nothing

This will open an excel workbook based on the temp query you created in your code.

I would recommend that you create your temp query, but don't delete it so you can compile your database...otherwise the object will be missing. Once you compile, then you can delete the query.

lwells

Reply With Quote
  #5  
Old January 26th, 2005, 07:37 AM
BBJo BBJo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 21 BBJo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 10 m 15 sec
Reputation Power: 0
Quote:
Originally Posted by lwells
Yes,

It would be

OutputTo
Query------------>tmpExcelOut
OutputFormat ---->Microsoft Excel 97-2003 (*.xls)
Output File-------> (optional)Type the file path here or leave blank
AutoStart-------->Yes

I gather you named the macro SaveToExcel which is great.

In your code this is how it would look


strSql = strSql & strSqlSign

Set qdf = db.CreateQueryDef("tmpExcelOut", strSql)

DoCmd.RunMacro "SaveToExcel"

'Delete the object
DoCmd.SetWarnings False
DoCmd.DeleteObject acQuery, "tmpExcelOut"
DoCmd.SetWarnings True

Set qdf = Nothing
Set db = Nothing

This will open an excel workbook based on the temp query you created in your code.

I would recommend that you create your temp query, but don't delete it so you can compile your database...otherwise the object will be missing. Once you compile, then you can delete the query.

lwells


Brilliant.
Works like magic, exactly what I wanted thanks again.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft Access Development > Open up New Excel workbook from Access


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway