473,396 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Error 3061. Too few parameters, expected 1 running a function.

Hi all,

I have a function in order to amend some data in a table.

Basically I have a list of names and a state column with T or F in it. I have sorted by name and the last state for each name should be F. The function groups the names first in a recordset, and then if the state for the record above is the same (as in 2 consecutive T's) it should delete it. Otherwise, it should exit the function.

Here is the function so far

Expand|Select|Wrap|Line Numbers
  1. Public Function UpdateTable()
  2.     On Error GoTo EH:
  3.         Dim db As Database
  4.         Dim rstNames As Recordset
  5.         Dim rstCallOuts As Recordset
  6.         Dim strSQL As String
  7.         Dim dtmFirstCallout As Date
  8.         Set db = CurrentDb()
  9.         strSQL = "SELECT name1 " & _
  10.             "FROM table1 " & _
  11.             "GROUP BY name1 " & _
  12.             "HAVING name1 <> 'N/A';"
  13.         Set rstNames = db.OpenRecordset(strSQL, dbOpenDynaset)
  14.         If Not rstNames.RecordCount = 0 Then
  15.             rstNames.MoveFirst
  16.             Do While Not rstNames.EOF
  17.                 strSQL = "SELECT ID, name1, state1, time1 " & _
  18.                     "FROM table1 " & _
  19.                     "WHERE name1 = '" & rstNames!Name1 & "' " & _
  20.                     "ORDER BY TimeStamp;"
  21.                 Set rstCallOuts = db.OpenRecordset(strSQL, dbOpenDynaset)
  22.                 If Not rstCallOuts.RecordCount = 0 Then
  23.                     rstCallOuts.MoveFirst
  24.                     dtmFirstCallout = rstCallOuts!TimeStamp
  25.                     rstCallOuts.MoveNext
  26.                     Do While Not rstCallOuts.EOF
  27.                         If (DLookup("[state1]", "table2", "[ID]=" & "[ID]" - 1) <> "[state1]") Then
  28.                         Exit Function
  29.                         Else
  30.                             With rstCallOuts
  31.                                 .Delete
  32.                             End With
  33.                         End If
  34.                         rstCallOuts.MoveNext
  35.                     Loop
  36.                 End If
  37.                 rstNames.MoveNext
  38.             Loop
  39.         End If
  40.         Exit Function
  41. EH:
  42.         MsgBox Err.Number & ":  " & Err.Description
  43.         Exit Function
  44.     End Function
  45.  
I have tried a number of different things however none seem to be working. A few different sites have similar problems and enclosing the WHERE statement in ' ' helps however it was already done.

Any ideas anyone?

Thanks in advance.

Gareth
Aug 25 '14 #1
19 1685
twinnyfo
3,653 Expert Mod 2GB
Gareth,

I think what you need to do is establish another variable to hold the values you are trying to evaluate in your If...Then in Line 27. Otherwise, your code will never evaluate as true (as it is written).

Instead of setting a varaible for the TimeStamp (which you never use, have a variable for State1. Then compare the next record's value for State1 with that variable.

I think that should get you moving in the right direction.
Aug 25 '14 #2
Thanks for replying. Am I right in saying I need to replace timestamp in line 24 with state1 so that it reads
Expand|Select|Wrap|Line Numbers
  1.  dtmFirstCallout = rstCallOuts!State1 
and replace line 27 so that it reads
Expand|Select|Wrap|Line Numbers
  1.   If (DLookup("[state1]", "table2", "[ID]=" & "[ID]" - 1) <> dtmFirstCallout) Then   
I have tried this and I get the same error unfortunately :(

Thanks
Aug 25 '14 #3
twinnyfo
3,653 Expert Mod 2GB
Well, there are several issues with your code (particularly line 27).

First, I don't know what type of data "State1" is. Let's assume an integer for the following re-do of your code (but please modify as required for your actual data):

Expand|Select|Wrap|Line Numbers
  1. Public Function UpdateTable() 
  2.     On Error GoTo EH: 
  3.         Dim db As Database 
  4.         Dim rstNames As Recordset 
  5.         Dim rstCallOuts As Recordset 
  6.         Dim strSQL As String 
  7.         Dim intState1 As Integer 
  8.         Set db = CurrentDb() 
  9.         strSQL = "SELECT name1 " & _ 
  10.             "FROM table1 " & _ 
  11.             "GROUP BY name1 " & _ 
  12.             "HAVING name1 <> 'N/A';" 
  13.         Set rstNames = db.OpenRecordset(strSQL, dbOpenDynaset) 
  14.         If Not rstNames.RecordCount = 0 Then 
  15.             rstNames.MoveFirst 
  16.             Do While Not rstNames.EOF 
  17.                 strSQL = "SELECT ID, name1, state1, time1 " & _ 
  18.                     "FROM table1 " & _ 
  19.                     "WHERE name1 = '" & rstNames!Name1 & "' " & _ 
  20.                     "ORDER BY TimeStamp;" 
  21.                 Set rstCallOuts = db.OpenRecordset(strSQL, dbOpenDynaset) 
  22.                 If Not rstCallOuts.RecordCount = 0 Then 
  23.                     rstCallOuts.MoveFirst 
  24.                     intState1 = rstCallOuts!State1 
  25.                     rstCallOuts.MoveNext 
  26.                     Do While Not rstCallOuts.EOF 
  27.                         If rstCallOuts!State1 <> intState1 Then 
  28.                             intState1 = rstCallOuts!State1
  29.                         Else 
  30.                             With rstCallOuts 
  31.                                 .Delete 
  32.                             End With 
  33.                         End If
  34.                         rstCallOuts.MoveNext 
  35.                     Loop 
  36.                 End If 
  37.                 rstNames.MoveNext 
  38.             Loop 
  39.         End If 
  40.         Exit Function 
  41. EH: 
  42.         MsgBox Err.Number & ":  " & Err.Description 
  43.         Exit Function 
  44.     End Function
However, there will still be problems with this code, as Your line 28 assumes only one iteration, but it appears from your origianl post that you want to cycle through all records. Note the replacement of line 28 above, which then re-assignes the value of intState1 (which would then assume the deletion of line 28)

Again, there may be additional challenges with this code, as what if there are three consecutive records that have hte sate State1?

Without additional information on everything your Tables have in them and how this is intended to be used, this is about all I can offer at this time.

I hope this helps (and I hope the code isn't fat-fingered with syntax errors).
Aug 25 '14 #4
I have tried the code above however I am still getting the same error that there were too few parameters :(

The background is the table records the times someone is called out. The state column contains the letter T or F. T indicates when they were called out (the date column records the time) F indicates the end of the call out in the next record. I need to calculate the duration between each T and F. It would be plain sailing if each of the records were in sequential order in that it was like T,F,T,F,T,F,T,F etc however occasionally its is recorded incorrectly and its T,T,F. I need to remove any instances where there is 2 or more T's together so that there is only the 1st T and the F of that particular call out.

I can do that easily in a query using:
IIf(DLookUp("[state1]","table2","[ID]=" & [ID]-1)=[state1],"N/A",[state1])

However that only works if the previous person ended on a F, otherwise if the F was not recorded for that person it takes the T for the previous person and the next F from the subsequent person.

Hope this makes sense :) I have tried subqueries and this function as I believe I need to remove the additional T's for each person individually to prevent the issue above.

Thanks :)
Aug 25 '14 #5
twinnyfo
3,653 Expert Mod 2GB
In addition to correcting the current data, may I also recommend coding the prevention of such errors....

It is difficult to understand the entire scope of your DB from your description, but have you considered the value of having a field representing the "T" and a separate field in the same record representing the "F"? This would make each "callout" a single record--thus preventing the need for a separate record for each instatnce.

This of course, assumes that every "T" has corresponding "F", and thaaat the information unique to each "T" is also relevant (and unchanged) to the corresponding "F". I hope this made sense. It made sense when I initially started typing it.....
Aug 25 '14 #6
twinnyfo
3,653 Expert Mod 2GB
BUT, this still does not get your code executing properly. At which line of the code are you getting that error? This may help us to pinpoint how to correct the initial problem and work forward from there!

We want to be able to automate your corrections!
Aug 25 '14 #7
I appreciate your help so far. I have stepped through all the code and and it doesnt fail until the function is finished. I did notice however when stepping through, on line 21 it says rstcallouts = nothing when I hover over it, and line 22 says rstcallouts.recordcount = <object variable or with block variable not set>

It appears to me that the recordset has nothing in it to work with at the moment. All the lines seem ok and populated with data until line 21.
Aug 25 '14 #8
twinnyfo
3,653 Expert Mod 2GB
Gareth,

Keep in mind that when your debugger is currently ON line 21, that rstCallOuts has NOT been set. The debugger pauses prior to executing the line.

I am certain that when the debugger pauses on line 22, that rstCallOuts will no longer be empty. Likewise, on line 23, RecordCount will now give you a value.

What is strange, however, is that your code does not break until after the function finishes???

This--at least on the surface--indicates there is an error somewhere else in your code (but not in this function). Have you continued to step through your code line by line (F8) to ensure the error is firing on this procedure?
Aug 25 '14 #9
I have done some more testing and those 2 lines still say the same after passing past them. It jumps straight from line 21 to 42. It then gives the error, and after clicking ok, it goes to line 43 and then starts again. This is the only code and only function in the DB. It is based on a simple table and not a query. Once it works, I can copy the function to the real DB. I may end up having to delete the records I don't need manually however its not really practical once the amount of records gets high :)
Aug 25 '14 #10
twinnyfo
3,653 Expert Mod 2GB
Try adding between Lines 20 and 21:

Expand|Select|Wrap|Line Numbers
  1. Debug.Print strSQL
Then paste the result that shows up in the Immediate Window.
Aug 25 '14 #11
Stewart Ross
2,545 Expert Mod 2GB
Two comments on what I see here. Firstly, as a possible explanation for jumping out of your record set with a too few parameters error you need to qualify all recordset definitions as DAO types. For example,

Expand|Select|Wrap|Line Numbers
  1. Dim rstNames as DAO.Recordset
ADO recordset definitions are likely to be expected if you do not qualify the type, and crucially their argument lists are not the same as the DAO calls, which is the most likely explanation for the parameter error and the jump to the error handler without executing the set recordset statement.

My second comment is that your relational design is not right in this example. You are trying to recover call out states with no details of the call out itself recorded. You are looping manually and deleting states to make up for the missing relationship. You will never get this to work effectively.

You have also got a problem with choice of primary key for your person table - names are not unique and cannot be used as keys in this way. My strong advice is to read up on relational table design, as your missing design relations are the real problem here. If you had a call out entity defined you would be able to identify the specific callouts attended by each person then group on the states for each person without code at all.

-Stewart

Ps in case you think it a bit strong to mention the lack of relational design, what you have mentioned before about two consecutive T states and having to delete one and so on is indicating the same thing. Ignoring the internal use of your time stamp field, which as Twinnyfo indicated is not intrinsic to your application, and your ID field, which is not a candidate key in the way it is being used, you are allowing duplicate rows to be stored for each person, in violation of relational requirements that rows must be unique.
Aug 25 '14 #12
I have tried inserting the debug line between 20 and 21 however its still jumping to the end and erroring. I tried making it a DAO recordset as well however its still doing the same. I believe Stewart has hit the nail on the head in that I should be able to do this with a subquery however I couldnt get the syntax right. The ID is the primary key in the table and each person has a unique ref number too which I can group on. I will have another go at that today :)

Appreciate all your help. I will report back if I get any success :)

Thanks
Aug 26 '14 #13
twinnyfo
3,653 Expert Mod 2GB
So, the generation of the SQL string in lines 17-20 is causing the error? That almost seems impossible, because if there was a problem with rstNames (or if it was empty) then those lines would not execute in the first place.

This one is starting to not make any sense at all.
Aug 26 '14 #14
twinnyfo
3,653 Expert Mod 2GB
@Stewart,

I have never explicitly declared my Recordsets as DAO recordsets and have never run across any ambiguity issues. I think there may be some situations in which it can cause problems, but I have not run across any. In this case, there may be some issues, but as I consider the error being generated, I wonder if we both are looking in the wrong place....

@Gareth,

In the Field "Name1" are these pure text (alpha-numeric) fields ONLY? By this, I mean are there any entries in the "Name1" field which include either dashes or apostrophes? I had a query once that was trying to look for names and it worked perfectly for years until I entered a name that had an apostrophe--then it broke and gave me (I think) the same error.

Please review your data. I agree with Stewart, however, that your data is not properly normalized. You should never search on a "Name," but on the index which points to that name. Lack of proper normalization may be the primary cause in this instance.
Aug 26 '14 #15
jimatqsi
1,271 Expert 1GB
Gareth,
Look at your data. This doesn't nullify anything twinnyfo and Stewart have said, but I suspect you are encountering a name that has a quote within the name. You are not handling that possibility and when it happens it gives you a bad SQL string and the behavior you are seeing.

Jim
Aug 26 '14 #16
jimatqsi
1,271 Expert 1GB
As I reread these posts it came to me that you are saying the failure happens every time, no matter what the data is. Or maybe you are using the same data every time, in which case it is possible the first name has a single-quote in it. But that seems unlikely to me.

So for sure you post the SQL code as Twinnyfo requested. Let's see what that code really looks like.
Aug 26 '14 #17
Thanks all for continuing to look into this.

After a relatively small amount of manual work, I have the table where I need it, and have been able to workout the duration etc and the report is now effectively finished.

It would still be good to see where I have gone wrong with this function however so below is the current code. I am now using the employee reference instead of the name, which is a reference no. with no spaces, dashes etc.

Expand|Select|Wrap|Line Numbers
  1.             Public Function UpdateTable()
  2.     On Error GoTo EH:
  3.         Dim db As Database
  4.         Dim rstNames As DAO.Recordset
  5.         Dim rstCallOuts As DAO.Recordset
  6.         Dim strSQL As String
  7.         Dim intState1 As Integer
  8.         Set db = CurrentDb()
  9.         strSQL = "SELECT emp_ref " & _
  10.             "FROM table2 " & _
  11.             "GROUP BY emp_ref " & _
  12.             "HAVING emp_ref <> 'N/A';"
  13.         Set rstNames = db.OpenRecordset(strSQL, dbOpenDynaset)
  14.         If Not rstNames.RecordCount = 0 Then
  15.             rstNames.MoveFirst
  16.             Do While Not rstNames.EOF
  17.                 strSQL = "SELECT * " & _
  18.                     "FROM table2 " & _
  19.                     "WHERE emp_ref = '" & rstNames!emp_ref & "' " & _
  20.                     "ORDER BY TimeStamp;"
  21.                     Debug.Print strSQL
  22.                 Set rstCallOuts = db.OpenRecordset(strSQL, dbOpenDynaset)
  23.                 Debug.Print strSQL
  24.                 If Not rstCallOuts.RecordCount = 0 Then
  25.                 Debug.Print strSQL
  26.                     rstCallOuts.MoveFirst
  27.                     intState1 = rstCallOuts!State1
  28.                     rstCallOuts.MoveNext
  29.                     Do While Not rstCallOuts.EOF
  30.                         If rstCallOuts!State1 <> intState1 Then
  31.                             intState1 = rstCallOuts!State1
  32.                         Else
  33.                             With rstCallOuts
  34.                                 .Delete
  35.                             End With
  36.                         End If
  37.                         rstCallOuts.MoveNext
  38.                     Loop
  39.                 End If
  40.                 rstNames.MoveNext
  41.             Loop
  42.         End If
  43.         Exit Function
  44. EH:
  45.         MsgBox Err.Number & ":  " & Err.Description
  46.         Exit Function
  47.     End Function 

The table only has 5 fields, ID, emp_ref, employee_name, state, and date. I have put a few debug lines in to try and catch the error but makes no difference.

Thanks all again
Aug 26 '14 #18
Stewart Ross
2,545 Expert Mod 2GB
I ran a test based on adapting your lines 9-12 to a calendar table of mine with no problems at all in setting the recordset.

Expand|Select|Wrap|Line Numbers
  1.     Dim strSQL As String
  2.     Dim db As DAO.Database
  3.     Dim rs As DAO.Recordset
  4.     strSQL = "SELECT fiscal_qtr " & _
  5.               "FROM Calendar " & _
  6.               "GROUP BY fiscal_qtr " & _
  7.               "HAVING fiscal_qtr <> 'N/A';"
  8.     Set db = CurrentDb
  9.     Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
As this assigned the recordset as expected and with no error I cannot understand what could be going wrong. As has been mentioned, we'd need to see what strSQL contains. If you debug.print it you can then copy the SQL and paste it into the SQL view of the query editor. If the same SQL runs from there I'm at a loss to suggest what else could be going wrong.

@Twinnyfo the DAO qualification applied in particular to Office 2003 versions of Access, where ADO was for a time the default type of recordset instead of DAO. From Office 2007 onwards this was reversed back to DAO being the default, and indeed ADO types were superseded by .NET versions that have no relevance to Access.

The ADO/DAO difference when not explicitly declared caused endless trouble at the time. As the OP did not tell us (nor did we ask) what version of Access was in use I have stuck with a practice of qualification of the type of recordset that at least ensures that it is indeed the right type of recordset that has been declared.

-Stewart
Aug 26 '14 #19
twinnyfo
3,653 Expert Mod 2GB
@Stewart

Thanks for the info on the history of the ADO/DAO issue. That helps me to understand some of the problems that could arise. I appreciate your sharing this background for us!
Aug 26 '14 #20

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

Similar topics

2
by: Steve Richfield | last post by:
There have been LOTS of postings about error 3061, but mine seems to be an even simpler case than the others. I have a simple **FUNCTIONING** query called qryEdits. Copying the SQL from the query,...
4
by: Richard Hollenbeck | last post by:
I thought I was very specific in this SQL request. There is a form open with a selected record (and a corresponding "lngRecipeID" on that form. The table also has a field called "lngRecipeID". ...
4
by: astri | last post by:
#include "Unit1.h" #include "math.h" #include "fixed_math.hpp" #include "algorithm.h" #define MBIT 0x4000 #define CBIT 16 long constbl; void __fastcall TForm1::Button1Click(TObject...
1
by: Richard Hollenbeck | last post by:
I wonder what I'm missing? I really feel like a retard because I've been screwing with some code for a very long time. I just must be missing something very simple. In the following example,...
3
by: phill86 | last post by:
Hi, I am trying to run the following query in a recordset and i get the following error message Runtime error 3061 - Too few parameters. Expected 1 i am using the following code
8
Cintury
by: Cintury | last post by:
The problem is I have a function that I've created and stored in a module. I call it as an expression (e.g. total: Function(parameter)). I'm receiving the error 3061: too few parameters, expected 1....
3
by: tasawer | last post by:
Hi, I need to add a new record to table "ACC_Vehicle", with three pieces of information. When I activate the code, I get the error "Error 3061, Too few parameters Expected 1" ACCVehicleID is...
6
by: dowlingm815 | last post by:
I am receiving error code too few parameters. Expected 1. I can't seem to find the issue. The error code occurs when on this line of code: Set rstTemp =...
7
by: bunchaCoconuts | last post by:
I'm trying to figure out why I'm getting this error. I'm relatively inexperienced. The nature of the following code is that a slightly different query is run whether or not the "ImportantDatesOnly"...
2
by: baruc308 | last post by:
I cant run this. please help! the error is run-time error 3061 too few parameters expected 1 in this line --------->> Set rs = db.OpenRecordset <---- below Public Function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.