Object variable or with block not set error

M

Mo

The code below is taken from http://www.lebans.com/rownumber.htm. The code
is meant to allow row-numbering inside a query by means of an expression.

When I use the code though I get a number of errors.

Firstly, I've had to change the Dim statement to 'Dim rs As DAO.Recordset'.

When I run the query I get a run-time error 91 message, which is basically
pointing to the 'rs.close' line. If I rem this line out, the code runs ok
but does not perform the row numbering as it should (the expression
calculates the row number as '0' for all rows in the query).

Does anyone know why the function is not working?

Thanks for any help.


Code:
Function Serialize(qryname As String, keyname As String, keyvalue) As Long

Dim rs As Recordset


On Error GoTo Err_Serialize

Set rs = CurrentDb.OpenRecordset(qryname, dbOpenDynaset, dbReadOnly)

rs.FindFirst Application.BuildCriteria(keyname, rs.Fields(keyname).Type,
keyvalue)

Serialize = Nz(rs.AbsolutePosition, -1) + 1


Err_Serialize:

rs.Close

Set rs = Nothing

End Function
 
K

Kailash Kalyani

I think chances are that your openrecordset command may have failed so rs is
set to nothing. Comment out the ON error line and fix the error with the
parameters and put the line back in again.

Hope that helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top