使用SQL UPDATE访问VBA

问题描述 投票:0回答:1

我正在使用两个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
access-vba
1个回答
0
投票

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

请注意,虽然您打开了与数据库DB_Cryk.accdb的ADO连接,但您可以使用DoCmd.RunSQL方法执行SQL语句,该方法在当前数据库上运行。

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

cnn.Execute strsql

在涉及查询参数化的地方,您可能希望参考this superb answer,特别是“使用ADO”部分。

© www.soinside.com 2019 - 2024. All rights reserved.