473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Autostore Password for Export from Access query to Excel MULTIPLE Sheets.

Hi Everyone,

I'm using a code to export a query from Access to create a new Excel with 1 Sheet and Protect with password, and everything's right.

Expand|Select|Wrap|Line Numbers
  1. Private Sub botton_Click()
  2. On Error GoTo Err_botton_Click
  3.  
  4.  Dim excelapp As New Excel.Application
  5.  Dim excelfile As New Excel.Workbook
  6.  Dim excelsheet As New Excel.Worksheet
  7.  
  8. DoCmd.TransferSpreadsheet acExport, , "query1", "C:\PED.xlsx", False
  9.  
  10.  Set excelfile = excelapp.Workbooks.Open("C:\PED.xlsx")
  11.  
  12. Set excelsheet = excelfile.Worksheets.ITEM(1)
  13. excelsheet.Protect Password:="secret"
  14.  
  15. excelfile.Save
  16.  
  17. excelapp.ActiveWorkbook.Close True, "C:\PED.xlsx"
  18. excelapp.Quit
  19.  
  20. Set excelsheet = Nothing
  21. Set excelfile = Nothing
  22. Set excelapp = Nothing
  23.  
  24. Exit_botton_Click:
  25.     Exit Sub
  26.  
  27. Err_botton_Click:
  28.     MsgBox Err.Number & " - " & Err.Description
  29.     Resume Exit_botton_Click
  30. End Sub
My reference it was that post:

http://bytes.com/topic/access/answer...el-spreadsheet

But I got Error 1004 when I try to use the Code to Protect a Excel with 3 Sheets, Could You help me please?

Expand|Select|Wrap|Line Numbers
  1. Private Sub botton_Click()
  2. On Error GoTo Err_botton_Click
  3.  
  4.  Dim excelapp As New Excel.Application
  5.  Dim excelfile As New Excel.Workbook
  6.  Dim excelsheet As New Excel.Worksheet
  7.  
  8. DoCmd.TransferSpreadsheet acExport, , "query1", "C:\PED.xlsx", False
  9. DoCmd.TransferSpreadsheet acExport, , "query2", "C:\PED.xlsx", False
  10. DoCmd.TransferSpreadsheet acExport, , "query3", "C:\PED.xlsx", False
  11.  
  12.  Set excelfile = excelapp.Workbooks.Open("C:\PED.xlsx")
  13.  
  14. Set excelsheet = excelfile.Worksheets.ITEM(1)
  15. excelsheet.Protect Password:="secret"   'ERROR 1004 in this line
  16. Set excelsheet = excelfile.Worksheets.ITEM(2)
  17. excelsheet.Protect Password:="secret"
  18. Set excelsheet = excelfile.Worksheets.ITEM(3)
  19. excelsheet.Protect Password:="secret"
  20.  
  21. excelfile.Save
  22.  
  23. excelapp.ActiveWorkbook.Close True, "C:\PED.xlsx"
  24. excelapp.Quit
  25.  
  26. Set excelsheet = Nothing
  27. Set excelfile = Nothing
  28. Set excelapp = Nothing
  29.  
  30. Exit_botton_Click:
  31.     Exit Sub
  32.  
  33. Err_botton_Click:
  34.     MsgBox Err.Number & " - " & Err.Description
  35.     Resume Exit_botton_Click
  36. End Sub
Thanks for all and best wishes from Brazil.
Sep 20 '13 #1

✓ answered by zmbd

Try the following:
Expand|Select|Wrap|Line Numbers
  1. Private Sub botton_Click() 
  2.     Dim excelapp As New Excel.Application
  3.     Dim excelfile As New Excel.Workbook
  4.     Dim excelsheet As New Excel.Worksheet
  5.     Dim PathToFile As String
  6.     On Error GoTo Err_botton_Click
  7.     '
  8.     'set the path
  9.     PathToFile = CurrentProject.Path & "\ped.xlsx"
  10.     '
  11.     'Transfer the data to a workbook
  12.     DoCmd.TransferSpreadsheet acExport, , "query1", PathToFile, False
  13.     DoCmd.TransferSpreadsheet acExport, , "query2", PathToFile, False
  14.     DoCmd.TransferSpreadsheet acExport, , "query3", PathToFile, False
  15.     '
  16.     'open the new workbook for modificaiton, if this fails then raise an errot
  17.     Set excelfile = excelapp.Workbooks.Open(PathToFile)
  18.     If excelfile Is Nothing Then Err.Raise vbObjectError + 513
  19.     '
  20.     'setup for protecting specfic worksheets within the workbook
  21.     Set excelsheet = excelfile.Worksheets.Item(1)
  22.     '
  23.     'with mutiple worksheets, lets make sure we're dealing with only one worksheet.
  24.     excelsheet.Select
  25.     'and protect it
  26.     excelsheet.Protect "secret"
  27.     'do the next worksheet
  28.     Set excelsheet = excelfile.Worksheets.Item(2)
  29.     excelsheet.Protect Password:="secret"
  30.     'and repeat
  31.     Set excelsheet = excelfile.Worksheets.Item(3)
  32.     excelsheet.Protect Password:="secret"
  33.     '
  34.     'Save the workbook
  35.     excelfile.Save
  36.     '
  37.     'close the workbook and prepare for cleanup
  38.     excelapp.ActiveWorkbook.Close True, PathToFile
  39.     '
  40. Exit_botton_Click:
  41.     '
  42.     'clean up any open references and free resouces.
  43.     If Not excelsheet Is Nothing Then Set excelsheet = Nothing
  44.     If Not excelfile Is Nothing Then Set excelfile = Nothing
  45.     excelapp.Quit
  46.     If Not excelapp Is Nothing Then Set excelapp = Nothing
  47. Exit Sub
  48. '
  49. 'Error trap.
  50. Err_botton_Click:
  51.     MsgBox Err.Number & " - " & Err.Description
  52.     Resume Exit_botton_Click
  53. End Sub
