I'm trying to write a function that would examine a date from a table and if it's between a certain range, it will output a string of characters. The problem is I'm getting a blank spot for some dates instead of a string of characters. Here's what I got so far:
Public Function Aged(datIn As Date) As String
Dim strAged As String
If datIn < Date - 120 Then
strAged = "121 and over "
ElseIf (datIn <= Date - 120) Then
strAge = " 91-120"
ElseIf (datIn <= Date - 90) Then
strAge = " 61-90"
ElseIf (datIn <= Date - 60) Then
strAge = " 31-60"
Well you don't have a default value. Its easier to do this type of stuff with a select case IMO. Try the following:
Code:
Public Function Aged(DateIn As Date) As String
Dim Age As String
Select Case -1 * (DateDiff("d", Date, DateIn))
Case Is > 120
Age = "121 and over"
Case 91 To 120
Age = "91 to 120"
Case 61 To 90
Age = "61 to 90"
Case 31 To 60
Age = "31 to 60"
Case 1 To 30
Age = "1 to 30"
Case 0
Age = "Today"
Case Else
Age = "Unknown"
End Select
Aged = Age
End Function
If you want, you could do like creosote has and put
Age = "Unknown " & datIn
so you can see what the value is thats getting through
__________________
<< Insert exceedingly large and overly verbose message of how 1337 you are here including full specs of every vehicle you've ever driven and PC you've owned >>