pass string variable without quotes in query vba

前端 未结 3 1216
不知归路
不知归路 2021-01-27 07:44
Dim StoreNoToUpdate As String
Dim Marsha As String

StoreNoToUpdate = \"ABCXYZ123\"

Marsha = \"hi\"

db.Execute \"Update TblLod         


        
相关标签:
3条回答
  • 2021-01-27 07:49

    Try:

    Dim StoreNoToUpdate As String
    Dim Marsha As String
    
    StoreNoToUpdate = "ABCXYZ123"
    
    Marsha = "hi"
    
    Db.Execute "Update TblLodgingReport set [MarshaCode]='" & Marsha & "'where [Store Number ID]= 'ABCXYZ123'"
    
    0 讨论(0)
  • 2021-01-27 07:55

    You need to get away from SQL concatenation and start using parameters.

    Query with parameters:

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

    Calling the above query in VBA:

    With CurrentDb().QueryDefs("qryName")
        .Parameters("[prmMarshaCode]").Value = Marsha 
        .Parameters("[prmStoreNoToUpdate]").Value = StoreNoToUpdate 
        .Execute dbFailOnError
    End With
    
    0 讨论(0)
  • 2021-01-27 07:59

    Try changing the following line

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

    Its because 'Marsha' means you are literally sending Marsha as string and not using the variable.

    0 讨论(0)
提交回复
热议问题