简体   繁体   English

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

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

I am trying to set up a "with" statement in vba code in Access, but the form that the "With" is referencing could change.我试图在 Access 中的 vba 代码中设置一个“with”语句,但是“With”引用的形式可能会改变。 Is there a way to do this without rewriting the code in the "with" statement twice?有没有办法在不重写“with”语句中的代码两次的情况下做到这一点? it seems like I could do something like this:似乎我可以做这样的事情:

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

but there is no way that would compile.但没有办法编译。

Use a variable:使用变量:

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

If FooVariable can be a precompiler constant then you can do this:如果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

Likely not what you were after, but good to know still.可能不是你想要的,但很高兴知道。 Once compiled, VBA only sees this if FooVariable is True :编译后,VBA 仅在FooVariableTrue才会看到:

Sub Test()

    With Forms!Form1
        'with block contents
    End With

End Sub

And this, if FooVariable is False :而这个,如果FooVariableFalse

Sub Test()

    With Forms!Form2
        'with block contents
    End With

End Sub

Notice that the compiler never ever sees an incomplete With block.请注意,编译器永远不会看到不完整的With块。

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

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