简体   繁体   English

From子句中的语法错误使用ADO删除记录

[英]Syntax Error in From clause Deleting record with ADO

I'm using ADO to delete a record in my MS Access 2007 database and am at a total loss as to why I'm getting this syntax error for my SQL code. 我正在使用ADO来删除MS Access 2007数据库中的记录,而对于为什么我的SQL代码遇到此语法错误,我全然不知所措。 It claims there is an error in the FROM clause but I do not see it. 它声称FROM子句中有错误,但我看不到它。 I have taken the FROM clause directly from a working SQL statement in another module using the same table. 我已经直接从另一个使用同一表的模块中的有效SQL语句中获取FROM子句。 I've entered the code into the SQL View of a new query and it runs just fine. 我已经将代码输入到新查询的SQL视图中,并且运行正常。 Here is the code: 这是代码:

Private Sub cmdDeleteMessage_Click()

  If MsgBox("Once you delete a message, it cannot be undone." & _ 
     "Are you sure you want to delete this message?", vbYesNo) = vbYes Then
    Dim sql As String
    Dim rsDel As New ADODB.Recordset
    rsDel.CursorType = adOpenDynamic
    rsDel.LockType = adLockOptimistic
    sql = "DELETE * FROM [Staff Notes] WHERE [MsgID] = " & Me.txtMsgID.Value & ";"

    rsDel.Open sql, CurrentProject.AccessConnection, , , adCmdTable

      With rsDelete
          .Update
          .Close
      End With
  End If

End Sub

And Ideas? 和想法? Thanks in advance! 提前致谢!

You're trying to run an action query but using a recordset (which expects a select query). 您正在尝试运行操作查询,但使用的是记录集(需要选择查询)。

Try this: 尝试这个:

sql = "DELETE * FROM [Staff Notes] WHERE [MsgID] = " & Me.txtMsgID.Value & ";"
CurrentProject.AccessConnection.Execute sql, , adExecuteNoRecords 

Also, if [MsgID] is a string, you need to enclose your value in quotes: 另外,如果[MsgID]是字符串,则需要将值用引号引起来:

sql = "DELETE * FROM [Staff Notes] WHERE [MsgID] = " & Chr$(34) & Me.txtMsgID.Value & Chr$(34) & ";"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM