I am attempting to create a function that will determine the minimum and maximum dates within a patients length of stay. for example:
Patient StartDate EndDate
pat1 1/1/2009 1/3/2009
pat1 1/4/2009 1/6/2009
pat1 2/15/2009 2/20/2009
The result would be that one function gives the admit/min start date and the other gives the discharge/max end date. Therefore, pat1 would have two separate length of stays. pat1's first length of stay would be from 1/1/2009 to 1/6/2009 and pat1's second length of stay would be from 2/15/2009 to 2/20/2009. Right now I only have a function to return the patients admit date/beginning length of stay, but I am receiving the error code 3021: no current record. The odd part of this is that it will loop through until "i" is 33, then I receive the error. When I run the module, the debugger shows that it is erroring out at the line:
patient1 = rstSorted.Fields("patientName")
If anybody could help me it would be greatly appreciated.
Thanks,
Tyler
Patient StartDate EndDate
pat1 1/1/2009 1/3/2009
pat1 1/4/2009 1/6/2009
pat1 2/15/2009 2/20/2009
The result would be that one function gives the admit/min start date and the other gives the discharge/max end date. Therefore, pat1 would have two separate length of stays. pat1's first length of stay would be from 1/1/2009 to 1/6/2009 and pat1's second length of stay would be from 2/15/2009 to 2/20/2009. Right now I only have a function to return the patients admit date/beginning length of stay, but I am receiving the error code 3021: no current record. The odd part of this is that it will loop through until "i" is 33, then I receive the error. When I run the module, the debugger shows that it is erroring out at the line:
patient1 = rstSorted.Fields("patientName")
If anybody could help me it would be greatly appreciated.
Thanks,
Tyler
Code:
Public Function dateStart()
Dim db As Database
Dim rst1 As Recordset
Dim rstCount As Long
Dim i As Long
Dim patient1 As String
Dim patient2 As String
Dim endDateCheck As Date
Dim startDateCheck As Date
Dim rstSorted As Recordset
Set db = CurrentDb
Set rst1 = db.OpenRecordset("MyRecords", dbOpenDynaset)
rst1.Sort = "patientName"
Set rstSorted = rst1.OpenRecordset()
rstSorted.MoveLast
rstSorted.MoveFirst
rstCount = rstSorted.RecordCount
i = 1
Do While Not rstSorted.EOF Or rstSorted.BOF
rstSorted.Move i
'this is where it errors out
'********************************
patient1 = rstSorted.Fields("patientName")
'********************************
rstSorted.MoveNext
patient2 = rstSorted.Fields("patientName")
rstSorted.Move i
If patient1 = patient2 Then
endDateCheck = rstSorted.Fields("endDate")
rstSorted.MoveNext
startDateCheck = rstSorted.Fields("startDate")
endDateCheck = DateAdd("d", 1, endDateCheck)
If endDateCheck = startDateCheck Then
rstSorted.Move i
dateStart = rstSorted.Fields("startDate")
End If
Else
dateStart = 0
End If
i = i + 1
Loop
rst1.Close
rstSorted.Close
Set rst1 = Nothing
Set rstSorted = Nothing
Set db = Nothing
End Function