If 语句 VBA ACCESS 2010
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12667632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If Statements VBA ACCESS 2010
提问by Padawan
When using a
当使用一个
If Then
'Do Something
Else If
'Do Something
End If
If the first and second condition is true would it execute just the first or both?
如果第一个和第二个条件为真,它会只执行第一个还是两者都执行?
for some reason when i have this code:
出于某种原因,当我有此代码时:
Dim Report As String
If (DateRange_Pro_TotalCount > 0) Then
Dim DateRange_Pro_ReportStr As String
DateRange_Pro_ReportStr = "Total Referrals: " & DateRange_Pro_TotalCount & vbNewLine _
Report = Report & DateRange_Pro_ReportStr
ElseIf (DateRange_InPro_TotalCount > 0) Then
Dim DateRange_InPro_ReportStr As String
DateRange_InPro_ReportStr = "Total Referrals: " & DateRange_InPro_TotalCount & vbNewLine _
Report = Report & DateRange_InPro_ReportStr
End If
'next statement prints report variable to textBox
This statement only executes the first condition even though both conditions are true. I changed 'Else If' to 'End If' and 'If' ( two diff. conditional statments instead of one statement with two conditions) and it worked.
即使两个条件都为真,该语句也只执行第一个条件。我将“Else If”更改为“End If”和“If”(两个不同的条件语句,而不是一个带有两个条件的语句)并且它起作用了。
采纳答案by Adriaan Stander
Yes, you are correct, it will only execute the first block
是的,你是对的,它只会执行第一个块
From IF-THEN-ELSE Statement (VBA)
If condition_1 Then
result_1
ElseIf condition_2 Then
result_2
...
ElseIf condition_n Then
result_n
Else
result_else
End If
condition_1 to condition_n are evaluated in the order listed. Once a condition is found to be true, the IF-THEN-ELSE statement will execute the corresponding code and not evaluate the conditions any further.
result_1 to result_n is the code that is executed once a condition is found to be true.
condition_1 到 condition_n 按列出的顺序进行评估。一旦发现条件为真,IF-THEN-ELSE 语句将执行相应的代码,不再进一步评估条件。
result_1 到 result_n 是一旦发现条件为真就执行的代码。
回答by lc.
If...ElseIf...EndIf
does exactly what it suggests in English:
If...ElseIf...EndIf
完全按照它在英语中的建议:
If
the first condition is true, execute the first block.Else
,if
the second condition is true, execute the second block.
If
第一个条件为真,执行第一个块。Else
,if
第二个条件为真,执行第二个块。
In your case, you want two separate if statements; they will both be independently evaluated regardless of the result of the other condition.
在您的情况下,您需要两个单独的 if 语句;无论其他条件的结果如何,它们都将被独立评估。