Avatar of Jack Marley
Jack Marley
 asked on

MS Access - Too Few Parameters

Hi,

New to MS Access so apologies in advance for glaring errors. I'm trying to leverage an Access template to build a database which can create and print invoices (or save as PDF).

Have a form called "SelectedOrdersToPrint" as he main menu. However when I select the invoices to save, I get "Runtime Error - Too few parameters - Expected 1" which it errors at line "Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)"

Here's the code in question:

Private Sub cmdSaveAsPDF_Click()



    Dim qdf As DAO.QueryDef
    Dim strSQL As String
    Dim strPathName As String
    Dim blRet As Boolean
    Dim rs As Recordset
    Dim stDocName As String
    
    Dim strSavedSQL As String
    
    If Me.Dirty Then Me.Dirty = False

    stDocName = "InvTotal"
    
   
    strSQL = "SELECT Contracts.OrderID FROM Contracts WHERE (((Contracts.SelectedPrint)=True));"
    
    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
    
    
    If rs.RecordCount < 1 Then
       MsgBox "Nothing found to process", vbCritical, "Error"
       Exit Sub
    End If
    
    CreateFolder CurrentProject.Path & "\Contracts"
    
    
      ' store the current SQL
        Set qdf = CurrentDb.QueryDefs("Invoices")
        strSavedSQL = qdf.SQL
        qdf.Close
        Set qdf = Nothing
    
    
    Do
    
        Set qdf = CurrentDb.QueryDefs("Invoices")
        strSQL = Left(strSavedSQL, InStr(strSavedSQL, ";") - 1) & " and (Contracts.OrderID = " & rs!OrderID & ");"
        qdf.SQL = strSQL
        qdf.Close
        Set qdf = Nothing
    
        ' put in the same folder as the database
         strPathName = CurrentProject.Path & "\Contracts\" & rs!OrderID & ".pdf"
        
        DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, strPathName

        rs.MoveNext
    
   Loop Until rs.EOF

   rs.Close
   
   Set rs = Nothing

      ' restore the  SQL
        Set qdf = CurrentDb.QueryDefs("Invoices")
        qdf.SQL = strSavedSQL
        qdf.Close
        Set qdf = Nothing


End Sub

Open in new window


I've attached the Access Database I'm working on. If anyone out there can offer any guidance it'd be much appreciated.

Many thanks.
Microsoft AccessVBA

Avatar of undefined
Last Comment
Jim Dettman (EE MVE)

8/22/2022 - Mon
Craig Yellick

Copy and paste the query text into a QueryDef SQL window and execute. You'll get much better error reporting. One thing I can see is that you have three opening parens but only two closing. Fix that, first. Then the query designer will clue you in to the misspelled elements.

SELECT Contracts.OrderID FROM Contracts WHERE (((Contracts.SelectedPrint)=True))

Open in new window

ASKER CERTIFIED SOLUTION
Jim Dettman (EE MVE)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck