473,512 Members | 15,089 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export Query to Excel File

52 New Member
I seem to be over my head and can use some help...

I have pieced together some code from several internet searched that is supposed to export a query from access to an existing excel file. I also wanted it to clear any existing data on the excel sheet before the query is exported. It seems to work sometimes, other times when the excel file opens it has a white screen and the toolbars are frozen, but if I close and reopen it all the data is there. The question I have is,

1. Which part of the code could be causing to problem with the screen
2. Does the code need more fine tuning and if so what is recommended
3. Is there a better way to do the export to an existing file and be sure all the contents of the sheet are overwritten

Thank You for any feed back you can offer

Expand|Select|Wrap|Line Numbers
  1. Private Sub Export_Submittal_to_Excel_Click()
  2.  
  3.     DoCmd.Hourglass True
  4.  
  5.     Dim xlApp As Excel.Application
  6.     Dim wkb As Excel.Workbook
  7.     Dim wks As Excel.Worksheet
  8.     Dim strQryName As String, strXLFile As String
  9.  
  10.     strDB = CurrentDb.Name
  11.     strCurrentDir = Left(strDB, Len(strDB) - Len(Dir(strDB)))
  12.  
  13.     strXLFile = "" & strCurrentDir & "Submit Road Rewards.xls"
  14.     strQryName = "Submit Road Rewards" 'Query Name
  15.  
  16.     Set xlApp = CreateObject("Excel.Application")
  17.     Set wkb = xlApp.Workbooks.Open(strXLFile)
  18.  
  19.     xlApp.Visible = False
  20.     xlApp.DisplayAlerts = False
  21.     xlApp.Application.ScreenUpdating = True
  22.  
  23.     wkb.Worksheets("Submit_Road_Rewards").Cells.ClearContents 'Delete
  24.     wkb.Worksheets("Template-Run Macro").Select
  25.  
  26.     wkb.Save
  27.     wkb.Close
  28.     xlApp.Quit
  29.  
  30.     Set xlApp = Nothing
  31.     Set wkb = Nothing
  32.     Set wks = Nothing
  33.  
  34.     DoCmd.Hourglass False
  35.     DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strQryName, strXLFile, True
  36.     If MsgBox("Do you want to view the file?", vbYesNo) = vbYes Then
  37.     FollowHyperlink strXLFile
  38.  
  39.     End If
  40.  
  41. End Sub
  42.  
  43.  
Jun 12 '10 #1
17 7477
MMcCarthy
14,534 Recognized Expert Moderator MVP
There doesn't appear to be any obvious problems with your code. I suspect the problem lies with the RESUME.XLW file. When you use Office automation to create a new instance of Excel it is done with the creation of a workspace, hence the RESUME.XLW file.

If for any reason your code gets interrupted or your file doesn't properly save, this workspace will retain all the information from the previous attempt. Then when you try to run it again it gets stuck. A check you can do is to run task manager and look at the process list. If you quit all open excel files on your desktop first, then there shouldn't be any Excel.exe processes running. If there is then they are instances that didn't properly close.

One thing I've found to be very useful is to use error handling to force the xlapp.quit code to run even if the code is interrupted.

Expand|Select|Wrap|Line Numbers
  1. Private Sub AnyEvent()
  2. On Error GoTo Err_AnyEvent
  3.  
  4.     'Your code here
  5.  
  6. Exit_AnyEvent:
  7.  
  8.     ' from your code move these statements to the exit point
  9.     ' not sure about the workbook save statement, you may want to leave that outside of this point
  10.     wkb.Save 
  11.     wkb.Close 
  12.     xlApp.Quit 
  13.  
  14.     Set xlApp = Nothing 
  15.     Set wkb = Nothing 
  16.     Set wks = Nothing 
  17.  
  18. Err_AnyEvent:
  19.  
  20.      MsgBox Err.Number & " - " & Err.Description & " in AnyEvent"
  21.      Resume Exit_AnyEvent
  22.  
  23. End Sub
Jun 12 '10 #2
ahmedtharwat19
55 New Member
With the permission of my brothers Experts

you can use this code

TO Un-View
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OutputTo acOutputQuery, _
  2. "yourQuery",acFormatXLS, "yrquey.XLS", 0
TO View
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OutputTo acOutputQuery, _
  2. "yourQuery",acFormatXLS, "yrquey.XLS", -1
