将长sql vba语句分成多行

问题描述 投票:0回答:2

我对 VBA 环境完全陌生。我试图将这一行分成多行,但失败了。有人可以帮我把这段代码分成多行吗?

DoCmd.RunSQL "UPDATE INDIVIDUAL SET INDIVIDUAL.INDI_FIRSTNAME = '" & prospect_contact!FirstName & "', INDIVIDUAL.INDI_LASTNAME = '" & prospect_contact!LastName & "', INDIVIDUAL.INDI_TEL = '" & prospect_contact!BusinessTelephone & "', INDIVIDUAL.INDI_ADDRESS1 = '" & Replace(prospect_contact!Street, "'", "") & "', INDIVIDUAL.INDI_ADDRESS2 = '" & Replace(prospect_contact!Street1, "'", "") & "', INDI_STATUS = '" & pro & "',INDIVIDUAL.INDI_FUNEL1 = '" & prospect_contact!QualificationStatus & "', INDIVIDUAL.INDI_COUNTRY = '" & prospect_contact!Country_Employer & "', INDIVIDUAL.ACCT_NAME = '" & Replace(prospect_contact!Employer, "'", "") & "' WHERE INDIVIDUAL.INDI_FULLNAME = '" & key & "';"

更新:我用 &_ 尝试了这个,但出现语法错误,并且代码在 VBA 中变成红色。 我在逗号或引号上犯了错误吗?我不知道。

                        DoCmd.RunSQL "UPDATE INDIVIDUAL SET INDIVIDUAL.INDI_FIRSTNAME = '" & prospect_contact!FirstName & "', & _
                    INDIVIDUAL.INDI_LASTNAME = '" & prospect_contact!LastName & "',  & _
                    INDIVIDUAL.INDI_TEL = '" & prospect_contact!BusinessTelephone & "', & _
                    INDIVIDUAL.INDI_ADDRESS1 = '" & Replace(prospect_contact!Street, "'", "") & "', & _
                    INDIVIDUAL.INDI_FUNEL1 = '" & prospect_contact!QualificationStatus & "', & _
                    INDI_STATUS = '" & pro & "', & _
                    INDIVIDUAL.INDI_COUNTRY = '" & prospect_contact!Country_Employer & "', & _
                    INDIVIDUAL.ACCT_NAME = '" & Replace(prospect_contact!Employer, "'", "") & "' & _
                    WHERE INDIVIDUAL.INDI_FULLNAME = '" & key & "';"

更新2:

它有效!有用!有用!感谢@Bathsheba,@TheLaurens:)

vba ms-access-2007
2个回答
14
投票

在 VBA 中,空格后跟下划线,后面没有任何内容,即可换行。

例如

DoCmd.RunSQL "UPDATE INDIVIDUAL SET INDIVIDUAL.INDI_FIRSTNAME = '" & _ prospect_contact!FirstName & "', INDIVIDUAL.INDI_LASTNAME = '" & _ etc

但是不要在字符串文字中换行:这是一个语法错误。

换行符的数量有一个令人惊讶的小限制。


0
投票
另一种方法(与上面

Bathsheba 的方法非常相似)是先使用 _

 下划线,然后在下一行使用 
&
 与符号。

With mclsQuery .SQLCommand = "SELECT rp.ITEM_CD " _ & "FROM RECIPE_PRODUCT rp " _ & "JOIN RECIPE_STEP rs ON rs.RECIPE_STEP_ID = rp.RECIPE_STEP_ID " _ & "JOIN RECIPE_REV rr ON rr.RECIPE_REV_ID = rs.RECIPE_REV_ID " _ & "JOIN RECIPE r ON r.RECIPE_CD = rr.RECIPE_CD " _ & "WHERE r.RECIPE_CD = '" & SimpleTouchRecipe.Value & "'"
看个人喜好啦!下一行的 

& _

_
 w/ 
&
 都可以工作。

我喜欢

_

 + 新行 + 
&
 方法,因为我有时会构建很长的代码行(仍然!),左边距的 
&
 提醒我,如果右边距不是',那么它是一个多行语句不可见。

请参阅 Microsoft Learn 文档中的

将单个语句分成多行 (Visual Basic)

© www.soinside.com 2019 - 2024. All rights reserved.