My Assistant
![]() ![]() |
|
|
Mar 30 2012, 08:33 AM
Post
#1
|
|
|
UtterAccess VIP Posts: 6,334 From: Bethesda, MD USA |
Well, if it's not one query it's another query of mine, that when exported to Excel I get a Run Time Error, why? I can always run the Queries fine, it's just when I try to export them into Excel, is when I get the error. Last time is was a select Query, this time it is a Union Query and these are the same queries I have used over and over again. If I create a Make Table Query, based on the Union Query, I am then able to export the data into Excel, I just don't want it to do it this way, because I will have to run the Make Table Query each time before I can export each set of data.
Suggestions? Thanks!!!! RAZMaddaz |
|
|
|
Mar 30 2012, 08:49 AM
Post
#2
|
|
|
UtterAccess Veteran Posts: 320 From: Ohio, USA |
How about just a temporary table?
My trick... run make table query, naming the table "ztmp" & descriptive table name (like the qry name, perhaps). Then change the query to an append query. Then if you have code that exports the results to Excel, run a delete query on the "ztmp" table, then the append query, then export to Excel. I've used this trick quite a bit. |
|
|
|
Mar 30 2012, 09:00 AM
Post
#3
|
|
|
UtterAccess VIP Posts: 6,334 From: Bethesda, MD USA |
Thanks Nuclear for your suggestion!!!!
I really am just try to figure out why this is happening, if it's not one Query this Quarter, it might be another Query next Quarter and it's not when the Query is run, it's when the Query is exported. Am I doing something wrong in VB? |
|
|
|
Mar 30 2012, 09:03 AM
Post
#4
|
|
|
UtterAccess VIP Posts: 8,191 From: CT |
ah the simple pleasure of asking a top VIP this.....
RAZ, can you post how you are exporting to excel? Perhaps we can see something. (IMG:style_emoticons/default/laugh.gif) |
|
|
|
Mar 30 2012, 09:11 AM
Post
#5
|
|
|
UtterAccess VIP Posts: 6,334 From: Bethesda, MD USA |
Thanks Bob!!!!!!!!!!!
Here is some of the VB: CODE Private Sub XXXX_Click()
Dim cnn5 As ADODB.Connection Set cnn5 = CurrentProject.Connection Dim rstWeeklyTimeslips5 As New ADODB.Recordset rstWeeklyTimeslips5.ActiveConnection = cnn5 Dim objWS5 As Excel.Worksheet Dim fld5 As ADODB.Field Dim objXL5 As Excel.Application Dim wbk5 As Excel.Workbook Dim intCol5 As Integer Dim intRow5 As Integer The next like currently doesn't work, however once I made the Table based on the Union Query, I was to Export the Table, two rows down, called EBUSECGTemp 'rstWeeklyTimeslips5.Open "EBUSandECG_RatiosByQuarter_Union", CurrentProject.Connection rstWeeklyTimeslips5.Open "EBUSECGTemp", CurrentProject.Connection Set objXL5 = New Excel.Application Set objWS5 = objXL5.Workbooks.Open("C:\Documents and Settings\t5000640\My Documents\ECG OA\ECG New Quarter Version\1Q_2012.xls").Sheets(6) For intCol5 = 0 To rstWeeklyTimeslips5.Fields.Count - 1 Set fld5 = rstWeeklyTimeslips5.Fields(intCol5) 'This is the First Row and Column of the Field Names 'Therefore the first Column is Column 0 (intCol5 = 0) + 1 objWS5.Cells(35, intCol5 + 1) = fld5.Name Next intCol5 'This is the First Row of the Data intRow5 = 36 'now the actual data Do Until rstWeeklyTimeslips5.EOF For intCol5 = 0 To rstWeeklyTimeslips5.Fields.Count - 1 'This is the first Row and Column of the Data objWS5.Cells(intRow5, intCol5 + 1) = rstWeeklyTimeslips5.Fields(intCol5).Value Next intCol5 rstWeeklyTimeslips5.MoveNext intRow5 = intRow5 + 1 Loop objXL5.Visible = True rstWeeklyTimeslips5.Close Set cnn5 = Nothing Set objXL5 = Nothing Set wbk5 = Nothing Set objWS5 = Nothing End Sub |
|
|
|
Mar 30 2012, 10:09 AM
Post
#6
|
|
|
UtterAccess VIP Posts: 8,191 From: CT |
Not surprised to see you are not using DoCmd.TransferSpreadsheet acExport but instead a module.
I use the above to send my query output to the excel then if i need to do any cosmetics to the worksheet I do it after the above. sorry that it isnt of much help |
|
|
|
Mar 30 2012, 10:27 AM
Post
#7
|
|
|
UtterAccess VIP Posts: 6,334 From: Bethesda, MD USA |
I didn't show you all of the VB, just the area where I am getting the error. Let me explain:
Depending on which button is chosen on one Form, will determine the criteria and the Fields of the criteria. After a button is clicked on this form and the Queries are run, the results from four sometimes five different sets of queries show. Then on another form I click on a button, this is where I am getting the error, which exports the results into Excel and then opens Excel where I need to manually do some formatting. If I should be using DoCmd.Transfer...instead, tell me how. I'll try anything to get this to work, except having to make another Query and Table. But why would this make a difference if I am able to export the same data, but from a Table as oppose to a Query? Thanks for your help!!!!!!!!!!!!!!!!!!!!!!!!! RAZ |
|
|
|
Mar 30 2012, 10:31 AM
Post
#8
|
|
|
UtterAccess VIP Posts: 8,191 From: CT |
there may be results from 4 or 5 queries, would all them be going to the same worksheet or is each one separate ??
|
|
|
|
Mar 30 2012, 10:41 AM
Post
#9
|
|
|
UtterAccess VIP Posts: 6,334 From: Bethesda, MD USA |
Four of them on the same and the fifth onto a different, which is why in the VB that you didn't see, I have Excel opened saved and Quit after the first four are copied onto the spreadsheet, then I have the fifth copied onto a different spreadsheet and the file in then opened and I make some formatting changes.
Below is a complete VB of the process of coping everything over to Excel. Sorry!!!! CODE Private Sub ReducedService_5_2_Click()
'You need to change the objWS and wbk each quarter to the new Excel File name, which will be the date Dim cnn As ADODB.Connection Set cnn = CurrentProject.Connection Dim rstWeeklyTimeslips As New ADODB.Recordset Dim rstWeeklyTimeslips2 As New ADODB.Recordset Dim rstWeeklyTimeslips3 As New ADODB.Recordset Dim rstWeeklyTimeslips4 As New ADODB.Recordset rstWeeklyTimeslips.ActiveConnection = cnn rstWeeklyTimeslips2.ActiveConnection = cnn rstWeeklyTimeslips3.ActiveConnection = cnn rstWeeklyTimeslips4.ActiveConnection = cnn Dim MySheetPath As String Dim objXL As Excel.Application Dim wbk As Excel.Workbook Dim objWS As Excel.Worksheet Dim StartRange As Excel.Range Dim fld As ADODB.Field Dim fld2 As ADODB.Field Dim fld3 As ADODB.Field Dim fld4 As ADODB.Field Dim intCol As Integer Dim intCol2 As Integer Dim intCol3 As Integer Dim intCol4 As Integer Dim intRow As Integer Dim intRow2 As Integer Dim intRow3 As Integer Dim intRow4 As Integer rstWeeklyTimeslips.Open "ECGStep3", CurrentProject.Connection rstWeeklyTimeslips2.Open "ECGStep3ByQuarter", CurrentProject.Connection 'rstWeeklyTimeslips3.Open "EBUSandECG_RatiosByMonth_Union", CurrentProject.Connection rstWeeklyTimeslips3.Open "EBUSECGTemp", CurrentProject.Connection rstWeeklyTimeslips4.Open "EBUSandECG_ALL_Table5_X_Averages", CurrentProject.Connection 'Launch Excel 'You need to update the name of the Spreedsheet/File below each quarter. Set objXL = New Excel.Application Set objWS = objXL.Workbooks.Open("C:\Documents and Settings\t5000640\My Documents\ECG OA\ECG New Quarter Version\1Q_2012.xls").Sheets(4) Set wbk = GetObject("C:\Documents and Settings\t5000640\My Documents\ECG OA\ECG New Quarter Version\1Q_2012.xls") 'Copy the data 'First the field names For intCol = 0 To rstWeeklyTimeslips.Fields.Count - 1 Set fld = rstWeeklyTimeslips.Fields(intCol) 'This is the First Row and Column of the Field Names 'Therefore the first Column is Column 0 (intCol = 0) + 1 objWS.Cells(1, intCol + 1) = fld.Name Next intCol 'This is the First Row of the Data intRow = 2 'now the actual data Do Until rstWeeklyTimeslips.EOF For intCol = 0 To rstWeeklyTimeslips.Fields.Count - 1 'This is the first Row and Column of the Data objWS.Cells(intRow, intCol + 1) = rstWeeklyTimeslips.Fields(intCol).Value Next intCol rstWeeklyTimeslips.MoveNext intRow = intRow + 1 Loop 'Copy the data 'First the field names For intCol2 = 0 To rstWeeklyTimeslips2.Fields.Count - 1 Set fld2 = rstWeeklyTimeslips2.Fields(intCol2) 'This is the First Row and Column of the Field Names 'Therefore the first Column is Column 0 (intCol = 0) + 1 objWS.Cells(36, intCol2 + 1) = fld2.Name Next intCol2 'Set 2 'This is the First Row of the Data intRow2 = 37 'now the actual data Do Until rstWeeklyTimeslips2.EOF For intCol2 = 0 To rstWeeklyTimeslips2.Fields.Count - 1 'This is the first Row and Column of the Data objWS.Cells(intRow2, intCol2 + 1) = rstWeeklyTimeslips2.Fields(intCol2).Value Next intCol2 rstWeeklyTimeslips2.MoveNext intRow2 = intRow2 + 1 Loop 'Copy the data 'First the field names 'Set 3 For intCol3 = 0 To rstWeeklyTimeslips3.Fields.Count - 1 Set fld3 = rstWeeklyTimeslips3.Fields(intCol3) 'This is the First Row and Column of the Field Names 'Therefore the first Column is Column 0 (intCol3 = 0) + 1 objWS.Cells(63, intCol3 + 1) = fld3.Name Next intCol3 'This is the First Row of the Data intRow3 = 64 'now the actual data Do Until rstWeeklyTimeslips3.EOF For intCol3 = 0 To rstWeeklyTimeslips3.Fields.Count - 1 'This is the first Row and Column of the Data objWS.Cells(intRow3, intCol3 + 1) = rstWeeklyTimeslips3.Fields(intCol3).Value Next intCol3 rstWeeklyTimeslips3.MoveNext intRow3 = intRow3 + 1 Loop 'Set 4 For intCol4 = 0 To rstWeeklyTimeslips4.Fields.Count - 1 Set fld4 = rstWeeklyTimeslips4.Fields(intCol4) 'This is the First Row and Column of the Field Names 'Therefore the first Column is Column 0 (intCol4 = 0) + 1 objWS.Cells(70, intCol4 + 1) = fld4.Name Next intCol4 'This is the First Row of the Data intRow4 = 71 'now the actual data Do Until rstWeeklyTimeslips4.EOF For intCol4 = 0 To rstWeeklyTimeslips4.Fields.Count - 1 'This is the first Row and Column of the Data objWS.Cells(intRow4, intCol4 + 1) = rstWeeklyTimeslips4.Fields(intCol4).Value Next intCol4 rstWeeklyTimeslips4.MoveNext intRow4 = intRow4 + 1 Loop wbk.Save objXL.Quit rstWeeklyTimeslips.Close rstWeeklyTimeslips2.Close rstWeeklyTimeslips3.Close rstWeeklyTimeslips4.Close Set cnn = Nothing Set objXL = Nothing Set wbk = Nothing Set objWS = Nothing 'Set 5 Dim cnn5 As ADODB.Connection Set cnn5 = CurrentProject.Connection Dim rstWeeklyTimeslips5 As New ADODB.Recordset rstWeeklyTimeslips5.ActiveConnection = cnn5 Dim objWS5 As Excel.Worksheet Dim fld5 As ADODB.Field Dim objXL5 As Excel.Application Dim wbk5 As Excel.Workbook Dim intCol5 As Integer Dim intRow5 As Integer 'rstWeeklyTimeslips5.Open "EBUSandECG_RatiosByQuarter_Union", CurrentProject.Connection rstWeeklyTimeslips5.Open "EBUSECGTemp", CurrentProject.Connection Set objXL5 = New Excel.Application 'You need to change the name of the Spreedsheet/File in the objWS5 below each quarter. Set objWS5 = objXL5.Workbooks.Open("C:\Documents and Settings\t5000640\My Documents\ECG OA\ECG New Quarter Version\1Q_2012.xls").Sheets(6) For intCol5 = 0 To rstWeeklyTimeslips5.Fields.Count - 1 Set fld5 = rstWeeklyTimeslips5.Fields(intCol5) 'This is the First Row and Column of the Field Names 'Therefore the first Column is Column 0 (intCol5 = 0) + 1 objWS5.Cells(35, intCol5 + 1) = fld5.Name Next intCol5 'This is the First Row of the Data intRow5 = 36 'now the actual data Do Until rstWeeklyTimeslips5.EOF For intCol5 = 0 To rstWeeklyTimeslips5.Fields.Count - 1 'This is the first Row and Column of the Data objWS5.Cells(intRow5, intCol5 + 1) = rstWeeklyTimeslips5.Fields(intCol5).Value Next intCol5 rstWeeklyTimeslips5.MoveNext intRow5 = intRow5 + 1 Loop objXL5.Visible = True rstWeeklyTimeslips5.Close Set cnn5 = Nothing Set objXL5 = Nothing Set wbk5 = Nothing Set objWS5 = Nothing End Sub |
|
|
|
Mar 30 2012, 10:43 AM
Post
#10
|
|
|
UtterAccess Veteran Posts: 320 From: Ohio, USA |
Instead of Docmd.Transfer, I use...
CODE DoCmd.OutputTo acOutputQuery, <<query name>>, AutoStart:=True There are some other parameters you can use as well, but for a while I had users with 2003 and 2010 so I used the above, which causes a box to open up and ask what they wanted it exported to. The whole thing is... QUOTE expression.OutputTo(ObjectType, ObjectName, OutputFormat, OutputFile, AutoStart, TemplateFile, Encoding, OutputQuality) Off the top of my head, the autostart=true is the command for opening the program you exported the query to. I haven't tried anything other than that and the TransferSpreadsheet. |
|
|
|
Mar 30 2012, 02:54 PM
Post
#11
|
|
|
UtterAccess VIP Posts: 6,334 From: Bethesda, MD USA |
Any suggestions regarding the VB script I am using above and why I am getting the error message?
Thanks!!!!! RAZMaddaz |
|
|
|
Apr 2 2012, 12:47 PM
Post
#12
|
|
|
UtterAccess VIP Posts: 6,334 From: Bethesda, MD USA |
I am getting the error on the Open of an ADODB.Recordset. The error happens when it is being opened and the connection fails.
The error is the following: CurrentProject.Connection="Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Dat... Note: I am unable to read anything after the "Dat..." Thanks for the help!!! RAZMaddaz |
|
|
|
Apr 2 2012, 12:56 PM
Post
#13
|
|
|
UtterAccess VIP Posts: 8,191 From: CT |
RAZ,
Sorry I have nothing more to suggest on this. I don't even come close to doing anything this way and wouldn't know where to start troubleshooting. B |
|
|
|
Apr 2 2012, 01:10 PM
Post
#14
|
|
|
UtterAccess VIP Posts: 6,334 From: Bethesda, MD USA |
"doing it this way", what way might you suggest copying the data into Excel?
BTW, I originally created this in Access 2003 and I don't recall, but MAYBE this problem might have started since the company upgraded to 2007. Is there something in the References area that I might need to change? Thanks for your help Bob!!!!!!!!! RAZ |
|
|
|
Apr 2 2012, 01:20 PM
Post
#15
|
|
|
UtterAccess VIP Posts: 8,191 From: CT |
looking at it from the very basic level and going forward. Realizing that some of these steps may not be the best, just wondering if they would actually work.
could you do a docmd.transferspreadsheet for each of the queries and then take the however many spreadsheets and combine them into one workbook? |
|
|
|
Apr 2 2012, 01:38 PM
Post
#16
|
|
|
UtterAccess VIP Posts: 6,334 From: Bethesda, MD USA |
Unfortunately no, because there are dozens of results that are transfered to certain areas in the workbook, where charts and graphs are setup to use the data.
Why is then when I create a Make Table based on the Query, I am able to transfer the data into Excel? |
|
|
|
Apr 2 2012, 01:40 PM
Post
#17
|
|
|
UtterAccess VIP Posts: 8,191 From: CT |
no clue
|
|
|
|
Apr 2 2012, 01:55 PM
Post
#18
|
|
|
UtterAccess VIP Posts: 6,334 From: Bethesda, MD USA |
Alright, thanks for your help!!!
(IMG:style_emoticons/default/cheers.gif) If I find something out, I 'll let you know. RAZMaddaz |
|
|
|
Apr 2 2012, 01:58 PM
Post
#19
|
|
|
UtterAccess VIP Posts: 8,191 From: CT |
strange that the "excel vips" havent chimed in.
I bet if I suggested you use .select they would come out of the woodwork. good luck |
|
|
|
Apr 3 2012, 03:45 AM
Post
#20
|
|
|
UtterAccess Editor Posts: 6,726 From: Capital District, NY, USA |
Doesn't appear to be an Excel problem but rather an ADO problem. I don't work with ADO much (at all, really), but I'd think that similar to DAO, you'll have different cursor types for the recordsets, which, like DAO, may be defaulted to one thing or another based on what type of object you're opening (table vs. query, which makes sense so far...)
The query itself will have some properties you might want to take a look at, as well as trying to find what information you can on the ADO recordset types and any caveats that might go with them. That's where I'd start... sorry I don't have the experience with ADO to give any better advice, but I'd take a good look at the querydef properties as well as the ADO recordset/cursor type which may be dependant on the query type. hth |
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 19th June 2013 - 03:17 PM |