1

我有一个来自 'Crew' 的子组件,其中包含名为 CrewName、KitNumber 和 ActionDate 的 3 个字段文本框。此表单使用来自按钮的数据填充。我想要做的是让用户输入一个数字,在 Crew 子表单之外的这个名为“ClearEntry”的未绑定文本字段中输入“111111”。然后,我在名为“ClearSelected”的下方有一个命令按钮,单击该按钮时,将清除 Crew 子表单中的相应记录。所以基本上我想在这个未绑定的文本中输入文本,点击按钮,让那个数字与 Crew Subform 中的相同数字对应,然后删除该表单中的记录。我现在拥有的命令按钮代码如下。

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

这可以做到吗?

4

2 回答 2

1

argument not optional”编译错误是由.=之后的符号引起的DoCmd.RunSQL。摆脱它:

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

然后你会得到一个错误,抱怨“标准类型不匹配”,因为Crew.KitNumber是数字而不是文本。所以不要在Me.txtClearKitEntry.

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

但是我会建议这种方法而不是DoCmd.RunSQL. 当您不使用RunSQL时,您不会有SetWarnings关闭 ( False) 的动力。

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

On Error GoTo ErrorHandler

    strSql = "DELETE FROM Crew WHERE KitNumber = " & Me.txtClearKitEntry
    CurrentDb.Execute strSql, dbFailOnError
    Me.Crew.Requery

ExitHere:
    On Error GoTo 0
    Exit Sub

ErrorHandler:
    strMsg = "Error " & Err.Number & " (" & Err.Description _
        & ") in procedure ClearKitSelected_Click of Form_Search"
    MsgBox strMsg
    GoTo ExitHere
End Sub
于 2013-07-09T14:00:02.013 回答
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
于 2018-11-21T22:23:59.717 回答