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

时间:2012-02-18 05:50:26

标签: sql ms-access ado syntax-error

我正在使用ADO删除MS Access 2007数据库中的记录,并且完全不知道为什么我的SQL代码会出现此语法错误。它声称FROM子句中有错误,但我没有看到它。我使用相同的表直接从另一个模块中的工作SQL语句中获取FROM子句。我已经将代码输入到新查询的SQL视图中,它运行得很好。这是代码:

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

和想法?提前谢谢!

1 个答案:

答案 0 :(得分:1)

您尝试运行操作查询但使用记录集(需要选择查询)。

试试这个:

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

此外,如果[MsgID]是一个字符串,则需要将您的值括在引号中:

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