Sure here is the function to calculate the total number of days someone stayed during a given time frame:
Code:
Public Function TotalDays(MoveInDate As Date, MoveOutDate As Date, StartDate As Date, EndDate As Date)
'Is client still here?
If IsNull(MoveOutDate) Then
'Did they move in before the start date?
If MoveInDate < StartDate Then
TotalDays = EndDate - StartDate
Else
TotalDays = EndDate - MoveInDate
End If
Else 'Client was discharged
'Were they discharged during time frame?
If MoveOutDate > StartDate And MoveOutDate < EndDate Then
'Did they move in during time frame?
If MoveInDate > StartDate And MoveInDate < EndDate Then
TotalDays = MoveOutDate - MoveInDate
Else
TotalDays = MoveOutDate - StartDate
End If
Else 'Client was not discharged during time frame
'Did they move in during time frame?
If MoveInDate > StartDate And MoveInDate < EndDate Then
TotalDays = EndDate - MoveInDate
Else
TotalDays = EndDate - StartDate
End If
End If
End If
End Function
I am calling the function from the Control Source property of the text box that I want to display the total in using:
Code:
=TotalDays([MoveInDate],[MoveOutDate],CDate([StartDate]),CDate([EndDate]))
It seems to be working now after I restarted Access. My new problem is that for some results I am receiving #Error when running the report. It seems to be isolated to the first IF condition where the client is not discharged, but they moved in before the start date. I don't know why I am getting #Error when I am just trying to subtract the EndDate from StartDate.