【问题标题】:pass string variable without quotes in query vba在查询vba中传递不带引号的字符串变量
【发布时间】:2018-04-30 14:18:58
【问题描述】:
Dim StoreNoToUpdate As String
Dim Marsha As String

StoreNoToUpdate = "ABCXYZ123"

Marsha = "hi"

db.Execute "Update TblLodgingReport set [MarshaCode]=  'Marsha'  where [Store Number ID]= 'ABCXYZ123'"

我正在尝试在“ABCXYZ123”位置更新“hi”。但它不是粘贴“hi”,而是粘贴“Marsha”。

【问题讨论】:

    标签: sql vba ms-access


    【解决方案1】:

    您需要摆脱 SQL 连接并开始使用参数。

    带参数查询:

    PARAMETERS [prmMarshaCode] Text (50), [prmStoreNoToUpdate] Text (50);
    UPDATE TblLodgingReport SET [MarshaCode] = [prmMarshaCode]
    WHERE [Store Number ID] = [prmStoreNoToUpdate];
    

    在 VBA 中调用上述查询:

    With CurrentDb().QueryDefs("qryName")
        .Parameters("[prmMarshaCode]").Value = Marsha 
        .Parameters("[prmStoreNoToUpdate]").Value = StoreNoToUpdate 
        .Execute dbFailOnError
    End With
    

    【讨论】:

      【解决方案2】:

      试试:

      Dim StoreNoToUpdate As String
      Dim Marsha As String
      
      StoreNoToUpdate = "ABCXYZ123"
      
      Marsha = "hi"
      
      Db.Execute "Update TblLodgingReport set [MarshaCode]='" & Marsha & "'where [Store Number ID]= 'ABCXYZ123'"
      

      【讨论】:

      • 不应鼓励字符串连接。改用 SQL 参数
      【解决方案3】:

      尝试更改以下行

      db.Execute "Update TblLodgingReport set [MarshaCode]='" & Marsha & "' where [Store Number ID]='" & StoreNoToUpdate & "'"
      

      这是因为'Marsha' 意味着您实际上是在将 Marsha 作为字符串发送,而不是使用变量。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-10-24
        • 2016-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-10
        • 1970-01-01
        相关资源
        最近更新 更多