我正在使用两个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
错误3078表示数据库中不存在目标表。
请注意,虽然您打开了与数据库DB_Cryk.accdb
的ADO连接,但您可以使用DoCmd.RunSQL
方法执行SQL语句,该方法在当前数据库上运行。
相反,如果您希望在DB_Cryk.accdb
数据库中执行SQL,则应使用ADODB Connection对象的Execute
方法,例如:
cnn.Execute strsql
在涉及查询参数化的地方,您可能希望参考this superb answer,特别是“使用ADO”部分。