[英]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
代码作为测试非空值的方法(我假设如果LaunchAndReportingACD
是Date
,我假设你正在做的事情)令人困惑。 当然它有效,但如果你说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.