Solved

MS Access - Too Few Parameters

Posted on 2016-09-21
2
51 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
Comment Utility
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
Comment Utility
<<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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now