簡體   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