繁体   English   中英

在“With”语句上使用“IF”语句

[英]Using an “IF” statement on a “With” Statement

我试图在 Access 中的 vba 代码中设置一个“with”语句,但是“With”引用的形式可能会改变。 有没有办法在不重写“with”语句中的代码两次的情况下做到这一点? 似乎我可以做这样的事情:

If FooVarible = true then
    with forms!form1
else
    with forms!form2!subForm1
endif
    'have code here
end with

但没有办法编译。

使用变量:

Dim frm as Object
If FooVarible = true then
    Set frm = forms!form1
else
    set frm = forms!form2!subform1.form
endif
With frm
    'have code here
end with

如果FooVariable可以是预编译器常量,那么您可以这样做:

#Const FooVariable = False

Sub Test()

#If FooVariable Then
    With Forms!Form1
#Else
    With Forms!Form2
#End If
        'with block contents
    End With

End Sub

可能不是你想要的,但很高兴知道。 编译后,VBA 仅在FooVariableTrue才会看到:

Sub Test()

    With Forms!Form1
        'with block contents
    End With

End Sub

而这个,如果FooVariableFalse

Sub Test()

    With Forms!Form2
        'with block contents
    End With

End Sub

请注意,编译器永远不会看到不完整的With块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM