I notice you've declared the variables as Integer. Are they actually Dates
in the fields, or are you storing something else? And if you don't have a
Move-in Date or a Move-out Date, what's stored? Hopefully it's Null.
If that's the case, try declaring your input parameters as Variants and
checking if their values are Null in your code.
If pintMoveIn is Null, presumably you'll need to set your function output to
Unknown. If pintMoveOut is Null, use the current date instead.
Function CalcTimeThere(pintMoveIn As Variant, _
pintMoveOut As Variant) As String
Dim pintYearsThere As Integer
Dim pintMonthsThere As Integer
Dim pintDaysThere As Integer
Dim pintDaysLeft As Integer
Dim pstrYears As String
Dim pstrMonths As String
If IsNull(pintMoveIn) Then
CalcTimeThere = "Unknown"
Else
If IsNull(pintMoveOut) Then
pintMoveOut = Date()
End If
pintDaysThere = pintMoveOut - pintMoveIn
pintYearsThere = Int(pintDaysThere / 365)
pintDaysLeft = pintDaysThere - (pintYearsThere * 365)
pintMonthsThere = Int(pintDaysLeft / 30)
If pintYearsThere = 0 Then
pstrYears = ""
ElseIf pintYearsThere = 1 Then
pstrYears = "1 Year"
Else
pstrYears = pintYearsThere & " Years"
End If
If pintMonthsThere = 0 Then
pstrMonths = ""
ElseIf pintMonthsThere = 1 Then
pstrMonths = "1 Month"
Else
pstrMonths = " " & pintMonthsThere & " Months"
End If
CalcTimeThere = pstrYears & pstrMonths
End If
End Function
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)
"Conan Kelly" <(E-Mail Removed)> wrote in message
news:vHyyc.6668$(E-Mail Removed)...
> Can anyone help me with this one?
>
> I have a table in a personal database with Previous & Current Addresses
for
> we have lived. I'm using that table in a query to calculate the amount of
> time living at each address in the form of "x Year(s) y Month(s)".
>
> Having the a "Total Days There" calculated field (using "Move-in Date" and
> "Move-out Date" fields for the calculation) in the qurey and passing the
> value from that field to the following code, it will calculate the "Time
> There" for EVERY record (returns "#Error" for those records missing a
"Total
> Days There" value because of missing "Move-in Date" or "Move-out Date"
> values):
>
> Function CalcTimeThere2(pintDaysThere As Integer) As String
> Dim pintYearsThere As Integer
> Dim pintMonthsThere As Integer
> Dim pintDaysLeft As Integer
> Dim pstrYears As String
> Dim pstrMonths As String
>
> pintYearsThere = Int(pintDaysThere / 365)
> pintDaysLeft = pintDaysThere - (pintYearsThere * 365)
> pintMonthsThere = Int(pintDaysLeft / 30)
>
> If pintYearsThere = 0 Then
> pstrYears = ""
> ElseIf pintYearsThere = 1 Then
> pstrYears = "1 Year"
> Else
> pstrYears = pintYearsThere & " Years"
> End If
>
> If pintMonthsThere = 0 Then
> pstrMonths = ""
> ElseIf pintMonthsThere = 1 Then
> pstrMonths = "1 Month"
> Else
> pstrMonths = " " & pintMonthsThere & " Months"
> End If
>
> CalcTimeThere2 = pstrYears & pstrMonths
>
> End Function
>
>
> The problem is if I try to skip the calculated field in the qurey and pass
> the values in the "Move-in Date" and "Move-out Date" fields as two
> arguements in the following code and try to do my calculations in code, it
> will correctly calculate only the last record correctly. "#Error" is
> returned for all of the other fields.
>
> Function CalcTimeThere(pintMoveIn As Integer, pintMoveOut As Integer) As
> String
> Dim pintYearsThere As Integer
> Dim pintMonthsThere As Integer
> Dim pintDaysThere As Integer
> Dim pintDaysLeft As Integer
> Dim pstrYears As String
> Dim pstrMonths As String
>
> pintDaysThere = pintMoveOut - pintMoveIn
> pintYearsThere = Int(pintDaysThere / 365)
> pintDaysLeft = pintDaysThere - (pintYearsThere * 365)
> pintMonthsThere = Int(pintDaysLeft / 30)
>
> If pintYearsThere = 0 Then
> pstrYears = ""
> ElseIf pintYearsThere = 1 Then
> pstrYears = "1 Year"
> Else
> pstrYears = pintYearsThere & " Years"
> End If
>
> If pintMonthsThere = 0 Then
> pstrMonths = ""
> ElseIf pintMonthsThere = 1 Then
> pstrMonths = "1 Month"
> Else
> pstrMonths = " " & pintMonthsThere & " Months"
> End If
>
> CalcTimeThere = pstrYears & pstrMonths
>
> End Function
>
> Part of the problem is that the second set of code will not calculate
every
> field. I'm trying to use the second set of code because I want to be able
> to calculate the time at the current address (but there is no "Move-out
> Date" for the current address). I know how to do that in code, as long as
I
> can get the code to calculate every record. If someone knows how to get
the
> "Move-out Date" for the current address to be the current date every time
> the qurey/table is opened, then I can use the first set of code.
>
> Thanks for any help anyone can offer,
>
> Conan Kelly
>
>