Results 1 to 14 of 14
  1. #1
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727

    too few parameters; expected 2

    Hi All, Using Access 2003 front end with Sql Server backend. I Have a form users can use to export a query to an excel spreadsheet . On the form are several unbound boxes: division, year, to month and from month. My query is a pass thru query. I am trying to let them choose the division and time frame of the query to output instead of outputting all the records in the query. I used this code before but running reports. I am trying to modify it to use a query instead. This is what I have so far. I am having difficulty referencing division and the year. I tried running to see what I needed but I can't get pass the error. Can someone guide me with this code. Thank you
    Code:
    Private Sub cmdExport_Click()
    On Error GoTo Err_cmdExport_Click
    Dim strSQL As String, intCounter As Integer
    Dim ctl As Control, strname As String, strnewquery As String
    Dim strRptSel As String
    Dim stMessage As String
    Set db = CurrentDb
          'Build SQL String
          For Each ctl In Me.Form
          If ctl.Tag = "input" Then
          'strname = "me." & ctl.Name
          If ctl.Value > "" Then
          strSQL = strSQL & "[" & ctl.Name & "] " & " like " & Chr(34) & ctl.Value & Chr(34) & " And "
          End If
          End If
         Next ctl
        
        ' Set the value of the parameter.
        Select Case Me.Module
        Case SP
           strnewquery = "[qrySPReports_test_passthru]"
        Case LTL
            strnewquery = "[qryLTLReports_test_passthru]"
        Case DTF
            strnewquery = "[qryDTFReports_test_passthru]"
        If Me.cboFROM & vbNullString <> "" And Me.cboTO & vbNullString <> "" Then
            strSQL = strSQL & " ([MONTHPROCESSED] BETWEEN " & Me.cboFROM & " And " & Me.cboTO & ") And "
        End If
        
        strnewquery = "Select qrySPReports_test_passthru.* FROM qrySPReports_test_passthru"
        If strSQL <> "" Then
            strSQL = Left(strSQL, (Len(strSQL) - 5))
            strnewquery = strnewquery & " WHERE " & strSQL & ";"
        End If
        Debug.Print strnewquery
            ' Create the recordset
            Set rs = db.OpenRecordset(strnewquery)
            If rs.RecordCount > 0 Then
                DoCmd.OpenQuery "[qrySPReports_test_passthru]", acViewNormal, strSQL
                DoCmd.Close acForm, "frmREPORTBUILDER"
            Else
                MsgBox "There are no records that match your criteria! Please select new criteria.", vbInformation
                Exit Sub
            End If
            DoCmd.Minimize
            stDocName = "[qrySPReports_test_passthru]"
            DoCmd.OpenQuery "[qrySPReports_test_passthru]", acViewNormal, strSQL
            DoCmd.Close acForm, "frmREPORTBUILDER"
                stDocName = "[qrySPReports_test_passthru]"
                DoCmd.OpenQuery stDocName, acPreview
                DoCmd.OutputTo acOutputQuery, [qrySPReports_test_passthru], acFormatXLS
                DoCmd.Close "[qrySPReports_test_passthru]"
    Exit_cmdExport_Click:
        Exit Sub
    Err_cmdExport_Click:
        Select Case Err.Number
            Case 2501 'OpenQuery action was cancelled
                Resume Exit_cmdExport_Click
            Case Else
                MsgBox Err.Description
                Resume Exit_cmdExport_Click
                Resume
            End Select
    End Sub


  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    On what line? What is the result of


    Debug.Print strnewquery
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    It look like you are missing an "End Select" statement:

    Code:
        Select Case Me.Module
        Case SP
           strnewquery = "[qrySPReports_test_passthru]"
        Case LTL
            strnewquery = "[qryLTLReports_test_passthru]"
        Case DTF
            strnewquery = "[qryDTFReports_test_passthru]"
        End Select 

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I didn't notice that, but presumably that's a cut/paste oversight. If it wasn't there, I expect you'd get a compile error, not the parameters error.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    thank you for responding. The debug print didn't error. I stepped through the code and it errored out at Set rs = db.OpenRecordset(strnewquery).
    Thanks
    And; yes end statement go lost in the copy and paste. Thanks

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You must not realize what that's for:

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Thanks for the reminder. Guess Im frustrated with this code not working when I've used it before. I made some corrections to the code when I reread the case statement and now The error is The microsoft access database engine cannot find the input table or query"WHERE ([MONTHPROCESSED] BETWEEN JUNE And JULY);" This is a date column. I selected SP for the module and June and July dates for the records to be returned. I'm still missing something but what. Thanks

    Code:
    Private Sub cmdExport_Click()
    On Error GoTo Err_cmdExport_Click
    Dim strSQL As String, intCounter As Integer
    Dim ctl As Control, strname As String, strnewquery As String
    Dim strRptSel As String
    Dim stMessage As String
    Set db = CurrentDb
          'Build SQL String
          For Each ctl In Me.Form
          If ctl.Tag = "input" Then
          'strname = "me." & ctl.Name
          If ctl.Value > "" Then
          strSQL = strSQL & "[" & ctl.Name & "] " & " like " & Chr(34) & ctl.Value & Chr(34) & " And "
          End If
          End If
         Next ctl
        
        ' Set the value of the parameter.
        Select Case Me.Module
        Case SP
           strnewquery = "[qrySPReports_test_passthru]"
        Case LTL
            strnewquery = "[qryLTLReports_test_passthru]"
        Case DTF
            strnewquery = "[qryDTFReports_test_passthru]"
        End Select
        
        If Me.cboFROM & vbNullString <> "" And Me.cboTO & vbNullString <> "" Then
            strSQL = strSQL & " ([MONTHPROCESSED] BETWEEN " & Me.cboFROM & " And " & Me.cboTO & ") And "
        End If
        
        'strnewquery = "Select qrySPReports_test_passthru.* FROM qrySPReports_test_passthru"
        If strSQL <> "" Then
            strSQL = Left(strSQL, (Len(strSQL) - 5))
            strnewquery = strnewquery & " WHERE " & strSQL & ";"
        End If
        Debug.Print strnewquery
            ' Create the recordset
            Set rs = db.OpenRecordset(strnewquery)
            If rs.RecordCount > 0 Then
                DoCmd.OpenQuery strnewquery, acViewNormal, strSQL
                DoCmd.Close acForm, "frmREPORTBUILDER"
            Else
                MsgBox "There are no records that match your criteria! Please select new criteria.", vbInformation
                Exit Sub
            End If
            DoCmd.Minimize
            stDocName = strnewquery
            DoCmd.OpenQuery strnewquery, acViewNormal, strSQL
            DoCmd.Close acForm, "frmREPORTBUILDER"
                stDocName = strnewquery
                DoCmd.OpenQuery stDocName, acPreview
                DoCmd.OutputTo acOutputQuery, strnewquery, acFormatXLS
                DoCmd.Close strnewquery
    Exit_cmdExport_Click:
        Exit Sub
    Err_cmdExport_Click:
        Select Case Err.Number
            Case 2501 'OpenQuery action was cancelled
                Resume Exit_cmdExport_Click
            Case Else
                MsgBox Err.Description
                Resume Exit_cmdExport_Click
                Resume
            End Select
    End Sub
    Last edited by slimjen; 07-19-2012 at 07:20 PM. Reason: forgot to post rest of the error

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Do you not want to post the SQL returned by the debug statement?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    It states: The microsoft access database engine cannot find the input table or query"WHERE ([MONTHPROCESSED] BETWEEN JUNE And JULY)

    The query is a pass thru query and "MONTHPROCESSED" is an alias: MONTHPROCESSED: Format([dteSHIPDTE],"mmmm yyyy")

    Maybe this is causing the errors? The query runs fine outside the form. I need to be able to select the time period without a popup asking for the date.
    Thanks so much

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    That's what the error is. I'm asking what SQL is being printed to the Immediate window.

    On another front, I don't think a date in that format is a good way to filter. alphabetic sorting will be problematic I would think.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    I apologize if Im not following. I am copy/pasting out of the immediate window:"WHERE ([MONTHPROCESSED] BETWEEN JUNE And JULY)
    This is what is being printed.

    Should I just have a popup window for the date then? What do you suggest to get the result I am after?
    Thanks so much for helping.

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Okay, then as you can see you're not building a complete, valid SQL statement for the recordset. You appear to have commented out the line that would have started the statement.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    I was trying to get the code to work. My original code that worked for me was built with no case statement( only one division) and for a report. Now I have multiple divisions(modules to choose and a query based on the module) I go turned around on how to write the code to include this and multiple years to choose from also. I need help as you can see to get this to work. Thanks so much for sticking with this. Any suggestions would be most appreciated.
    Thanks

  14. #14
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I looked at your code and made a few changes.
    Not sure what you are trying to do; seeing the original code code would help.
    Here is what I have:
    Code:
    Private Sub cmdExport_Click()
       On Error GoTo Err_cmdExport_Click
    
       Dim db As DAO.Database
       Dim RS As DAO.Recordset
    
       Dim strSQL As String, intCounter As Integer
       Dim ctl As Control, strname As String, strCriteria As String
       Dim strRptSel As String
       Dim stMessage As String
    
       Set db = CurrentDb
    
       'Build SQL String
       Select Case Me.Module
          Case SP
             strnewquery = "[qrySPReports_test_passthru]"
          Case LTL
             strnewquery = "[qryLTLReports_test_passthru]"
          Case DTF
             strnewquery = "[qryDTFReports_test_passthru]"
       End Select
       'create select clause
       strSQL = "Select " & strnewquery & ".* FROM " & strnewquery
       'strnewquery = "Select qrySPReports_test_passthru.* FROM qrySPReports_test_passthru"
    
       For Each ctl In Me.Form
          If ctl.Tag = "input" Then
             If ctl.Value > "" Then
                strCriteria = strCriteria & "[" & ctl.Name & "] " & " like " & Chr(34) & ctl.Value & Chr(34) & " And "
             End If
          End If
       Next ctl
    
       ' Set the value of the parameter.
       If Me.cboFROM & vbNullString <> "" And Me.cboTO & vbNullString <> "" Then
          strCriteria = strCriteria & " ([MONTHPROCESSED] BETWEEN " & Me.cboFROM & " And " & Me.cboTO & ") And "
       End If
    
       If strCriteria <> "" Then
          strCriteria = Left(strCriteria, (Len(strCriteria) - 5))
          strSQL = strSQL & " WHERE " & strCriteria & ";"
       End If
    
       Debug.Print strSQL
    
       ' Create the recordset
    
       '************************
       'not sure what you are trying to do here
       'why open a record set AND open the query
       '************************
       Set RS = db.OpenRecordset(strnewquery)
       If RS.RecordCount > 0 Then
          DoCmd.OpenQuery strSQL, acViewNormal
          DoCmd.Close acForm, "frmREPORTBUILDER"
    
          DoCmd.Minimize
          'opened the query a second time???
          stDocName = strnewquery
          DoCmd.OpenQuery strSQL, acViewNormal
          DoCmd.Close acForm, "frmREPORTBUILDER"
    
          'don't need to open the query before outputting
          stDocName = strnewquery
          DoCmd.OpenQuery stDocName, acPreview
    
          DoCmd.OutputTo acOutputQuery, strSQL, acFormatXLS
          DoCmd.Close acQuery, stDocName
    
       Else
          MsgBox "There are no records that match your criteria! Please select new criteria.", vbInformation
       End If
    
    
       RS.Close
       Set RS = Nothing
       Set db = Nothing
    
    Exit_cmdExport_Click:
       Exit Sub
    Err_cmdExport_Click:
       Select Case Err.Number
          Case 2501   'OpenQuery action was cancelled
             Resume Exit_cmdExport_Click
          Case Else
             MsgBox Err.Description
             Resume Exit_cmdExport_Click
             Resume
       End Select
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Too few parameters. expected 1.
    By seth1685 in forum Programming
    Replies: 1
    Last Post: 01-11-2012, 08:08 AM
  2. 3061 Error. Too few parameters. Expected 1.
    By rghollenbeck in forum Queries
    Replies: 5
    Last Post: 09-28-2011, 12:12 PM
  3. Too few parameters expected 4
    By forestgump in forum Programming
    Replies: 1
    Last Post: 04-15-2011, 09:10 AM
  4. Run-time '3051' - Too few parameters. Expected 1
    By jgelpi16 in forum Programming
    Replies: 3
    Last Post: 04-11-2011, 07:17 AM
  5. Too few parameters. Expected 2.
    By PPCMIS2009 in forum Access
    Replies: 0
    Last Post: 01-28-2009, 01:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums