Hi everybody,
My aim is to return a count on the number of records returned from a table, matching the user entry on a form field (the value for which I pass from the form field "preorder_no" to the variable "var_preorder_no" and then include the variable in the strSQL definition) so that an IF statement, on the form calling this function, would then evaluate the answer and perform the appropriate action.
Please peruse the following code and tell me what's wrong with creating this recordset. One thing I noticed was that if I randomly picked an order number from the table and explicitly state it in the strSQL variable, it then returns the records.
var_preorder_no
I get the following error: "Error 3061 has occurred. Too few parameters. Expected 1" and it seemingly doesn't recognise the "var_preorder_no" in the strSQL definition.
If there is an alternative method to return a record count to a variable, based on a form field, I'M ALL EYES .
Any comments will be appreciated (newbie to recordsets). "All is not as it seems"
My aim is to return a count on the number of records returned from a table, matching the user entry on a form field (the value for which I pass from the form field "preorder_no" to the variable "var_preorder_no" and then include the variable in the strSQL definition) so that an IF statement, on the form calling this function, would then evaluate the answer and perform the appropriate action.
Please peruse the following code and tell me what's wrong with creating this recordset. One thing I noticed was that if I randomly picked an order number from the table and explicitly state it in the strSQL variable, it then returns the records.
Code:
Function CountRecords(var_preorder_no As String) As Long
'this function counts the number of records in a query
Dim myDB As Database
Dim ctr As Integer, num As Integer
Dim myrs As Recordset
On Error GoTo points_Err:
strSQL = "SELECT * " _
& "FROM PreOrders " _
& "WHERE preorder_no = " &
Code:
& ";"
Set myDB = DBEngine.Workspaces(0).Databases(0)
Set myrs = myDB.OpenRecordset(strSQL)
If myrs.BOF = True And myrs.EOF = True Then
num = 0
Else
myrs.MoveFirst
myrs.MoveLast
num = myrs.RecordCount
End If
myrs.Close
CountRecords = num
Exit Function
points_Err:
MsgBox "Error " & Err & "has occurred " & vbCrLf & Error
Exit Function
End Function
I get the following error: "Error 3061 has occurred. Too few parameters. Expected 1" and it seemingly doesn't recognise the "var_preorder_no" in the strSQL definition.
If there is an alternative method to return a record count to a variable, based on a form field, I'M ALL EYES .
Any comments will be appreciated (newbie to recordsets). "All is not as it seems"