簡體   English   中英

在VBA MS-Access中嵌套If / Then

[英]Nesting If/Then in VBA MS-Access

我有一個包含7個不同里程碑的表格,包括每個的ACD和狀態。 我正在嘗試編寫代碼來創建一個整體狀態,通過編寫if / then to,從最后一個里程碑開始,檢查ACD字段中的日期,並返回相應的狀態。 如果milestone7具有日期,則返回里程碑狀態7。 如果沒有日期,那么它將檢查milestone6 acd是否有日期,如果有,則返回milestonestatus7。 如果沒有,它將檢查milestone5 acd,如果有日期,則返回milestone6狀態。

這就是我所擁有的,它運作良好。

If Not LaunchAndReportingACD Then
    Me.OverallInitiativeStatus = "Complete"
ElseIf Not FinalTargetingACD Then
    Me.OverallInitiativeStatus = LaunchAndReportingStatus
ElseIf Not CommunicationsApprovalACD Then
    Me.OverallInitiativeStatus = FinalTargetingStatus
ElseIf Not CommunicationsDevelopmentACD Then
    Me.OverallInitiativeStatus = CommunicationsApprovalStatus
ElseIf Not VendorContractedACD Then
    Me.OverallInitiativeStatus = CommunicationsDevelopmentStatus
ElseIf Not DesignApprovalACD Then
    Me.OverallInitiativeStatus = VendorContractedStatus
ElseIf Not InitiativeDesignACD Then
    Me.OverallInitiativeStatus = DesignApprovalStatus
Else
    Me.OverallInitiativeStatus = InitiativeDesignStatus
End If

我遇到的問題是2個里程碑可能不適用,我需要傳遞它們,因為我不想報告不適用的整體狀態。 我單獨編寫代碼來處理這個問題; 但是,我無法將上面的代碼完全合並在一起。

    If VendorContractedStatus = "Not Applicable" Then
            If Not DesignApprovalACD Then
            Me.OverallInitiativeStatus = CommunicationsDevelopmentStatus
                ElseIf Not InitiativeDesignACD Then
                Me.OverallInitiativeStatus = DesignApprovalStatus
                Else
                Me.OverallInitiativeStatus = InitiativeDesignStatus
                End If
            End If
    If VendorContractedStatus <> "Not Applicable" Then
        If Not VendorContractedACD Then
        Me.OverallInitiativeStatus = CommunicationsDevelopmentStatus
            ElseIf Not DesignApprovalACD Then
            Me.OverallInitiativeStatus = VendorContractedStatus
                ElseIf Not InitiativeDesignACD Then
                Me.OverallInitiativeStatus = DesignApprovalStatus
                Else
                Me.OverallInitiativeStatus = InitiativeDesignStatus
                End If
            End If

FinalTarget和VendorContracted都有可能不適用。 如果其中任何一個是,我想繼續編碼以讀取之前的里程碑,並且如果有acd,則返回之后的里程碑狀態(如果VendorContracted Status不適用,那么我想檢查DesignApprovalACd是否有一個日期,如果是,我想返回CommunicationsDevelopmentStatus作為我的整體狀態,如果沒有日期,我想檢查一下是否有一個日期,如果是,則返回DesignApproval狀態作為整體狀態,如果沒有,則返回主動設計狀態作為總體狀態)。

我遇到的問題是讓所有這一切流動起來。 有關更好的方法的任何建議,或創建if / then / elseif的最佳方法?

我也嘗試了Select Case,做了單獨的If / Then / End If語句,並且無法讓它返回正確的結果。

喝完早上的咖啡(並在代碼中添加了很多評論)后,我想我已經能夠了解你想要達到的目標。

您應該只需將額外條件添加到現有的If語句中:

If Not LaunchAndReportingACD Then

    'If LaunchAndReportingACD is not Null, then project is Complete
    Me.OverallInitiativeStatus = "Complete"

ElseIf Not FinalTargetingACD Then

    'If FinalTargetingACD is not Null, we are in the LaunchAndReporting phase
    Me.OverallInitiativeStatus = LaunchAndReportingStatus

ElseIf Not CommunicationsApprovalACD Then

    'If CommunicationsApprovalACD is not Null, we are in the FinalTargeting phase,
    ' or the LaunchAndReporting phase (if there is no FinalTargeting phase)
    If FinalTargetingStatus = "Not Applicable" Then
        Me.OverallInitiativeStatus = LaunchAndReportingStatus
    Else
        Me.OverallInitiativeStatus = FinalTargetingStatus
    End If

ElseIf Not CommunicationsDevelopmentACD Then

    'If CommunicationsDevelopmentACD is not Null, we are in the CommunicationsApproval phase
    Me.OverallInitiativeStatus = CommunicationsApprovalStatus

