2016-03-04 121 views
0

我有一些麻煩得到一些複雜的IF邏輯工作內部的MS Access查詢,並在這一刻感覺油炸,所以我希望一雙新鮮的眼睛可以幫助我解決我的問題。複雜訪問公式邏輯

下面的截圖我希望能幫助我的邏輯更加清晰。與式

​​

我的最終結果是,以確定是否一個借方(Borr)是外的日期提交的文件。在這種情況下,我只關心(P)FS和/或IE文件。

過期的條件有點複雜,我會在下面解釋它們。 TRUE等同於過期並且爲合規。

  1. 如果NR複選框被選中爲(P)FSIE然後FALSE
  2. 如果不檢查(P)FSIENR框和(P)FS date is blank AND IE`日期爲空白,則TRUE

這很容易,但現在對於棘手的部分 CYYY =當前的年,PYYY =前一年,PY2Y = 2年前

  1. 如果(P)FSIE是必需的,如果當前日期是1/1/CYYY和4/30/CYYYY和日期之間在(P)FSIE爲10/1/PY2Y和4之間/ 30/PYYY然後FALSE否則TRUE
  2. 如果(P)FSIE是必需的,如果當前日期是5/1/CYYY和9/30之間/ CYYYY和日期(P)FSIE小於1歲然後FALSE否則TRUE
  3. 最後一點要記住的是,只要有一個向上的最新(P)FSIE文件,結果應該是FALSE。他們都不是必需的。它是或者。

這張截圖應該有助於澄清日期公式:

enter image description here

這是我已經進入最後的取數公式,但仍然無法正常工作。我也想我知道爲什麼,但我也用這個炒,試圖繼續解決的時刻:

B1-FS-IE: IIf([annual_review].[prop_cd]<>"0056" And [nr_fs_b1]=True And 
[nr_ie_b1]=True,False,IIf(([nr_fs_b1]=False And IsNull([fs_b1])) And ([nr_ie_b1]=False 
And IsNull([ie_b1])),True,IIf(Month([fs_b1])>9 Or Month([fs_b1])<5 
And (Month(Date())<5 And Date()-[fs_b1]>576) Or (Month([fs_b1])>4 
And Month([fs_b1])<10 And Date()-[fs_b1]>=365) Or Month([ie_b1])>9 
Or Month([ie_b1])<5 And (Month(Date())<5 And Date()-[ie_b1]>576) 
Or (Month([ie_b1])>4 And Month([ie_b1])<10 And Date()-[ie_b1]>=365),True,False))) 

最後,我把Excel標籤,因爲如果有人能工作,這一點在Excel中,我可以轉換爲Access。

+2

像這樣的公式不僅是一個噩夢發展,但也維護。我會把邏輯放入一個公共的VBA函數中,並將相關字段作爲參數傳遞。在那裏你可以編寫可讀的代碼。 – Andre

+0

@Andre - 輝煌!我甚至沒有想到這一點。這將使得構建,閱讀和維護變得更容易!感謝您對我需要的觀點的改變! –

回答

0

您還需要計算年適當的方法 - 像這樣的功能:

Public Function Years(_ 
    ByVal datDate1 As Date, _ 
    ByVal datDate2 As Date, _ 
    Optional ByVal booLinear As Boolean) _ 
    As Integer 

' Returns the difference in full years between datDate1 and datDate2. 
' 
' Calculates correctly for: 
' negative differences 
' leap years 
' dates of 29. February 
' date/time values with embedded time values 
' negative date/time values (prior to 1899-12-29) 
' 
' Optionally returns negative counts rounded down to provide a 
' linear sequence of year counts. 
' For a given datDate1, if datDate2 is decreased step wise one year from 
' returning a positive count to returning a negative count, one or two 
' occurrences of count zero will be returned. 
' If booLinear is False, the sequence will be: 
' 3, 2, 1, 0, 0, -1, -2 
' If booLinear is True, the sequence will be: 
' 3, 2, 1, 0, -1, -2, -3 
' 
' If booLinear is False, reversing datDate1 and datDate2 will return 
' results of same absolute Value, only the sign will change. 
' This behaviour mimics that of Fix(). 
' If booLinear is True, reversing datDate1 and datDate2 will return 
' results where the negative count is offset by -1. 
' This behaviour mimics that of Int(). 

' DateAdd() is used for check for month end of February as it correctly 
' returns Feb. 28. when adding a count of years to dates of Feb. 29. 
' when the resulting year is a common year. 
' 
' 2000-11-03. Cactus Data ApS, CPH. 
' 2000-12-16. Leap year correction modified to be symmetrical. 
'    Calculation of intDaysDiff simplified. 
'    Renamed from YearsDiff() to Years(). 
' 2000-12-18. Introduced cbytMonthDaysMax. 
' 2007-06-22. Version 2. Complete rewrite. 
'    Check for month end of February performed with DateAdd() 
'    after idea of Markus G. Fischer. 

    Dim intDiff As Integer 
    Dim intSign As Integer 
    Dim intYears As Integer 

    ' Find difference in calendar years. 
    intYears = DateDiff("yyyy", datDate1, datDate2) 
    ' For positive resp. negative intervals, check if the second date 
    ' falls before, on, or after the crossing date for a full 12 months period 
    ' while at the same time correcting for February 29. of leap years. 
    If DateDiff("d", datDate1, datDate2) > 0 Then 
    intSign = Sgn(DateDiff("d", DateAdd("yyyy", intYears, datDate1), datDate2)) 
    intDiff = Abs(intSign < 0) 
    Else 
    intSign = Sgn(DateDiff("d", DateAdd("yyyy", -intYears, datDate2), datDate1)) 
    If intSign <> 0 Then 
     ' Offset negative count of years to continuous sequence if requested. 
     intDiff = Abs(booLinear) 
    End If 
    intDiff = intDiff - Abs(intSign < 0) 
    End If 

    ' Return count of years as count of full 12 months periods. 
    Years = intYears - intDiff 

End Function 
0

對於第一部分以獲得借款人是否是過時的PFS,IE複選框,日期,

可以使用下面的等式有關借款人控制的控制源以指示True或False

IIF(checkPFS.value = True和CheckIE.value =真,假,IIF(checkPFS.value = True和CheckIE.value = True和PFSDate =''和IEDate ='',True,False))