(Medo)
Jun 12 '10 #3
timleonard
52 New Member
@msquared
Thanks for the replys. I tried the On Error GoTo Err_AnyEvent. I am not sure but I think there must be an error somewhere because after the code runs and it follows the link to open the XLS file, it still at times displays the white screen. I did check the task manager and there is no other instance of Excel shown. Also once the XLS file is closed and the code should exit out it displays a error message of "91 - Object variable or With block variable not set in Anyevent" This message does not respond to the "ok" button and the only way to close it is the end task from the task manager.
Jun 12 '10 #4
MMcCarthy
14,534 Recognized Expert Moderator MVP
@timleonard
Hi Tim,

I was only using AnyEvent as a sample not specific to your code.

Your code should look like this ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Export_Submittal_to_Excel_Click() 
  2. On Error GoTo Err_Export_Submittal_to_Excel_Click
  3.  
  4.     DoCmd.Hourglass True 
  5.  
  6.     Dim xlApp As Excel.Application 
  7.     Dim wkb As Excel.Workbook 
  8.     Dim wks As Excel.Worksheet 
  9.     Dim strQryName As String, strXLFile As String 
  10.  
  11.     strDB = CurrentDb.Name 
  12.     strCurrentDir = Left(strDB, Len(strDB) - Len(Dir(strDB))) 
  13.  
  14.     strXLFile = "" & strCurrentDir & "Submit Road Rewards.xls" 
  15.     strQryName = "Submit Road Rewards" 'Query Name 
  16.  
  17.     Set xlApp = CreateObject("Excel.Application") 
  18.     Set wkb = xlApp.Workbooks.Open(strXLFile) 
  19.  
  20.     xlApp.Visible = False 
  21.     xlApp.DisplayAlerts = False 
  22.     xlApp.Application.ScreenUpdating = True 
  23.  
  24.     wkb.Worksheets("Submit_Road_Rewards").Cells.ClearContents 'Delete 
  25.     wkb.Worksheets("Template-Run Macro").Select 
  26.  
  27. Exit_Export_Submittal_to_Excel_Click:
  28.  
  29.     wkb.Save 
  30.     wkb.Close 
  31.     xlApp.Quit 
  32.  
  33.     Set xlApp = Nothing 
  34.     Set wkb = Nothing 
  35.     Set wks = Nothing 
  36.  
  37.     DoCmd.Hourglass False 
  38.     DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strQryName, strXLFile, True 
  39.  
  40.     If MsgBox("Do you want to view the file?", vbYesNo) = vbYes Then 
  41.         FollowHyperlink strXLFile 
  42.  
  43.     End If 
  44.  
  45. Err_Export_Submittal_to_Excel_Click:
  46.  
  47.      MsgBox Err.Number & " - " & Err.Description & " in Export_Submittal_to_Excel_Click" 
  48.      Resume Exit_Export_Submittal_to_Excel_Click
  49.  
  50. End Sub 
I would also comment out the line

xlApp.DisplayAlerts = False

That way you can see what errors may be occuring in Excel. You can always put it back later when your code is fixed.
Jun 12 '10 #5
timleonard
52 New Member
@ahmedtharwat19
Thanks for the reply, but I am under the impression that the DoCmd.OutputTo command will overewrite the file. I need to output to an existing XLS file as the data is then processed further from the file.
Jun 12 '10 #6
timleonard
52 New Member
@msquared
Ok i tried the modification and it still gives the same error "91 - Object variable or With block variable not set in Export_Submittal_to_Excel_Click" it also gives a "0 - in Export_Submittal_to_Excel_Click" and a "20 - Resume without an error in Export_Submittal_to_Excel_Click" The only way to get out of the error loop is through the task manager.

BTW- I added the switch NewWindow:=True to the line "FollowHyperlink strXLFile, NewWindow:=True" and this seems to stop the white screen issue.
Jun 12 '10 #7
MMcCarthy
14,534 Recognized Expert Moderator MVP
Did you comment out the line
Expand|Select|Wrap|Line Numbers
  1. xlApp.DisplayAlerts = False
and if so what excel alert messages did you see?

Also I'm not sure what this line of code is doing?
Expand|Select|Wrap|Line Numbers
  1. wkb.Worksheets("Template-Run Macro").Select
Jun 12 '10 #8
timleonard
52 New Member
@msquared
Yes I did comment out the xlApp.DisplayAlerts = False and it does not give any errors related to the openning and closing of the excel file. The errors come after the followhyperlink command. In regards to the wkb.Worksheets("Template-Run Macro").Select...I have a macro on that worksheet so that when the file is opened it is already on the required worksheet.
Jun 12 '10 #9
MMcCarthy
14,534 Recognized Expert Moderator MVP
The issue may simply be a timing one. Add in a DoEvents command to make sure all code executes fully before the transfer spreadsheet statement runs.
Expand|Select|Wrap|Line Numbers
  1.     DoEvents
  2.  
  3.     DoCmd.Hourglass False 
  4.     DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, strQryName, strXLFile, True 
Let me know if this makes any difference.
Jun 12 '10 #10
timleonard
52 New Member
@msquared
No the DoEvents did not help...But I did put an Exit Sub after the If MsgBox and that seems to stop the code at the right spot and prevent the errors stated above from happening.

Expand|Select|Wrap|Line Numbers
  1. If MsgBox("Do you want to view the file?", vbYesNo) = vbYes Then  
  2.         FollowHyperlink strXLFile  
  3.  
  4.     End If  
  5.     Exit Sub
  6.  
Jun 13 '10 #11
MMcCarthy
14,534 Recognized Expert Moderator MVP
@timleonard
Weird ............. lol

I've never tried to program a follow hyperlink command after populating a spreadsheet but someone else may have done so and will have some ideas. At least its not causing a problem any more.

I'd love to know why this works though :)
Jun 13 '10 #12
timleonard
52 New Member
@msquared
I'd love to learn more as to why myself...

Thank you though for sticking with me to work the problem out
Jun 13 '10 #13
NeoPa
32,557 Recognized Expert Moderator MVP
Without the Exit Sub, the process follows the code following and into the error-handling code. I would guess this is not required so there should be code that handles contolling the process after the execution of the hyperlink call (Not necessarily the hyperlinked process(es)). An Exit Sub is an example of something that would work.

It appears that the original code finished after that line anyway, so the modified code should unless the error handling code is required, which is very doubtful.
Jun 14 '10 #14
MMcCarthy
14,534 Recognized Expert Moderator MVP
@NeoPa
NeoPa much as I love you, could you say that again in English please lol!
Jun 14 '10 #15
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
In the code you posted (msquared) you forgot to add a Exit Sub before the error handling. Therefore the error handling code will be called each time the sub is called, regardless of whether or not an error has occurred. If the Err has not been cleared, you might even end up seing an old error message.

I think the DoEvents actually solved the problem, but since the error handling code was executed without there actully being an error, it seemed to not solve it. With the addition of the Exit Sub you now no longer proceed into the error handling.

Hope that made sense :P
Jun 14 '10 #16
MMcCarthy
14,534 Recognized Expert Moderator MVP
@TheSmileyOne
lol much clearer, now why can't NeoPa just say Mary you made a mistake and forgot the Exit Sub before the error code.
Jun 14 '10 #17
NeoPa
32,557 Recognized Expert Moderator MVP
msquared: I'd love to know why this works though :)
I'm always a bit literal Mary. I wasn't trying to answer what was wrong with your code, but specifically why the fix worked. Luckily Smiley popped up and gave a more sensible answer anyway :)
Jun 14 '10 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

6
13100
by: Robin Cushman | last post by:
Hi all, I need some help -- I'm working with an A2K database, using DAO, and am trying to read records into a Crystal Report and then export it to a folder on our network as an Excel...
1
2147
by: David | last post by:
(dhl)Using 2K. I have a query I want to export to excel. There are 35,000+ lines for this query. I created a macro to run on a schedule to export the excel file to a network shared drive. I can...
1
2001
by: sanju | last post by:
hi guyz .. can anyone send sample code to export a excel file into sql server table using web interface . ie programatically read and select some columns of excel and insert into a table. please...
5
1186
by: sanju | last post by:
hi guyz .. can anyone have an idea to export a excel file into sql server table using web interface . please give me an idea on this thank u very much san auck
0
1751
by: Server Control | last post by:
Hi, I am exporting my datagrid to Excel using the standard Export to Excel Method using : dg.RenderControl(). and Page.Response.AddHeader("Content-Disposition", "attachment;filename=" +...
5
2164
by: cassey14 | last post by:
Hi! Is it possible that the result in the subform will export to excel file?? Please help..
0
1548
by: johnlim20088 | last post by:
Hi, Can someone help me on this? What can I done on code? I able to export csv file in http://mypage.com, but when i change to https://mypage.com, it unable to export, error "cannot display...
4
2247
by: sethson22 | last post by:
Hello , How to import and export the excel file into Access through VB Code let me know , if anybody knows the same
2
3971
by: sbettadpur | last post by:
hello, i want to export reports into excel file, i got some class files which will export into excel file. Now i need to change the font size, font should be in bold. how i can do that one.
0
1392
by: Ivan Stefanov | last post by:
Hello, I have the following problem: I created a website project and I want in that website to be make a button, which will export some data in an excel file. This is the code that I have: using...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.