Solved

MS Access - Too Few Parameters

Posted on 2016-09-21
2
65 Views
Last Modified: 2016-09-26
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.
0
Comment
Question by:Jack Marley
2 Comments
 
LVL 11

Expert Comment

by:CraigYellick
ID: 41809175
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

0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 41809208
<<Runtime Error - Too few parameters - Expected 1>>

 What this means is that Access doesn't have a value for what it thinks is a parameter in the query.

 That can be something as simple as a mis-spelled table or field reference.     If it's a valid parameter (like a reference to a control on a form), then you'll need to provide a value for that before you execute the query (when you open a query in code, Access leaves everything up to you).  There are various ways to do that.

 If you open the query on it's own as Craig suggested, you'll get prompted for the parameter that it's trying to figure out and can go from there.

 Also, one other comment; opening a recordset as a snapshot is not a great idea unless you really need it.  While it sounds fast, what your asking for is a copy of every record.   On a large table, that can cause a considerable delay.   Use a dynaset instead.

Jim.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question