Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS Access - Too Few Parameters

Posted on 2016-09-21
2
Medium Priority
?
127 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:Craig Yellick
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 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

604 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