Results 1 to 4 of 4
  1. #1
    SevnSins is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2010
    Posts
    2

    Execute Insert Into SQL with Variable define recordset

    I have the following module for exporting some sales reports, and am having problems getting it to run completely.

    I have defined Rs2 as a recordset, and want to use it for the data source of my Insert Into statements. Is this possible?

    Here's what I have so far. I'm getting a "Missing Semicolon" Error on the line in red.


    Code:
    Public Sub ProdGrpExport()
    
    Dim dbPath As String
    Dim fileFolder As String
    Dim Rs1 As DAO.Recordset
    Dim Rs2 As DAO.Recordset
    Dim Rs3 As DAO.Recordset
    Dim strRepID As String
    Dim strRep As String
    
    dbPath = Application.CurrentProject.Path
    Set Rs1 = CurrentDb.OpenRecordset("SELECT tblSmGrd.SalesTerrSCust, tblTerr.TerrName FROM tblTerr RIGHT JOIN tblSmGrd ON tblTerr.TerrID = tblSmGrd.SalesTerrSCust GROUP BY tblSmGrd.SalesTerrSCust, tblTerr.TerrName HAVING (((tblSmGrd.SalesTerrSCust) Is Not Null And (tblSmGrd.SalesTerrSCust)<>' '));")
    
    Do Until Rs1.EOF
    strRepID = Rs1("SalesTerrSCust")
    strRep = Rs1("TerrName")
    
    Set Rs2 = CurrentDb.OpenRecordset("SELECT sum(tblSmGrd.Year) AS Yr, tblSmGrd.ProdGrp, tblSmGrd.ProdCat, sum(tblSmGrd.NWgtImperial) AS Vol, sum(tblSmGrd.GrossSalesUSD) AS GsUSD, sum(tblSmGrd.GrossSalesL) AS GsCAD, sum(tblSmGrd.NetSalesUSD) AS NsUSD, sum(tblSmGrd.NetSalesL) AS NsCAD, sum(tblSmGrd.COGSUSD) AS CgUSD, sum(tblSmGrd.COGSL) AS CgCAD, sum(tblSmGrd.MarginUSD) AS MgnUSD, sum(tblSmGrd.MarginL) AS MgnCAD FROM tblSmGrd WHERE (((tblSmGrd.SalesTerrSCust)= '" & SalesTerrScust & "')) GROUP BY tblSmGrd.Year, tblSmGrd.ProdGrp, tblSmGrd.ProdCat;", dbOpenDynaset)
    
    DoCmd.RunSQL ("DELETE tblProdTot.* FROM tblProdTot;")
    CurrentDb.Execute ("INSERT INTO tblProdTot ( ProdGrp, ProdCat, BuVol, BuGsUSD, BuGsCAD, BuNsUSD, BuNsCAD, BuCgUSD, BuCgCAD, BuMgnUSD, BuMgnCAD ) VALUES (Rs2.ProdGrp, Rs2.ProdCat, Rs2.Vol, Rs2.GsUSD, Rs2.GsCAD, Rs2.NsUSD, Rs2.NsCAD, Rs2.CgUSD, Rs2.CgCAD, Rs2.MgnUSD, Rs2.MgnCAD) FROM Rs2 WHERE Rs2.Yr=10;")
    
    DoCmd.OpenReport "rptProdTot", acViewReport
    DoCmd.OutputTo acOutputReport, "rptProdTot", acFormatPDF, dbPath & "\Exports\" & Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & "\" & strRep & ".pdf", , , , acExportQualityPrint
    
    Rs1.MoveNext
    Loop
    
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    As noted on the other site you have this, you can't refer to the recordset values in the SQL. You'd have to concatenate them in:

    "...VALUES(" & Rs2.ProdGrp & ", "...

    Note that would work for numeric data types. Text would need single quotes added:

    "...VALUES('" & Rs2.ProdGrp & "', "...

    and dates:

    "...VALUES(#" & Rs2.ProdGrp & "#, "...

    That said, it seems like a roundabout way of creating reports. I would think you could have the report based on a query that got the criteria for SalesTerrSCust from a form control. Then cycle through your recordset placing the current value in that form control and running the report. This would be another method you could adapt:

    http://www.granite.ab.ca/access/emai...recipients.htm

  3. #3
    SevnSins is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2010
    Posts
    2
    I tried it using the syntax you provided, but it now tells me Method or Data Member not Found.

    About you're other suggestion...
    I think I understand what you're saying:

    1. attach my "filter" query (Rs2) to a text box in a hidden form?
    2. Then tell my module to apply the loop variable to the text box
    3. Run my Insert statements
    4. open/export report
    5. Loop


    If so, I think that will work. I will try it real quick, and see what happens.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    As to the error, I made the same copy/paste error; dot should be bang:

    Rs2!ProdGrp

    Yes to what you propose except no insert statements. You shouldn't need a temp table (they have their place, but this doesn't sound like one of them). The report would be based on a query, not a table.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Can a report execute generate a file
    By techexpressinc in forum Reports
    Replies: 7
    Last Post: 01-16-2010, 04:03 AM
  2. Replies: 1
    Last Post: 11-13-2009, 03:03 AM
  3. Variable Criteria
    By JamesLens in forum Queries
    Replies: 0
    Last Post: 01-02-2009, 04:55 PM
  4. Execute Command Interactively
    By zephaneas in forum Programming
    Replies: 1
    Last Post: 11-08-2008, 09:56 AM
  5. Variable question
    By synapse5150 in forum Programming
    Replies: 1
    Last Post: 07-09-2008, 08:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums