Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2016
    Location
    Denver
    Posts
    4

    QueryDefs.OpenRecordset problems

    Hi all,



    First-time poster here! Having trouble with this piece of code (mostly because I don't understand it). The problem is I have defined my parameters in frmAccountingDatabaseInput, but when I run the macro, it asks me for parameters for approveparam, dateparam, and typeparam. Shouldn't it already have been defined in frmAccountingDatabaseInput??? Below is the VBA and SQL code I'm using:

    VBA
    Code:
    Sub Save_Invoices_Meet_Criteria()
    
    
    Dim FileName As String
    Dim FilePath As String
    Dim myStmt As String
    Dim Db As DAO.Database
    Dim myrs As DAO.Recordset
    
    
    Set Db = CurrentDb()
    
    
    Dim qdf As DAO.QueryDef
    Set qdf = Db.QueryDefs("qryCreateInvoicesApproved")
    
    
    qdf!Approveparam = [Forms]![frmAccountingDatabaseInput]![Invoice_approved]
    qdf!Dateparam = [Forms]![frmAccountingDatabaseInput]![Combo272]
    qdf!Typeparam = [Forms]![frmAccountingDatabaseInput]![Combo274]
    
    
    Set myrs = qdf.OpenRecordset(dbOpenDynaset)
    
    
    
    
    Do Until myrs.EOF
    
    
    FileName = Me.reference
    foldername = Format(Now(), "YYYY-MM-DD")
    FilePath = "C:\Users\company\Desktop\Invoicing Database\Save_Test\" & foldername & "\" & FileName & ".pdf"
    
    
    
    
    
    
    DoCmd.OpenReport "RPTInvoice", acViewPreview, FilePath
    
    
    
    
    DoCmd.Close
    myrs.MoveNext
    
    
    Loop
    
    
    myrs.Close
    Set myrs = Nothing
    
    
    End Sub

    SQL
    Code:
    PARAMETERS [Approveparam] Bit, [Dateparam] DateTime, [Typeparam] Text ( 255 );
    SELECT DISTINCT tblAccountingDatabase.[reference]
    FROM tblAccountingDatabase
    WHERE (((tblAccountingDatabase.Invoice_approved)=[Approveparam]) AND ((tblAccountingDatabase.invoice_date)=[Dateparam]) AND ((tblAccountingDatabase.Invoice_Type)=[Typeparam]));

    Thanks in advance for any and all help!
    - Dwight K. Schrute

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    You don't need all this code when you can open it with a simple
    docmd.openQuery "qsMyQuery"

  3. #3
    Join Date
    Aug 2016
    Location
    Denver
    Posts
    4
    That's basically what I started with. It leads to an error - Too few parameters. Expected 2.

    This is what the code looks like with your adjustment. I just need to define myrs as the query result but I can't figure out how to do that:
    Code:
    Sub Save_Invoices_Meet_Criteria()
    
    
    Dim FileName As String
    Dim FilePath As String
    Dim myStmt As String
    Dim Db As DAO.Database
    Dim myrs As DAO.Recordset
    
    
    Set Db = CurrentDb()
    
    
    
    
    DoCmd.OpenQuery ("qryCreateInvoicesApproved")
    
    
    
    
    Do Until myrs.EOF
    
    
    FileName = Me.reference
    foldername = Format(Now(), "YYYY-MM-DD")
    FilePath = "C:\Users\company\Desktop\Invoicing Database\Save_Test\" & foldername & "\" & FileName & ".pdf"
    
    
    
    
    DoCmd.OpenReport "RPTInvoice", acPreviewView, FilePath
    
    
    DoCmd.Close
    myrs.MoveNext
    
    
    Loop
    
    
    myrs.Close
    Set myrs = Nothing
    
    
    End Sub

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

Similar Threads

  1. Embedding SQL in VBA vs Using Stored Querydefs
    By sneuberg in forum Queries
    Replies: 2
    Last Post: 02-16-2015, 08:40 AM
  2. Performance of nested querydefs
    By Javier Hernández in forum Queries
    Replies: 4
    Last Post: 12-13-2014, 06:52 PM
  3. Connection and QueryDefs
    By dccjr in forum Programming
    Replies: 11
    Last Post: 01-15-2013, 09:14 PM
  4. Working with QueryDefs and ADO...
    By ETCallHome in forum Programming
    Replies: 1
    Last Post: 03-16-2011, 07:07 AM
  5. CurrentDb.QueryDefs
    By sarnata in forum Queries
    Replies: 3
    Last Post: 09-09-2010, 09:05 AM

Tags for this Thread

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