MS ACCESS 2007 - 使用未绑定的值删除特定记录

时间:2013-07-08 18:44:15

标签: sql ms-access access-vba

我有一个子'Crew',里面有3个字段文本框,名为CrewName,KitNumber和ActionDate。此表单将填充来自按钮的数据。我想要做的是让用户输入一个数字,在Crew子表单之外的名为'ClearEntry'的未绑定文本字段中输入“111111”。然后我在名为“ClearSelected”的命令按钮下面,当单击时,将清除我的Crew子窗体中的相应记录。因此,基本上我想在此未绑定中输入文本,点击按钮,并使该编号与Crew子表单中的相同编号对应,并删除该表单中的记录。我现在用于命令按钮的代码如下所示。

Private Sub ClearSelected_Click()
DoCmd.SetWarnings False
DoCmd.RunSQL = "DELETE FROM Crew WHERE KitNumber = '" & Me.ClearEntry & "'"
Crew.Form.Requery
End Sub

可以这样做吗?

2 个答案:

答案 0 :(得分:1)

“参数不是可选的”编译错误是由=之后的DoCmd.RunSQL符号引起的。摆脱它:

DoCmd.RunSQL "DELETE * FROM Crew WHERE KitNumber = '" & Me.txtClearKitEntry& "'"

然后,您会收到错误抱怨“标准中的类型不匹配”,因为Crew.KitNumber是数字而非文字。因此,请勿在{{1​​}}附近添加单引号。

Me.txtClearKitEntry

但是我会建议这种方法而不是DoCmd.RunSQL "DELETE * FROM Crew WHERE KitNumber = " & Me.txtClearKitEntry 。当您不使用DoCmd.RunSQL时,您将无法将RunSQL关闭(SetWarnings)。

False

答案 1 :(得分:0)

这是我最终能够使其正常工作的方式:

Private Sub cmdDelete_Click()
Dim strMsg As String
Dim strSql As String

If Me.Dirty Then
Me.Dirty = False
End If
On Error GoTo ErrorHandler
Const MESSAGETEXT = "Are you sure you wish to delete the current record?"
 If MsgBox(MESSAGETEXT, vbYesNo + vbQuestion, "Confirm") = vbNo Then
     Cancel = True
 End If
 strSql = "DELETE FROM Item WHERE ID = " & Me.txtID2
 CurrentDb.Execute strSql, dbFailOnError

 MsgBox "The Item list has been updated"
 Clear
 List40.Requery

 ExitHere:
 On Error GoTo 0
 Exit Sub

 ErrorHandler:
 strMsg = "Error " & Err.Number & " (" & Err.Description _
     & ") in procedure Delete of Items"
 MsgBox strMsg
 GoTo ExitHere

 End Sub