VBA 多行字符串问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27112716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:20:55  来源:igfitidea点击:

VBA Multiline String Issues

stringvba

提问by Chito

I am not having much experience writing VBA script, I am almost lost to find out in the multiline string. Where exactly I am lost is when I try to split the String in multiple lines, could you help me how to end

我没有太多编写 VBA 脚本的经验,我几乎无法在多行字符串中找到答案。我迷失的地方是当我尝试将字符串拆分成多行时,你能帮我如何结束吗

Working String is below before split:

拆分前的工作字符串如下:

strSQL = "insert into ded_limit_analysis (PDPD_ID,PDDS_DESC,PRODUCT_CAT,BASE_PDPD_ID,PROD_CYCLE,HMO_IND_DED) " & vbCrLf & _
 "values (" & "'" & Me.txt_pdpd_id & "'" & "," & "'" & Me.txt_pdds & "'" & "," & "'" & Me.cbx_prod_type & "'" & "," & "'" & Me.txt_base_pdpd & "'" & "," & "'" & Me.cbx_prod_cycle & "'" & "," & "'" & Me.txt_hmo_ind_ded & "'" & ")"

The one not working is below, I am trying to split the lines because I have many columns to include in insert and it is more than 1000 caracters and not able to fit in single line (below is just sample, actual is much longer and I am forced to split the lines).

不工作的一个在下面,我试图拆分行,因为我有很多列要包含在插入中,并且它有 1000 多个字符并且无法放入单行(下面只是示例,实际要长得多,我我被迫分割线)。

strSQL = "insert into ded_limit_analysis (PDPD_ID,PDDS_DESC,PRODUCT_CAT,BASE_PDPD_ID,PROD_CYCLE,HMO_IND_DED) " & vbCrLf & _
 "values (" & "'" & Me.txt_pdpd_id & "'" & "," & "'" & Me.txt_pdds & "'" & "," & "'" & Me.cbx_prod_type & "'" & "," & "'" & Me.txt_base_pdpd & "'" & "," & "'" & Me.cbx_prod_cycle & "'" & "," & " & vbCrLf &" _
 & "'" & Me.txt_hmo_ind_ded & "'" & ")"

Please advice where I am messing up, thanks

请建议我搞砸的地方,谢谢

回答by n8.

You need to start the next line with "&"

您需要以“&”开始下一行

So:

所以:

strSQL = "insert into ded_limit_analysis (PDPD_ID,PDDS_DESC,PRODUCT_CAT,BASE_PDPD_ID,PROD_CYCLE,HMO_IND_DED) " _
  & vbCrLf & "values (" & "'" & Me.txt_pdpd_id & "'" & "," _
  & "'" & Me.txt_pdds & "'" & "," & "'" & Me.cbx_prod_type _
  & "'" & "," & "'" & Me.txt_base_pdpd & "'" & "," & "'" _
  & Me.cbx_prod_cycle & "'" & "," & vbCrLf _
  & "'" & Me.txt_hmo_ind_ded & "'" & ")"

Although, looking at the string, you can eliminate a lot of the "&" in your code. This would be cleaner:

虽然,查看字符串,您可以消除代码中的很多“&”。这会更干净:

strSQL = "insert into ded_limit_analysis (PDPD_ID, " _
                                       & "PDDS_DESC, " _
                                       & "PRODUCT_CAT, " _
                                       & "BASE_PDPD_ID, " _
                                       & "PROD_CYCLE, " _
                                       & "HMO_IND_DED) " _
      & " values ('" & Me.txt_pdpd_id & "', '" _
                     & Me.txt_pdds & "', '"  _
                     & Me.cbx_prod_type & "', '" _
                     & Me.txt_base_pdpd & "', '"  _
                     & Me.cbx_prod_cycle & "', '" _
                     & Me.txt_hmo_ind_ded & "')"

It's just much easier to understand this way. Also, you don't need "vbCrLf" in your SQL. It's just white space.

这样理解起来就容易多了。此外,您的 SQL 中不需要“vbCrLf”。这只是空白。

回答by Dustin

On the second string vbCrLfis within quotes and is seen as a string rather than a command.

第二个字符串vbCrLf在引号内,被视为字符串而不是命令。

Try this code in place of the problem line above:

试试这个代码代替上面的问题行:

strSQL = "insert into ded_limit_analysis (PDPD_ID,PDDS_DESC,PRODUCT_CAT,BASE_PDPD_ID,PROD_CYCLE,HMO_IND_DED) " & vbCrLf & _
 "values (" & "'" & Me.txt_pdpd_id & "'" & "," & "'" & Me.txt_pdds & "'" & "," & "'" & Me.cbx_prod_type & "'" & "," & "'" & Me.txt_base_pdpd & "'" & "," & "'" & Me.cbx_prod_cycle & "'" & "," & vbCrLf _
 & "'" & Me.txt_hmo_ind_ded & "'" & ")"

回答by Chito

I tried with this option after seraching other post and works, thanks for proving other solutions, appreciate that.

我在搜索其他帖子和作品后尝试使用此选项,感谢您提供其他解决方案,感谢。

 strSQL = "insert into ded_limit_analysis (PDPD_ID,PDDS_DESC,PRODUCT_CAT,BASE_PDPD_ID,PROD_CYCLE,HMO_IND_DED) " & vbCrLf & _
 "values (" & "'" & Me.txt_pdpd_id & "'" & "," & "'" & Me.txt_pdds & "'" & "," & "'" & Me.cbx_prod_type & "'" & "," & "'" & Me.txt_base_pdpd & "'" & "," & "'" & Me.cbx_prod_cycle & "'" & ","
 strSQL = strSQL & "'" & Me.txt_hmo_ind_ded & "'" & ")"