使用 acCmdDeleteRecord 在 MS Access 中删除连续表单上的记录

Deleting record on Continuous form in MS Access using acCmdDeleteRecord

我有只有 2 个字段和一个按钮的连续表单。

Design View

'

Form View

下面是删除按钮的代码。

Private Sub cmdDelete_Click()
     DoCmd.RunCommand acCmdDeleteRecord
     Debug.Print "IDWeb - " & Me.IDWeb
     'Here I Execute a Web API to Delete Data based on Me.IDWeb
End Sub

问题 - Web API 没有得到 Me.IDWeb Value 。有时 IDWeb 被正确捕获,有时则不正确。

Edit1 :我尝试了下面的代码。但是还是有问题。

我猜用户删除当前记录时可能在另一条记录上。因此问题。但该记录当前已在 MS Access 中删除。唯一的问题是 Me.IDWeb 没有被正确捕获,因此我的网络 API 失败了。

Private Sub cmdDelete_Click()
    Me.WebType.SetFocus
    If Me.IDWeb = "" Or Me.IDWeb = vbNull Or Me.IDWeb = vbNullString Then
        MsgBox "No Record Selected"
    Else
        DoCmd.RunCommand acCmdSelectRecord
        DoCmd.RunCommand acCmdDeleteRecord
        Debug.Print "IDWeb - " & Me.IDWeb
        'Here I execute my Web API to Delete based on Me.IDWeb
    End If
End Sub

就我个人而言,我不使用 DoCmd。您正在尝试获取已删除记录的 IdWeb。 假设您的 IDWeb 是一个数字(如果没有使用适当的数据类型),以下应该有效

Dim lngIdWeb as long
lngIdWeb = nz(Me.IdWeb, 0)
If lngIdWeb > 0 Then
Me.RecordsetClone.FindFirst "IdWeb = " & lngIdWeb
If Not Me.RecordsetClone.NoMatch Then
Me.RecordsetClone.Delete
End If
Debug.Print "IdWeb - " & lngIdWeb
End If

解决方案是首先获取Me.IDWeb并将其存储在变量中,

然后 运行 acCmdDeleteRecord 然后