FoxProProgrammer
Programmer
Hi folks,
I am helping a colleague with his database. He designed the database, tables, queries, etc, and I am just trying to help him implement a Median function within his structure. I wrote the Median function (code below), and tested it with a small table that I created. It works; however, when I use the function in a query in his database I get run-time error 3061: Too few parameters. Expected 2.
Here is a summary of the structure of his database:
TableA - Resides on a network drive.
QueryA - Retrieves data from TableA. One field in QueryA is a calculated field, derived as follows:
Select TableA.fld1 - TableA.fld2 As calcfld1 ... Between Forms!Form1!fld3 And Forms!Form1!fld4
Note that fld3 and fld4 create a parameter query. Therein lies the problem, I think, which is why I created a form to enter that data. Before I created a form, Access would ask the user to enter the data, and I thought that I could fix the problem by entering the data on a form. No such luck.
QueryB - Retrieves data from QueryA and calculates various aggregate functions, one of which is my Median function:
Select Median("calcfld1", "QueryA") As MedianCalcfld ...
I have done the following to try to resolve this problem:
* Checked the spelling of "fldname" and "tblname" in the Median function call to make sure that they are correct.
* Ran the debugger to try to look at the record set, which I can't do because the program bombs when it gets to the statement where it creates the record set. (Set rs = ...)
* Copied the query string (strQuery) created in the Median function to the query builder and ran the query. It worked there, so the SQL is good.
* Looked in QueryA to make sure that fldname exists. It does.
Ok, I thought that maybe the two parameters that it was looking for are fld3 and fld4, which are the two parameters required to run QueryA. In an attempt to resolve this, I created a form that contains two text boxes and a command button. One text box is named fld3 and the other is named fld4. When the command button is pressed, the OnClick procedure runs QueryB as follows:
This part runs ok, so I think that it recognizes the two flds on my Form (fld3 and fld4). Just for grins, I renamed them but left the Between clause in QueryA the same. As expected, Access asked me to enter the values so it could run QueryA.
I apologize for the length of this message, but I wanted to describe the problem and what I have tried to do to resolve it. This problem has me stumped. Anyone see what I am doing wrong?
Thanks so much for your time!
dz
dzaccess@yahoo.com
I am helping a colleague with his database. He designed the database, tables, queries, etc, and I am just trying to help him implement a Median function within his structure. I wrote the Median function (code below), and tested it with a small table that I created. It works; however, when I use the function in a query in his database I get run-time error 3061: Too few parameters. Expected 2.
Code:
Public Function Median(fldname As String, tblname As String) As Single
Dim strQuery As String
Dim db As DAO.Database
Dim rs2 As DAO.Recordset
Dim numRecs, dataPt As Integer
' Get the data and sort it.
strQuery = "Select [" & fldname & "] From [" & tblname & "] Where NOT IsNull([" & fldname & "]) Order by [" & fldname & "];"
Set db = CurrentDb
Set rs2 = db.OpenRecordset(strQuery, dbOpenDynaset, dbReadOnly)
' Count the number of records and move to the beginning of record set.
rs2.MoveLast
numRecs = rs.RecordCount
rs2.MoveFirst
dataPt = Int((numRecs + 1) / 2)
' Find the Median
If dataPt Mod 2 = 0 Then
' Even number of records. Average the middle two values.
rs2.Move dataPt - 1
Median = rs.Fields(fldname)
rs2.MoveNext
Median = (Median + rs2.Fields(fldname)) / 2
Else
' Odd number of records.
rs2.Move dataPt - 1
Median = rs2.Fields(fldname)
End If
End Function
Here is a summary of the structure of his database:
TableA - Resides on a network drive.
QueryA - Retrieves data from TableA. One field in QueryA is a calculated field, derived as follows:
Select TableA.fld1 - TableA.fld2 As calcfld1 ... Between Forms!Form1!fld3 And Forms!Form1!fld4
Note that fld3 and fld4 create a parameter query. Therein lies the problem, I think, which is why I created a form to enter that data. Before I created a form, Access would ask the user to enter the data, and I thought that I could fix the problem by entering the data on a form. No such luck.
QueryB - Retrieves data from QueryA and calculates various aggregate functions, one of which is my Median function:
Select Median("calcfld1", "QueryA") As MedianCalcfld ...
I have done the following to try to resolve this problem:
* Checked the spelling of "fldname" and "tblname" in the Median function call to make sure that they are correct.
* Ran the debugger to try to look at the record set, which I can't do because the program bombs when it gets to the statement where it creates the record set. (Set rs = ...)
* Copied the query string (strQuery) created in the Median function to the query builder and ran the query. It worked there, so the SQL is good.
* Looked in QueryA to make sure that fldname exists. It does.
Ok, I thought that maybe the two parameters that it was looking for are fld3 and fld4, which are the two parameters required to run QueryA. In an attempt to resolve this, I created a form that contains two text boxes and a command button. One text box is named fld3 and the other is named fld4. When the command button is pressed, the OnClick procedure runs QueryB as follows:
Code:
Private Sub Command1_Click()
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Set db = CurrentDb
Set rs1 = db.OpenRecordset("QueryB", dbOpenDynaset, dbReadOnly)
This part runs ok, so I think that it recognizes the two flds on my Form (fld3 and fld4). Just for grins, I renamed them but left the Between clause in QueryA the same. As expected, Access asked me to enter the values so it could run QueryA.
I apologize for the length of this message, but I wanted to describe the problem and what I have tried to do to resolve it. This problem has me stumped. Anyone see what I am doing wrong?
Thanks so much for your time!
dz
dzaccess@yahoo.com