Access - VB中行分隔"&_"和"_"之间的区别

Ale*_*lex 2 ms-access vba access-vba

我似乎无法找到任何具体的内容.我知道,为了将一段代码分成多行,只需在行尾添加"_"即可.

但是,我注意到我的一些代码行仅在我使用"&_"时才有效,而下一行包含一组额外的语音标记,如果在一行上则不包括它们,为什么这是?

代码在一行:

Msg1 = MsgBox("Removing an LPA will remove ALL of their details from the database, including their documents - You cannot undo this action. Do you wish to continue?", vbYesNo, "WARNING!")
Run Code Online (Sandbox Code Playgroud)

代码行分开工作:

Msg1 = MsgBox("Removing an LPA will remove ALL of their details from the database," & _
"including their documents - You cannot undo this action. Do you wish to" & _
" continue?", vbYesNo, "WARNING!")
Run Code Online (Sandbox Code Playgroud)

分隔的代码行不起作用:

Msg1 = MsgBox("Removing an LPA will remove ALL of their details from the database, _
including their documents - You cannot undo this action. Do you wish to _
continue?", vbYesNo, "WARNING!")
Run Code Online (Sandbox Code Playgroud)

那么"&_""_"之间有什么区别?

如果不关闭并打开一组新的语音标记,你能否在新行上继续使用字符串?

Hei*_*nzi 5

VBA(以及VB6,VBScript和早期版本的VB.NET)不支持多行字符串文字:

' Doesn't work
myString = "a
b c"

' Doesn't work either, because _ does not have a special meaning when
' inside double quotes.
myString = "a _
b c"
Run Code Online (Sandbox Code Playgroud)

因此,您需要将字符串拆分为多个字符串.您可以将它们与&运算符连接:

myString = "a " & "b c"       ' yields "a b c"
Run Code Online (Sandbox Code Playgroud)

而且那么可以拆分您的线路,在两个标记之间的任何地方的字符串:

myString = "a " & _
           "b c"              ' my personal preference

' or

myString = "a " _
           & "b c"

' or

myString = "a " _
           & _
           "b c"

' or even

myString _
    = _
    "a " _
    & _
    "b c"
Run Code Online (Sandbox Code Playgroud)