ElseIf Not VendorContractedACD Then

    'If VendorContractedACD is not Null, we are in the CommunicationsDevelopment phase
    Me.OverallInitiativeStatus = CommunicationsDevelopmentStatus

ElseIf Not DesignApprovalACD Then

    'If DesignApprovalACD is not Null, we are in the VendorContracted phase,
    ' or the CommunicationsDevelopment phase (if there is no VendorContracted phase)
    If VendorContractedStatus = "Not Applicable" Then
        Me.OverallInitiativeStatus = CommunicationsDevelopmentStatus
    Else
        Me.OverallInitiativeStatus = VendorContractedStatus
    End If

ElseIf Not InitiativeDesignACD Then

    'If InitiativeDesignACD is not Null, we are in the DesignApproval phase
    Me.OverallInitiativeStatus = DesignApprovalStatus

Else

    'We must be in the InitiativeDesign phase
    Me.OverallInitiativeStatus = InitiativeDesignStatus

End If

注意:我做了很多變量名稱的輸入,而不僅僅是復制/粘貼,所以請檢查以確保我沒有引入任何意外的錯別字。

PS使用諸如Not LaunchAndReportingACD代碼作為測試非空值的方法(我假設如果LaunchAndReportingACDDate ,我假設你正在做的事情)令人困惑。 當然它有效,但如果你說Not IsNull(LaunchAndReportingACD)會更容易理解。 令人困惑的代碼會給那些在更換作業后必須維護代碼的人帶來麻煩。 (並且還會混淆那些試圖在早上6點幫助回答SO問題的人!)

我希望分享更新的版本,這要歸功於對我的表單有用的輸入。 我接受了更新Not IsNull的建議並修改了另一條線到底部到目前為止(我正在測試的59個中的5個)它完美地工作。

If Not IsNull(LaunchAndReportingACD) Then

    'If LaunchAndReportingACD is not Null, then project is Complete
    Me.OverallInitiativeStatus = "Complete"

ElseIf Not IsNull(FinalTargetingACD) Then

    'If FinalTargetingACD is not Null, we are in the LaunchAndReporting phase
    Me.OverallInitiativeStatus = LaunchAndReportingStatus

ElseIf Not IsNull(CommunicationsApprovalACD) Then

    'If CommunicationsApprovalACD is not Null, we are in the FinalTargeting phase,
    ' or the LaunchAndReporting phase (if there is no FinalTargeting phase)
    If FinalTargetingStatus = "Not Applicable" Then
        Me.OverallInitiativeStatus = LaunchAndReportingStatus
    Else
        Me.OverallInitiativeStatus = FinalTargetingStatus
    End If

ElseIf Not IsNull(CommunicationsDevelopmentACD) Then

    'If CommunicationsDevelopmentACD is not Null, we are in the CommunicationsApproval phase
    Me.OverallInitiativeStatus = CommunicationsApprovalStatus

ElseIf Not IsNull(VendorContractedACD) Then

    'If VendorContractedACD is not Null, we are in the CommunicationsDevelopment phase
    Me.OverallInitiativeStatus = CommunicationsDevelopmentStatus

ElseIf Not IsNull(DesignApprovalACD) Then

    'If DesignApprovalACD is not Null, we are in the VendorContracted phase,
    ' or the CommunicationsDevelopment phase (if there is no VendorContracted phase)
    If VendorContractedStatus = "Not Applicable" Then
        Me.OverallInitiativeStatus = CommunicationsDevelopmentStatus
    Else
        Me.OverallInitiativeStatus = VendorContractedStatus
    End If

ElseIf Not IsNull(InitiativeDesignACD) Then

    'If InitiativeDesignACD is not Null, we are in the DesignApproval phase
    Me.OverallInitiativeStatus = DesignApprovalStatus

Else

    'We must be in the InitiativeDesign phase
    Me.OverallInitiativeStatus = InitiativeDesignStatus

End If

這也是能夠顯示整體狀態的另一種方式。 創建了一個循環函數,以及一個標志,它循環以識別具有ECD的最后一個里程碑,然后返回下一個里程碑的狀態。 這在加載時以連續形式工作。 我正在使用上面的if / then版本(因為我更容易親自管理),但也想分享這個,因為它是一種完全不同的方法(我個人喜歡看人們如何以不同的方式做事)。 再次感謝所有幫助過的人!

Private Sub Form_Current()
cycleThrough
End Sub

'Private Sub Form_Load()
'
'cycleThrough
'
'End Sub

Function cycleThrough()

Dim lastfield As String
Dim whichfield  As String
Dim whichfield2 As String

Dim flag As Byte

flag = 0

Me.txt_overall.Value = "Hi"


If Not IsNull(txt_LaunchAndReportingACD.Value) Then
whichfield = "LaunchAndReporting"
flag = 1
End If

If Not IsNull(txt_FinalTargetingACD.Value) And flag = 0 Then
whichfield = "FinalTargeting"
flag = 1
End If

