VBA多线字符串问题

时间:2014-11-24 19:34:09

标签: string vba

我没有太多编写VBA脚本的经验,我几乎输掉了多线字符串。我错过的地方是当我尝试将字符串分成多行时,你能不能帮我结束

在拆分之前,工作字符串位于以下位置:

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 & "'" & ")"

一个不工作的是下面,我试图分割线,因为我有很多列要包含在插入中,它是超过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 & "'" & ")"

请告知我搞砸的地方,谢谢

3 个答案:

答案 0 :(得分:9)

您需要使用"&"

开始下一行

所以:

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 & "'" & ")"

虽然看着字符串,你可以消除很多"&"在你的代码中。这会更清洁:

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 & "')"

这种方式更容易理解。此外,你不需要" vbCrLf"在你的SQL中。它只是空白区域。

答案 1 :(得分:0)

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

尝试使用此代码代替上面的问题行:

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 & "'" & ")"

答案 2 :(得分:0)

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

 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 & "'" & ")"