frr*_*rrt 3 ms-access vba if-statement flow-control
我有一个简短的问题.VBA之间有什么不同吗?
if x1 and x2 and x3 and ... and x10 then
    foo
end if
和
if x1 then
  if x2 then
    if x3 then
      ...
      foo
    end if
  end if
end if
关于速度?
更具体地说:我有10列数据,需要逐行比较数据库中的重复数据(SELECT DISTINCT之类的东西在这种情况下不起作用).
我可以想象,那是用的
x1 = recordset.fields("Field1").value
if x1 then
  x2 = recordset.fields("Field2").value
  if x2 then
    x3 = recordset.fields("Field3").value
    if x3 then
      ...
      foo
    end if
  end if
end if
会快于
x1 = recordset.fields("Field1").value
x2 = recordset.fields("Field2").value
...
if x1 and x2 and x3 and ... and x10 then
    foo
end if
因为我不必读取记录集中的所有数据.或者ifs的数量是否会扼杀这种速度优势?
小智 9
如果其中任何一个已经失败,则单行检查所有条件无视.
Sub Main()
    If Check And Check And Check Then
    End If
End Sub
Function Check() As Boolean
    Debug.Print "checked"
    Check = False
End Function
嵌套ifs是更好的选择,因为只要一个条件失败,代码执行就会跳转到:else/end if block,而不是尝试评估所有其他条件.
| 归档时间: | 
 | 
| 查看次数: | 236 次 | 
| 最近记录: |