如果查询,VBA嵌套

时间:2014-12-31 17:39:28

标签: vba access-vba

我正在尝试在VBA中编写嵌套的if语句。在cobol中,我通常会使用evaluate子句。但是我在VBA中使用什么以避免长循环。

实施例

if cmbfield = "green" then
 me.frame1.enable = true
else
 me.frame2.enable = false
 me.frame3.enable = false
end if
if cmbfield = "red" then
 me.frame2.enable = true
else
 me.frame1.enable = false
 me.frame3.enable = false
end if
if cmbfield = "white" then
 me.frame3.enable = true
else
 me.frame1.enable = false
 me.frame2.enable = false
end if

3 个答案:

答案 0 :(得分:3)

在您给出的示例中,我使用了切换命令:

http://www.techonthenet.com/excel/formulas/case.php

Select Case test_expression

   Case condition_1
      result_1

   Case condition_2
      result_2

   ...

   Case condition_n
      result_n

   Case Else
      result_else

End Select

你也可以这样做... elseif ...结束如果

http://www.techonthenet.com/excel/formulas/if_then.php

If condition_1 Then
   result_1

ElseIf condition_2 Then
  result_2

...

ElseIf condition_n Then
   result_n

Else
   result_else

End If

答案 1 :(得分:2)

Dim isGreen as Boolean, isRed as Boolean, isWhite as Boolean
isGreen = (cmbfield = "green")
isRed = (cmbfield = "red")
isWhite = (cmbfield = "white")

me.frame1.enabled = isGreen
me.frame2.enabled = isRed
me.frame3.enabled = isWhite

这是编写相同代码的较短方法。应该以模数语法工作;希望这有帮助。

答案 2 :(得分:2)

我是这样做的:

Me.frame1.enabled = (cmbfield = "green")
Me.frame2.enabled = (cmbfield = "red")
Me.frame3.enabled = (cmbfield = "white")