I am using the following code to test a Function which will return a fixed number of data values from a Table recordset.
However when I run the Test() which calls the Function in question passing some test data, I get the error message: "runtime error 3420. Object invalid or no longer set". I am sure I am doing something really stupid but I can't pin down what is wrong (not an expert user!).
Can anyone help?
======================
Function Test()
Dim PreviousTenancy() As Variant, i As Integer
PreviousTenancy = Get_Previous_Tenancy("11FOS5", "000355")
For i = 0 To 1
MsgBox PreviousTenancy(i)
Next i
End Function
Function Get_Previous_Tenancy(nPropRef As String, nCurrentAccRef As String) As Variant()
Dim dbs As Database, xTenancy As Recordset, nSQL As String
Set dbs = CurrentDb()
nSQL = "SELECT Property.PRef, Property.PAddress1, Rent_Book.AccRef, Rent_Book.TenancyStart, Tenants.TName " & _
"FROM (Property INNER JOIN Rent_Book ON Property.PRef = Rent_Book.PRef) INNER JOIN Tenants ON " & _
"Rent_Book.AccRef = Tenants.AccRef WHERE (((Property.PRef)='" & nPropRef & "'));"
Set xTenancy = dbs.OpenRecordset(nSQL)
If xTenancy.EOF Then GoTo nExit
xTenancy.MoveFirst
Do Until xTenancy!AccRef = nCurrentAccRef: xTenancy.MoveNext: Loop
If xTenancy.RecordCount = 1 Then MsgBox "No Earlier Tenancy Exists": GoTo nExit
xTenancy.MovePrevious
Get_Previous_Tenancy = Array(xTenancy!AccRef, xTenancy!TenancyStart)
nExit:
xTenancy.Close
End Function