使用SQL UPDATE访问

时间:2019-02-23 17:26:36

标签: access-vba

我正在使用两个Access数据库(前端和后端)。

我的查询代码有效,但我可以将其用于更新数据库。我在做什么错了?

第25行的DoCmd.RunSQL strSql收到运行时错误3078。

Set cnn = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=" & CurrentProject.Path & "\DB_Cryk.accdb"

cnn.Open strConnection

MemberID = txtMemberID.Value

strSql = "UPDATE Cryk " & _
"SET Membership = '" & txtMembership.Value & "', " & _
"    Memberstatus = '" & txtMemberstatus.Value & "', " & _
"    Membername = '" & txtMembername.Value & "', " & _
"    Memberaddress = '" & txtMemberaddress.Value & "', " & _
"    Memberzip = '" & txtMemberzip.Value & "', " & _
"    Membercity = '" & txtMembercity.Value & "', " & _
"    Memberphone = '" & txtMemberphone.Value & "', " & _
"    Membermail = '" & txtMembermail.Value & "', " & _
"    Memberyear = '" & txtMemberyear.Value & "', " & _
"    Dateofbirth = '" & txtDateofbirth.Value & "', " & _
"    Memberno = '" & txtMemberno.Value & "', " & _
"    Memberfee = '" & txtMemberfee.Value & "', " & _
"    Memberpayment = '" & txtMemberpayment.Value & "'" & _
"WHERE MemberID= '" & MemberID & "'"

DoCmd.RunSQL strSql

cnn.Close
Set cnn = Nothing

1 个答案:

答案 0 :(得分:0)

错误3078指示目标表在您的数据库中不存在。

请注意,尽管打开了与数据库DB_Cryk.accdb的ADO连接,但是您仍使用DoCmd.RunSQL方法执行SQL语句,该方法在当前数据库上运行。 / p>

相反,如果希望在DB_Cryk.accdb数据库中执行SQL,则应使用ADODB Connection对象的Execute方法,例如:

cnn.Execute strsql

关于查询参数化,您可能希望参考this superb answer,特别是“使用ADO”部分。