I assume that the [Top 200 Customer Accounts Based on LY Sales by Branch*]
query contains the parameter that asks for the [Branch Number] value, right?
If yes, modify that query to eliminate that criterion parameter from the
query. The query now should be this:
SELECT TOP 100 [Branch List].[Branch Number], [Top Customer Accounts Sales
Base].SAccountNum, [Top Customer Accounts Sales Base].SAccountName, [Top
Customer Accounts Sales Base].SSalesman, [Top Customer Accounts Sales
Base].SName, [Top Customer Accounts Sales Base].NYTDSold, [Top Customer
Accounts Sales Base].NYTDSoldPrev
FROM [Top Customer Accounts Sales Base] INNER JOIN [Branch List] ON [Top
Customer Accounts Sales Base].[Acct Prefix] = [Branch List].[Branch
Prefix]
ORDER BY [Top Customer Accounts Sales Base].NYTDSold DESC;
(Parenthetical note: It's not a good idea to use a character such as * in
the name of a query. If not properly handled, you can cause problems for
your query because * is the wild card search character.)
OK, now create a new, regular module in your database (see Modules on left
side of database window). You can name the module anything you want, so long
as it's not the same name as the function that we're going to create next.
Open the Module that you've created and paste the following function into
the module (I've written this function with the assumption that the Branch
Number field is a numeric field, not a text field):
' *** Start of Function
Public Function ExportMyReports() AS Variant
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Dim lngOrderBy As Long
Dim strSQL As String, strSQL_O As String, strSQL_S As String
Dim strSQL_S1 As String, strSQL_S2 As String
Const strQuery As String = "Top 200 Customer Accounts Based on LY Sales by
Branch*"
Const strRecordset As String = "Branch List Query"
Const strCriterion As String = " WHERE [Branch Number]="
Const strFile As String = "//Sps1/clc/FOCUS/1001/Shared%20Documents/Top 200
Customer Accounts based on LY Sales by Branch _ "
Const strReport As String = "Top 200 Customer Accounts based on LY Sales by
Branch"
Set dbs = CurrentDb()
Set qdf = dbs.QueryDefs(strQuery)
' Save original SQL string
strSQL_O = qdf.SQL
strSQL_S = strSQL_O
lngOrderBy = InStrRev(strSQL_S, "ORDER BY")
' Get part of SQL string before ORDER BY clause
strSQL_S1=Left(strSQL_S, lngOrderBy -1) & " "
' Get part of SQL string starting with ORDER BY clause
strSQL_S2 = " " & Mid(strSQL_S, lngOrderBy)
Set rst = dbs.OpenRecordset(strRecordset, dbOpenDynaset, dbReadOnly)
Do While rst.EOF = False
' create branch-specific SQL string in query
strSQL = strCriterion & rst![Branch Number].Value
qdf.SQL = strSQL_S1 & strSQL & strSQL_S2
' output report for the branch (branch number is in file name)
DoCmd.OutputTo acOutputReport, strReport, acFormatRTF, _
strFile & rst![Branch Number].Value & ".rtf",False
' get next branch number
rst.MoveNext
Loop
' Set query's SQL string back to original string
qdf.SQL = strSQL_O
qdf.Close
Set qdf = Nothing
rst.Close
Set rst = Nothing
dbs.Close
Set dbs = Nothing
Exit Function
End Function
' *** End of Function
Now, in the macro that currently does the OutputTo action, replace the steps
with just one step (be sure to include the () at end of function name):
Action: RunCode
Function Name: ExportMyReports()
This macro will run the function, which will create a report for each branch
number in the branch number list query.
--
Ken Snell
<MS ACCESS MVP>
Jim said:
Ok Ken,
I think I have what you need:
I have a query named: [Branch List Query] based off of a table called
[Branch List] with a field called [Branch Number], this is due to the fact
that we have some branch #s that I do not want to run.
Report Recordsource:
Saved query titled [Top 200 Customer Accounts Based on LY Sales by
Branch*]
Sharepoint:
Macro used is OutputTo with the following settings:
Type: Report
Report Name: Top 200 Customer Accounts based on LY Sales by Branch
Output Format: Rich Text Format
Output File: //Sps1/clc/FOCUS/1001/Shared%20Documents/Top 200 Customer
Accounts based on LY Sales by Branch.rtf
The sharepoint outputting works just like kicking a file out to a
folder--so
it's pretty basic.
Thanks for your help and let me know if I've missed anything.
Jim