Object variable or with block not set error

  • Thread starter Thread starter Mo
  • Start date Start date
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
 
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!
 
Back
Top