If Not IsNull(txt_CommunicationsApprovalACD.Value) And flag = 0 Then
whichfield = "CommunicationsApproval"
flag = 1
End If


If Not IsNull(txt_CommunicationsDevelopmentACD.Value) And flag = 0 Then
whichfield = "CommunicationsDevelopment"
flag = 1
End If


If Not IsNull(txt_VendorContractedACD.Value) And flag = 0 Then
whichfield = "VendorContracted"
flag = 1
End If


If Not IsNull(txt_DesignApprovalACD.Value) And flag = 0 Then
whichfield = "DesignApproval"
flag = 1
End If


If Not IsNull(txt_InitiativeDesignACD.Value) And flag = 0 Then
whichfield = "InitiativeDesign"
flag = 1
End If

Me.txt_overall.Value = whichfield

If whichfield = "LaunchAndReporting" Then
whichfield2 = "Green"
' then green
End If

' And txt_FinalTargetingStatus.Value <> "Not Applicable"

If whichfield = "FinalTargeting" Then
whichfield2 = txt_LaunchAndReportingStatus.Value
End If


If whichfield = "CommunicationsApproval" Then
If txt_LaunchAndReportingStatus.Value <> "Not Applicable" Then whichfield2 = txt_LaunchAndReportingStatus.Value
If txt_FinalTargetingStatus.Value <> "Not Applicable" Then whichfield2 = txt_FinalTargetingStatus.Value
End If

If whichfield = "CommunicationsDevelopment" Then
If txt_LaunchAndReportingStatus.Value <> "Not Applicable" Then whichfield2 = txt_LaunchAndReportingStatus.Value
If txt_FinalTargetingStatus.Value <> "Not Applicable" Then whichfield2 = txt_FinalTargetingStatus.Value
If txt_CommunicationsApprovalStatus.Value <> "Not Applicable" Then whichfield2 = txt_CommunicationsApprovalStatus.Value
End If


If whichfield = "VendorContracted" Then
If txt_LaunchAndReportingStatus.Value <> "Not Applicable" Then whichfield2 = txt_LaunchAndReportingStatus.Value
If txt_FinalTargetingStatus.Value <> "Not Applicable" Then whichfield2 = txt_FinalTargetingStatus.Value
If txt_CommunicationsApprovalStatus.Value <> "Not Applicable" Then whichfield2 = txt_CommunicationsApprovalStatus.Value
If txt_CommunicationsDevelopmentStatus.Value <> "Not Applicable" Then whichfield2 = txt_CommunicationsDevelopmentStatus.Value
End If


If whichfield = "DesignApproval" Then
If txt_LaunchAndReportingStatus.Value <> "Not Applicable" Then whichfield2 = txt_LaunchAndReportingStatus.Value
If txt_FinalTargetingStatus.Value <> "Not Applicable" Then whichfield2 = txt_FinalTargetingStatus.Value
If txt_CommunicationsApprovalStatus.Value <> "Not Applicable" Then whichfield2 = txt_CommunicationsApprovalStatus.Value
If txt_CommunicationsDevelopmentStatus.Value <> "Not Applicable" Then whichfield2 = txt_CommunicationsDevelopmentStatus.Value
If txt_VendorContractedStatus.Value <> "Not Applicable" Then whichfield2 = txt_VendorContractedStatus.Value
End If


If whichfield = "InitiativeDesign" Then
If txt_LaunchAndReportingStatus.Value <> "Not Applicable" Then whichfield2 = txt_LaunchAndReportingStatus.Value
If txt_FinalTargetingStatus.Value <> "Not Applicable" Then whichfield2 = txt_FinalTargetingStatus.Value
If txt_CommunicationsApprovalStatus.Value <> "Not Applicable" Then whichfield2 = txt_CommunicationsApprovalStatus.Value
If txt_CommunicationsDevelopmentStatus.Value <> "Not Applicable" Then whichfield2 = txt_CommunicationsDevelopmentStatus.Value
If txt_VendorContractedStatus.Value <> "Not Applicable" Then whichfield2 = txt_VendorContractedStatus.Value
If txt_DesignApprovalStatus.Value <> "Not Applicable" Then whichfield2 = txt_DesignApprovalStatus.Value
End If



Me.txt_whichStatus.Value = whichfield2



'txt_LaunchAndReportingStatus
'txt_LaunchAndReportingACD
'txt_FinalTargetingStatus
'txt_FinalTargetingACD
'txt_CommunicationsApprovalStatus
'txt_CommunicationsApprovalACD
'txt_CommunicationsDevelopmentStatus
'txt_CommunicationsDevelopmentACD
'txt_VendorContractedStatus
'txt_VendorContractedACD
'txt_DesignApprovalStatus
'txt_DesignApprovalACD
'txt_InitiativeDesignStatus
'txt_InitiativeDesignACD


End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM