简体   繁体   English

在VBA MS-Access中嵌套If / Then

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

I have a table with 7 different milestones, include an ACD and Status for each. 我有一个包含7个不同里程碑的表格,包括每个的ACD和状态。 I am trying to write code to create an overallstatus, by writing if/then to, beginning at the last milestone, check for a date in the ACD field, and return a corresponding status. 我正在尝试编写代码来创建一个整体状态,通过编写if / then to,从最后一个里程碑开始,检查ACD字段中的日期,并返回相应的状态。 If milestone7 has a date, milestone status7 is returned. 如果milestone7具有日期,则返回里程碑状态7。 If there is no date, then it would check if milestone6 acd has a date, and return milestonestatus7 if it does. 如果没有日期,那么它将检查milestone6 acd是否有日期,如果有,则返回milestonestatus7。 If it doesnt, it would check milestone5 acd, and if there is a date, return milestone6 status. 如果没有,它将检查milestone5 acd,如果有日期,则返回milestone6状态。

This is what I have, and it works fine. 这就是我所拥有的,它运作良好。

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

The problem I have is that 2 of the milestones can be Not Applicable, and I need to pass them over, as I dont want to report an overallstatus of Not Applicable. 我遇到的问题是2个里程碑可能不适用,我需要传递它们,因为我不想报告不适用的整体状态。 I wrote code individually that works to deal with this; 我单独编写代码来处理这个问题; however, I am not able to merge it together completely with my code above. 但是,我无法将上面的代码完全合并在一起。

    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

Both FinalTarget and VendorContracted have the potential to be not applicable. FinalTarget和VendorContracted都有可能不适用。 If either of them are, I want to continue to code to read the milestone before that, and if there is an acd, return the milestone status of the one after (if VendorContracted Status is Not Applicable, I then want to check if DesignApprovalACd has a date, if it does, I want to return CommunicationsDevelopmentStatus as my overallstatus, and if there isnt a date, I want to then check if InitiativeDesignACD has a date, and if so, return DesignApproval Status as the overallstatus, and if not, return Initiative Design Status as the overall status). 如果其中任何一个是,我想继续编码以读取之前的里程碑,并且如果有acd,则返回之后的里程碑状态(如果VendorContracted Status不适用,那么我想检查DesignApprovalACd是否有一个日期,如果是,我想返回CommunicationsDevelopmentStatus作为我的整体状态,如果没有日期,我想检查一下是否有一个日期,如果是,则返回DesignApproval状态作为整体状态,如果没有,则返回主动设计状态作为总体状态)。

The issue I am having is getting it all to flow together. 我遇到的问题是让所有这一切流动起来。 Any suggestions on a better way to do this, or the best way to create if/then/elseif? 有关更好的方法的任何建议,或创建if / then / elseif的最佳方法?

I also tried Select Case, doing individual If/Then/End If statements and wasn't able to get it to return the correct results. 我也尝试了Select Case,做了单独的If / Then / End If语句,并且无法让它返回正确的结果。

After getting my morning coffee (and adding lots of comments to the code), I think I have been able to get my head around what you are trying to achieve. 喝完早上的咖啡(并在代码中添加了很多评论)后,我想我已经能够了解你想要达到的目标。

You should be able to just add the extra conditions to your existing If statement: 您应该只需将额外条件添加到现有的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

Note: I did a lot of typing of the variable names, not just copy/pastes, so please check to make sure I didn't introduce any accidental typos. 注意:我做了很多变量名称的输入,而不仅仅是复制/粘贴,所以请检查以确保我没有引入任何意外的错别字。

PS Using code such as Not LaunchAndReportingACD as a way to test for a non-null value (which I assume is what you are doing if LaunchAndReportingACD is a Date ) is confusing. PS使用诸如Not LaunchAndReportingACD代码作为测试非空值的方法(我假设如果LaunchAndReportingACDDate ,我假设你正在做的事情)令人困惑。 Sure it works, but it would be a lot easier to understand if you said Not IsNull(LaunchAndReportingACD) . 当然它有效,但如果你说Not IsNull(LaunchAndReportingACD)会更容易理解。 Confusing code causes headaches for people who have to maintain your code after you change jobs. 令人困惑的代码会给那些在更换作业后必须维护代码的人带来麻烦。 (And also confuses people who try to help answer SO questions at 6:00am!) (并且还会混淆那些试图在早上6点帮助回答SO问题的人!)

I wanted to share the updated version thanks to the input on what worked for my form. 我希望分享更新的版本,这要归功于对我的表单有用的输入。 I took the advise for updating the Not IsNull and modified another line towards the bottom and so far (5 out of my 59 I am testing) it is working perfectly. 我接受了更新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

This was also another way that the overall status was able to be displayed. 这也是能够显示整体状态的另一种方式。 A cycle through function was created, as well as a flag, and it loops through to identify the last milestone that has an ECD, and then returns the status of the next milestone. 创建了一个循环函数,以及一个标志,它循环以识别具有ECD的最后一个里程碑,然后返回下一个里程碑的状态。 This worked on the continuous form when upon load. 这在加载时以连续形式工作。 I am using the above if/then version (as it is easier for me to manage personally), but also wanted to share this as it was a completely different approach (and I personally enjoy seeing how people do things differently). 我正在使用上面的if / then版本(因为我更容易亲自管理),但也想分享这个,因为它是一种完全不同的方法(我个人喜欢看人们如何以不同的方式做事)。 Thanks again to everyone who helped! 再次感谢所有帮助过的人!

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