IIf Заявление Глядя на месяцы - PullRequest
0 голосов
/ 03 февраля 2019

Я хотел бы создать оператор IIf для расчета ожидаемой даты отъезда ребенка.

Например, ожидается, что ребенок, родившийся до 31/08,покинуть ясли через 4 года, ребенок, родившийся после этой даты, как ожидается, уйдет через 5 лет.

Теперь, что я пытался сделать, это спросить заявление ИИФ, которое смотрит на дату рождения и решает, рассчитывать ли на 4 года или на 5 лет.Однако я продолжаю сталкиваться с проблемами с кодом, который я использую, который является

= IIf([Date of Birth]>#31/08/0000# , =DateAdd("yyyy",4,[Date of Birth]) , =DateAdd("yyyy",5,[Date of Birth]))

, поскольку есть много детей с разными датами рождения.Должен быть способ взглянуть конкретно только на месяцы.

РЕДАКТИРОВАТЬ: Оказывается, это не то, что нужно моему боссу, а то, что ему нужно в основном, чтобы показать, когда ребенок уходит из детской, то есть когдановый школьный семестр наступает, и ребенку 4 года.если ребенок родился до сентября, он может поступить в школу в этом году.если он не ребенок, он может поступить в школу в сентябре следующего года.И сейчас я понятия не имею, что делать, поскольку мои попытки выполнить функцию IIF полностью провалились.Может ли кто-нибудь помочь?

1 Ответ

0 голосов
/ 03 февраля 2019

Попробуйте:

=DateAdd("yyyy", IIf([Date of Birth] > DateSerial(Year([Date of Birth]), 8, 31), 4, 5), [Date of Birth])

Редактировать 1 :

Вы можете использовать DateAdd , например:

=IIf(DateAdd("yyyy", 4, [Date of Birth]) < DateSerial(Year(Date()), 9, 1), "Start school this year", "Postpone school start")

Изменить 2 :

Или вы можете рассчитать возраст детей 1 сентября:

AgeAtSeptember: Age([Date of Birth], DateSerial(Year(Date()), 9, 1))

, используя эту функцию:

' Returns the difference in full years from DateOfBirth to current date,
' optionally to another date.
' Returns zero if AnotherDate is earlier than DateOfBirth.
'
' Calculates correctly for:
'   leap years
'   dates of 29. February
'   date/time values with embedded time values
'   any date/time value of data type Date
'
' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28th when adding a count of years to dates of Feb. 29th
' when the resulting year is a common year.
'
' 2015-11-24. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Age( _
    ByVal DateOfBirth As Date, _
    Optional ByVal AnotherDate As Variant) _
    As Integer

    Dim ThisDate    As Date
    Dim Years       As Integer

    If IsDateExt(AnotherDate) Then
        ThisDate = CDate(AnotherDate)
    Else
        ThisDate = Date
    End If

    ' Find difference in calendar years.
    Years = DateDiff("yyyy", DateOfBirth, ThisDate)
    If Years > 0 Then
        ' Decrease by 1 if current date is earlier than birthday of current year
        ' using DateDiff to ignore a time portion of DateOfBirth.
        If DateDiff("d", ThisDate, DateAdd(IntervalSetting(DtInterval.dtYear), Years, DateOfBirth)) > 0 Then
            Years = Years - 1
        End If
    ElseIf Years < 0 Then
        Years = 0
    End If

    Age = Years

End Function
...