title: Where Statement in DoCmd.OutputTo acOutputForm
body: <p>I'm working on a database that already sends out two sales reports based on a date range that the user enters into a form. However, I'm attempting to add another report that will be output from a form based on the sales guys' ID. However, I'm having trouble figuring out where to put the where statement stating "WHERE TERRITORY=" &amp; rs.fields(1) , which is what I need to make the specification equal to, if that makes sense. Here is my code. Any help with this is much appreciated. </p>
  
  <p>**The line for FinalFormWithGauges is the one I'm specifically talking about </p>
  
  <pre><code>Dim rs As DAO.Recordset
  Dim fld As DAO.Field
  Dim territory As String
  Dim strSQL As String
  Dim strSQL2 As String
  Dim strSQL3 As String
  Dim strQry As String
  Dim qdf As QueryDef
  Dim qdf2 As QueryDef
  Dim dbs As Database
  Dim i As Integer
  Dim timestamp As String
  DoCmd.OpenForm "Sales Email"
  Forms![Sales Email]![FromDate].SetFocus
  
  timestamp = Format(Forms![Sales Email]![FromDate].Value, "mm-yyyy")
  
  
  Set rs = DBEngine(0)(0).OpenRecordset("KNOXLIVE_Engineers_Test")
  
  For Each fld In rs.Fields
  Do While Not rs.EOF
  territory = rs.Fields(1)
  Set dbs = CurrentDb
  strSQL = "SELECT * FROM 001_SALES_OUTPUT_FOR_CURRENT_MONTH WHERE TERRITORY = """ &amp; rs.Fields(1) &amp; """"
  strSQL2 = "SELECT * FROM CURRENT_OPEN_ORDERS WHERE TERRITORY = """ &amp; rs.Fields(1) &amp; """"
  'strSQL3 = "SELECT * FROM FinalFormWithGauges WHERE TERRITORY = """ &amp; rs.Fields(1) &amp; """"
  'MsgBox (strSQL)
  Set qdf = dbs.CreateQueryDef("TheNewQueryDef", strSQL)
  DoCmd.OutputTo acOutputQuery, "TheNewQueryDef", acFormatXLS, "\\cletus\data\accounting\Regional Sales Reporting\Monthly Sales Database Files By Month\Monthly Sales for " &amp; territory &amp; " " &amp; timestamp &amp; ".xls", False
  CurrentDb.QueryDefs.Delete qdf.Name
  Set qdf2 = dbs.CreateQueryDef("TheNewQueryDef", strSQL2)
  DoCmd.OutputTo acOutputQuery, "TheNewQueryDef", acFormatXLS, "\\cletus\data\accounting\Regional Sales Reporting\Monthly Sales Database Files By Month\Current Open Orders for " &amp; territory &amp; " " &amp; timestamp &amp; ".xls", False
  CurrentDb.QueryDefs.Delete qdf.Name
  'Set gdf3 = dbs.CreateQueryDef("TheNewQueryDef", strSQL3)
  DoCmd.OutputTo acOutputForm, "FinalFormWithGauges", acFormatPDF, "\\cletus\data\accounting\Regional Sales Reporting\Monthly Sales Database Files By Month\Actual Sales vs Budget for " &amp; territory &amp; " " &amp; timestamp &amp; ".PDF", False**
  'CurrentDb.QueryDefs.Delete qdf.Name
  </code></pre>