Thanks John. I'll try this.
On Nov 30, 2014, at 4:19 PM, John Viescas JohnV@msn.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:
Patty-
What you need to do is modify the query on the fly. Try this:Dim qd As DAO.QueryDef' Create a temp querySet qd = dbs.CreateQueryDef "zqryExcel"Do Until rst.EOF' Modify the query to filter on the current OfficeIDqd.SQL = "Select * from [qryListOfCopiers_Xerox_Survey] where [OfficeID] = " & (rst!OfficeID) & ";"
outputFileName = "C:\CopierSurveyForms\" & strTitle' Output using the filtered temp queryDoCmd.OutputTo acOutputQuery, "zqryExcel", "MicrosoftExcel(*.xls)", outputFileName, False, ""rst.MoveNext
Loop
rst.Close' Close and delete the temp queryqd.Closedbs.QueryDefs.Delete "zqryExcel"What the above code is doing is creating a temporary query and then sets its SQL to filter on the current OfficeID. It uses the filtered query to output your data on each pass.When done, it closes and deletes the "temp" query.John Viescas, AuthorMicrosoft Access 2010 Inside OutMicrosoft Access 2007 Inside OutMicrosoft Access 2003 Inside OutBuilding Microsoft Access ApplicationsSQL Queries for Mere Mortals(Paris, France)On Nov 30, 2014, at 4:54 PM, Patricia Mapes patty.mapes@gmail.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:I tried Steve's qryTemp idea but couldn't make it work. I created qryTemp in the application, and put a reference to the strId but either it doesn't work, or I don't know how to do it.What I am trying to do it take a list of 500+ records (in a query) and create more or less 60 xls files, where each file will only have one "OfficeID". The files will then be emailed to the 60+ offices.I have one query with Titles for the xls file - more or less 60 records.The second query has the records. I had thought I would be able to use the query and "where" to do this, but I'm not having success.I also tried transfer spreadsheet and ran into the same issues.I guess the most important question is, can I do this at all? It seems obvious, but I cannot figure it out.I am grateful for any and all help.Thank you,PattyThis is my code:Dim strSQL As String
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim rstDetail As DAO.Recordset
Dim strTitle As String
Dim outputFileName As String
Dim qryTemp As String
Dim strOutput As String
Dim qryString As String
Dim frmListXeroxCopiers As FormSet dbs = CurrentDBSet rst = CurrentDB.OpenRecordset("Select * from[qryCopiersTitles_Xerox_Survey]")
Do Until rst.EOFSet rstDetail = CurrentDB.OpenRecordset("Select * from[qryListOfCopiers_Xerox_Survey] where [OfficeID] = " & (rst!OfficeID) & ";")
MsgBox "title data complete"
strSQL = "qryListOfCopiers_Xerox_Survey"outputFileName = "C:\CopierSurveyForms\" & strTitleDoCmd.OutputTo acOutputQuery, strSQL, "MicrosoftExcel(*.xls)", outputFileName, False, ""'DoCmd.OutputTo acOutputReport, qryListOfCopiers_Xerox_Survey, acFormatXLS, "", False, ""
rst.MoveNext
Loop
rst.Close
On Tue, Nov 25, 2014 at 10:52 AM, Patricia Mapes <patty.mapes@gmail.com> wrote:I have the code below where I am trying to create multiple excel files, filtered by an OfficeID. They have distinct titles and each file should have all the records for a particular OfficeID.I can export the query to one file with the first title, and all of the records for all of the OfficeID's. I cannot seem to file the query satisfactorily however.No matter what I do I get some variation of the message:"...could not find the object 'qryListOfCopiers_Xerox_Survey where [OfficeID]= 2' (Two being the correct value for the first set of records.)I have tried many different ways to do this (leaving a couple commented out in the code) and each one gives a very close variation of the same message.I am hoping that you will the optimum way to do this!Thank you,PattyCode:Set dbs = CurrentDB
Set rst = CurrentDB.OpenRecordset("Select * from[qryCopiersTitles_Xerox_Survey]")
strId = rst!OfficeID
rst.Filter = "OfficeID = '" & strId & "'"
Set rstFiltered = rst
strTitle = rstFiltered!Title & "_" & ".xls"
strSQL = "qryListOfCopiers_Xerox_Survey"
'strSQL = "qryListOfCopiers_Xerox_Survey where (qryListOfCopiers_Xerox_Survey.OfficeID) = " & rstFiltered!OfficeID & ")"
'strSQL = "qryListOfCopiers_Xerox_Survey where [OfficeID] = " & strId & ")"
outputFileName = "C:\CopierSurveyForms\" & strTitle
DoCmd.OutputTo acQuery, strSQL, "MicrosoftExcel(*.xls)", outputFileName, False, ""
MsgBox "Export completed."
rst.Close
Exit Sub
--Patricia Mapes
--Patricia Mapes
__._,_.___
Posted by: Patricia Mapes <patty.mapes@gmail.com>
Reply via web post | • | Reply to sender | • | Reply to group | • | Start a New Topic | • | Messages in this topic (9) |
.
__,_._,___
Tidak ada komentar:
Posting Komentar