使用VBA更新查询

时间:2015-04-23 11:01:29

标签: sql excel vba

我试图通过Excel更新sql中的记录。我在excel中的一个按钮上写下了一些代码,以便在sql server中插入和更新记录。我设法插入记录但在更新查询时收到错误。我已经检查了stackoverflow上的解决方案,但仍然收到错误。任何人都可以在以下代码行中检查我做错了什么

  conn.Execute = "UPDATE dbo.EmployeeInfo" _
        & "SET Week= ('" & Week& "') " _
        & "Time= ('" & Time & "') " _
        & "DateFrom = ('" & DateFrom & "') " _
        & "DatoTo = ('" & DatoTo & "') " _
        & "Name= ('" & Name & "') " _
        & "Description= ('" & Description & "') " _
        & "Codes= ('" & Codes& "')" _
        & "WHERE Week= (" & .Cells(iLoop, 1) & ") 
       AND DateFrom = (" & .Cells(iLoop, 3) & ")
       AND DatoTo = (" & .Cells(iLoop, 4) & ") 
       AND Name= (" & .Cells(9, oLoop) & ")"

1 个答案:

答案 0 :(得分:1)

每列之间需要逗号...

  conn.Execute = "UPDATE dbo.EmployeeInfo" _
        & "SET Week= ('" & Week& "'), " _
        & "Time= ('" & Time & "'), " _
        & "DateFrom = ('" & DateFrom & "'), " _
        & "DatoTo = ('" & DatoTo & "'), " _
        & "Name= ('" & Name & "'), " _
        & "Description= ('" & Description & "'), " _
        & "Codes= ('" & Codes& "')" _
        & "WHERE Week= (" & .Cells(iLoop, 1) & ") 
       AND DateFrom = (" & .Cells(iLoop, 3) & ")
       AND DatoTo = (" & .Cells(iLoop, 4) & ") 
       AND Name= (" & .Cells(9, oLoop) & ")"