473,785 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Too Few Parameters..!!! !!

31 New Member
I am trying to run the following code, where a query runs by taking some values from a form, then a recordset is intialized to count the records from the query result....

but each time i run, i get this error

Error 3061: Too Few Parameters.Expe cted 2

my code:


Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstEmployee As DAO.Recordset
  3. Dim AdminArea As String
  4. Dim Line As String
  5. Dim Refining As Integer
  6. Dim NAOilOpns As Integer
  7. Dim MSP As Integer
  8. Dim mysql As String
  9.  
  10.  
  11. Set MyDB = CurrentDb
  12. mysql = "SELECT [Admin Area].[admin Name], [Business Line].[Business Line  Name], [PTD Request].PTD_Request_Receive_date, [PTD Request].PTD_Status, Employee.[American?]FROM (([Business Line] INNER JOIN [Admin Area] ON [Business Line].[Business Line ID] = [Admin Area].[business line id]) INNER JOIN Employee ON [Admin Area].[admin ID] = Employee.[Admin id]) INNER JOIN [PTD Request] ON Employee.[Badge Number] = [PTD Request].[Badge Number] _
  13. WHERE ((([PTD Request].PTD_Request_Receive_date) Between [forms]![report]![start] And [forms]![report]![end]) AND (([PTD Request].PTD_Status)=[forms]![report]![Received from source]) AND ((Employee.[American?])=[forms]![report]![Yes]));"
  14.  
  15.  
  16. Set rstEmployee = MyDB.OpenRecordset(mysql)
  17.  
  18.  
  19. Refining = 0
  20. NAOilOpns = 0
  21. MSP = 0
  22.  
  23.  
  24. With rstEmployee
  25.  Do While Not .EOF
  26.  
  27. Admin = ![Admin Name]
  28. Line = ![Business Line  Name]
  29.  
  30. If Line = "E & P" Then
  31.  
  32.  
  33.          If Admin = "Refining" Then
  34.            Refining = Refining + 1
  35.        Else
  36.            If Admin = "M&SP" Then
  37.            MSP = MSP + 1
  38.            End If
  39.          End If
  40. Else
  41.     If Line = "R M & I" Then
  42.  
  43.       NAOilOpns = NAOilOpns + 1
  44.     End If
  45.  
  46.    End If
  47.  
  48.      .MoveNext
  49.  
  50.  Loop
  51. End With
  52. rstEmployee.Close
  53. Set rstEmployee = Nothing
  54.  
  55. [Reports]![report1]![received_ref] = Refining
  56. [Reports]![report1]![received_msp] = MSP
  57. [Reports]![report1]![received_total] = MSP + Refining
  58. [Reports]![report1]![received_oil] = NAOilOpns
  59.  
  60. End Sub

I am desperate to have this work...
Aug 26 '09 #1
1 1719
Stewart Ross
2,545 Recognized Expert Moderator Specialist
Problem is your use of direct form control references in your WHERE clause. These are not fields as such, and when run from VBA the SQL interpreter treats them as parameters with no values. The simplest solution is to include the values of the form controls, not references to the form control names, in the SQL string. As you are using controls representing dates, which can be tricky to compare, I am using Format below to bring them to the mm/dd/yyyy format required for ANSI SQL date comparisons. If your dates are already in this format you will not need the Format function to do so.

Expand|Select|Wrap|Line Numbers
  1. WHERE ((([PTD Request].PTD_Request_Receive_date) Between #" _
  2. & Format([forms]![report]![start], "mm/dd/yyyy") & "# And #" _
  3. & Format([forms]![report]![end], "mm/dd/yyyy") & "#) AND _
  4. (([PTD Request].PTD_Status) = '" & [forms]![report]![Received from source]) _ 
  5. & "' AND ((Employee.[American?])= " & [forms]![report]![Yes]& "));"
I am also assuming that your PTD-Status field is of type Text - if not you will not need the single quotes before and after the value shown above.

For an alternate approach to this kind of issue please see post # 8 of this thread: Access does not recognise valid field name.

-Stewart
Aug 26 '09 #2

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.