Solved

MS Access - Too Few Parameters

Posted on 2016-09-21
2
88 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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