Please take note of the following:
Line 18: Best Practice to check that the external object is set
Line 24: If more than one worksheet is grouped, then the protect method will fail; thus, make sure only one worksheet is selected. This took a while to find as the error is somewhat generic and it takes a few trys to determine which object is tossing the error.
Line 40: Starting here is, IMHO, the best way to shut down your code... both with a normal execution and also in the event of an error. This order of commands should ensure that anything that has been set is cleared and that the excel application is freed from memory. Failure to do this can tie up system resources, cause data corruption, and is simply like feeding the Grimlins after mid-night.

BOL
-z

13 2424
zmbd
5,501 Expert Mod 4TB
--> Please provide BOTH the NUMBER and the EXACT TEXT of the error message.

--> Please provide the version of Office/Access you are using.

Depending on the version of office AND the EXACT NUMBER and EXACT text (please do not paraphrase or alter the text in any way) of the error message you received:
VBA-RT-Error-1004 can refer to:
- vba security violation at object
- error of method
- error in writing to read-only file or object


etc...
Sep 20 '13 #2
Here We go:

1. runtime error 1004 application defined or object defined

2. WIN7 and Office/Ms Access 2010

Thank You Zmbd for your attention.
Sep 21 '13 #3
ADezii
8,834 Expert 8TB
May be a Version problem, since the following Code ran flawlessly:
Expand|Select|Wrap|Line Numbers
  1. Dim excelapp As New Excel.Application
  2. Dim excelfile As New Excel.Workbook
  3. Dim excelsheet As New Excel.Worksheet
  4.  
  5. DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "query1", "C:\PED.xls", False
  6. DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "query2", "C:\PED.xls", False
  7. DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "query3", "C:\PED.xls", False
  8.  
  9. Set excelfile = excelapp.Workbooks.Open("C:\PED.xls")
  10.  
  11. Set excelsheet = excelfile.Worksheets.Item(1)
  12.   excelsheet.Protect Password:="secret"
  13.  
  14. Set excelsheet = excelfile.Worksheets.Item(2)
  15.   excelsheet.Protect Password:="secret"
  16.  
  17. Set excelsheet = excelfile.Worksheets.Item(3)
  18.   excelsheet.Protect Password:="secret"
  19.  
  20. excelfile.Save
  21.  
  22. excelapp.ActiveWorkbook.Close True, "C:\PED.xls"
  23. excelapp.Quit
  24.  
  25. Set excelsheet = Nothing
  26. Set excelfile = Nothing
  27. Set excelapp = Nothing
P.S. - I am referring to Code Lines 5-7.
Sep 21 '13 #4
zmbd
5,501 Expert Mod 4TB
W/ADezii reply leads me towards one of two possiblities:
1) The c-drive root isn't available for read/write/execute
2) Excel has the VBA security set to inhibit automation.

I'm leaning towards the first as Win7 has a few new security features built-in... especially if this is a domain-group or enterprise/network connected PC. Networked PCs often have it set so that "normal" users can NOT normally write/modify directly to the C-Drive root "C:\", a lot of malware does this very thing. I know that when our company pushed the OS to Win7 a lot of my error routines started failing because I used to send these to a textfile at the local pc c-drive root level (long story; however, I could remote access the networked PC c-drive root at one point in time). When Win7 was installed, the error traps started to fail because they couldn't open the file for "write." Now I just have them emailed to the help desk - and they have no idea what to do with the email so they bounce it to my boss who then sends it to me (^-^)

So, the first step is to simply change the path to either a network or local location where you have read/write permisions... I suggest you use the application's current path. Line9 original code in Post#8 referenced in OP

The next thing I would do would be to set a check after line 12 in OP/second code block where you are setting the object
I would insert at line 13
Expand|Select|Wrap|Line Numbers
  1.  if excelfile is nothing then
  2.     MsgBox prompt:="Creation of Excel Object Failed - Contact Support", Buttons:=vbCritical + vbOKOnly, Title:="Critical Error"
  3.     Err.Raise vbObjectError + 513
  4. End If
This should trap if the object hasn't been created for some reason.

Please let us know what happens from here.
Sep 21 '13 #5
ADezii
8,834 Expert 8TB
@zmbd:
The first possibility, as you have stated, can effectively be eliminated since, as quoted by the OP:
I'm using a code to export a query from Access to create a new Excel with 1 Sheet and Protect with password, and everything's right.
Sep 21 '13 #6
Zmbd,

Thank You once again for the reply.

I tried to use the code, but the error is the same one.


@zmbd
Sep 23 '13 #7
And I tried to change the local (Raiz) "C:\PED.xls" for "C:\TEST\PED.xls" and the error it was the same.

TKS
Sep 23 '13 #8
zmbd
5,501 Expert Mod 4TB
@Malungo1970
Well so much for the OS easy fix.
Did you insert the code I gave you in my last post?
We need to make sure that the object is being created and opened properly first.
-IF, for some reason, like you have huge query, the file may not have finished at the OS level; thus, it is not yet available for write.
I Still suspect that there is something happening with the Win7-OS that is causing the issue, the code I provided should help to point that out.
Sep 23 '13 #9
Zmdb,

Yes I inserted your code after line 12.

Thank You.
Sep 23 '13 #10
zmbd
5,501 Expert Mod 4TB
So, must I assume that the code I asked you to insert didn't pop-up an "-2147220991(80040201) automation error" in that code and the VBA-Editor took you to the debug state?
Sep 23 '13 #11
zmbd
5,501 Expert Mod 4TB
Try the following:
Expand|Select|Wrap|Line Numbers
  1. Private Sub botton_Click() 
  2.     Dim excelapp As New Excel.Application
  3.     Dim excelfile As New Excel.Workbook
  4.     Dim excelsheet As New Excel.Worksheet
  5.     Dim PathToFile As String
  6.     On Error GoTo Err_botton_Click
  7.     '
  8.     'set the path
  9.     PathToFile = CurrentProject.Path & "\ped.xlsx"
  10.     '
  11.     'Transfer the data to a workbook
  12.     DoCmd.TransferSpreadsheet acExport, , "query1", PathToFile, False
  13.     DoCmd.TransferSpreadsheet acExport, , "query2", PathToFile, False
  14.     DoCmd.TransferSpreadsheet acExport, , "query3", PathToFile, False
  15.     '
  16.     'open the new workbook for modificaiton, if this fails then raise an errot
  17.     Set excelfile = excelapp.Workbooks.Open(PathToFile)
  18.     If excelfile Is Nothing Then Err.Raise vbObjectError + 513
  19.     '
  20.     'setup for protecting specfic worksheets within the workbook
  21.     Set excelsheet = excelfile.Worksheets.Item(1)
  22.     '
  23.     'with mutiple worksheets, lets make sure we're dealing with only one worksheet.
  24.     excelsheet.Select
  25.     'and protect it
  26.     excelsheet.Protect "secret"
  27.     'do the next worksheet
  28.     Set excelsheet = excelfile.Worksheets.Item(2)
  29.     excelsheet.Protect Password:="secret"
  30.     'and repeat
  31.     Set excelsheet = excelfile.Worksheets.Item(3)
  32.     excelsheet.Protect Password:="secret"
  33.     '
  34.     'Save the workbook
  35.     excelfile.Save
  36.     '
  37.     'close the workbook and prepare for cleanup
  38.     excelapp.ActiveWorkbook.Close True, PathToFile
  39.     '
  40. Exit_botton_Click:
  41.     '
  42.     'clean up any open references and free resouces.
  43.     If Not excelsheet Is Nothing Then Set excelsheet = Nothing
  44.     If Not excelfile Is Nothing Then Set excelfile = Nothing
  45.     excelapp.Quit
  46.     If Not excelapp Is Nothing Then Set excelapp = Nothing
  47. Exit Sub
  48. '
  49. 'Error trap.
  50. Err_botton_Click:
  51.     MsgBox Err.Number & " - " & Err.Description
  52.     Resume Exit_botton_Click
  53. End Sub
Please take note of the following:
Line 18: Best Practice to check that the external object is set
Line 24: If more than one worksheet is grouped, then the protect method will fail; thus, make sure only one worksheet is selected. This took a while to find as the error is somewhat generic and it takes a few trys to determine which object is tossing the error.
Line 40: Starting here is, IMHO, the best way to shut down your code... both with a normal execution and also in the event of an error. This order of commands should ensure that anything that has been set is cleared and that the excel application is freed from memory. Failure to do this can tie up system resources, cause data corruption, and is simply like feeding the Grimlins after mid-night.

