错误VB DoCmd.RunSQL更新

时间:2014-09-02 09:00:42

标签: access-vba

我试图在Access中的VB中运行更新查询,我在Update语句中遇到语法错误。有人可以帮忙吗?

Sub Update_Project()
Dim ProjectID, ProjectName, ProjectManager, sql_text as string

ProjectID = Wkbk.Sheets(1).Range("C5").Value

ProjectName = Wkbk.Sheets(1).Range("D5").Value
ProjectManager = Wkbk.Sheets(1).Range("E5").Value

sql_text = "UPDATE Projects (set ProjName, ProjManager) = '" & ProjectName & "," & ProjectManager & "' where ProgrammeID = " & ProjectID
DoCmd.RunSQL(sql_text)
End Sub

我认为他的错误可能在要更新的字段的串联中,但我无法解决。

谢谢!

1 个答案:

答案 0 :(得分:1)

你有UPDATE语句的语法混乱。此外,您需要显式声明变量,这将节省您很多麻烦。以下是您的需求。

Sub Update_Project()
    Dim ProjectID As Long, ProjectName As String, ProjectManager As String, sql_text As String

    ProjectID = Wkbk.Sheets(1).Range("C5").Value

    ProjectName = Wkbk.Sheets(1).Range("D5").Value
    ProjectManager = Wkbk.Sheets(1).Range("E5").Value

    sql_text = "UPDATE Projects SET ProjName = '" & ProjectName & "', ProjManager = '" & ProjectManager & _
               "' WHERE ProgrammeID = " & ProjectID
    DoCmd.RunSQL sql_text
End Sub