BOL
-z
Sep 23 '13 #12
Zmbd Thank You very Much,

Now the code is working.
The Point is
Expand|Select|Wrap|Line Numbers
  1. excelsheet.Select
I use "excelsheet.Select" in the lines 15, 18 and 21

Expand|Select|Wrap|Line Numbers
  1. Private Sub botton_Click()
  2. On Error GoTo Err_botton_Click
  3.  
  4. Dim excelapp As New Excel.Application
  5. Dim excelfile As New Excel.Workbook
  6. Dim excelsheet As New Excel.Worksheet
  7.  
  8. DoCmd.TransferSpreadsheet acExport, , "query1", "C:\PED.xlsx", False
  9. DoCmd.TransferSpreadsheet acExport, , "query2", "C:\PED.xlsx", False
  10. DoCmd.TransferSpreadsheet acExport, , "query3", "C:\PED.xlsx", False
  11.  
  12. Set excelfile = excelapp.Workbooks.Open("C:\PED.xlsx")
  13.  
  14. Set excelsheet = excelfile.Worksheets.ITEM(1)
  15. excelsheet.Select
  16. excelsheet.Protect Password:="secret"   
  17. Set excelsheet = excelfile.Worksheets.ITEM(2)
  18. excelsheet.Select
  19. excelsheet.Protect Password:="secret"
  20. Set excelsheet = excelfile.Worksheets.ITEM(3)
  21. excelsheet.Select
  22. excelsheet.Protect Password:="secret"
  23.  
  24. excelfile.Save
  25.  
  26. excelapp.ActiveWorkbook.Close True, "C:\PED.xlsx"
  27. excelapp.Quit
  28.  
  29. Set excelsheet = Nothing
  30. Set excelfile = Nothing
  31. Set excelapp = Nothing
  32.  
  33. Exit_botton_Click:
  34. Exit Sub
  35.  
  36. Err_botton_Click:
  37. MsgBox Err.Number & " - " & Err.Description
  38. Resume Exit_botton_Click
  39. End Sub
  40.  
Best wishes from Brazil.
Sep 24 '13 #13
zmbd
5,501 Expert Mod 4TB
Yes, that was the final key once I was sure that there were no object creation/access issues which have for me in the past been the source.

You should not need the additional worksheet select statements unless you are going to modify the range values within the worksheet selected.

The first one at line 15 is enough to ensure that the worksheets are not grouped.

Shouldn't hurt anything, just adds unneeded cycles to the runtime execution.

Also, the way your code is written, versis what I posted, fails to release your resources in the event of an error. Might I suggest that you use the code I posted instead OR at least co-op the last section that handles the clean-up?!

Brazil... maybe the company will send me there one year... we have joint ventures... doubt it though... but we can dream of the beaches and the warm water.

off to bed with visions of sugar-plums in my head... been a very long day at my end of the world.

(Z_Z)
Sep 24 '13 #14

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

Similar topics

1
by: Carl Corcoran | last post by:
I'm developing a website for work that exports some reports to excel. I am using the... Response.AddHeader "Content-Disposition","attachment;filename=report.xls" Response.ContentType =...
9
by: M O J O | last post by:
Hi, (I'm new to XML) I can export data from Access as an XML file (with schema integrated in the XML file). I can read it into a DotNet DataSet, but the schema is not correct that is - I do...
2
by: Matt | last post by:
Greetings, I have an application in which I update a sql database from access append query via linked tables. I use an ODBC connection with sql authentication. I would like to hard code the sa...
8
by: Jordi | last post by:
Hello, can anyone tell what is the best way to access to a excel spread sheet from c++? I would be interested in read it and write it. Thank you in advance Jordi
1
by: Pk | last post by:
Hi, I have an data in multiple excel worksheet which i wan to upload into sql server through asp code. Please help me how can i use server.createobject("excel.application") help me its...
37
by: Chris Gilpin | last post by:
Hey everybody! This is my first post here, so hopefully I will be able to make it as clear as possible. I have a very large database (over 1,000,000 entries) and my current goal is to find out the...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
1
by: cut123 | last post by:
I have a query that I would like to export to different Excel documents based on the product. For each type of product I want all pertinent data to export to a different Excel document. There will...
2
by: ezra | last post by:
I have an access report based on a bunch of queries that does a quarterly summary of some research related stats. Now my boss wants this report to export into excel based on a specific format she has...
2
by: Amin Bardai | last post by:
How about this: I want to export results of an MS-Access query into an existing XLS template using TransferSpreadsheet method? I can create a new XLS file but I just want to append records from...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.