简体   繁体   中英

Delete record from table in Access

I am working with two tables Mitigations and New Control IDs in an Access database. The Mitigations table has a form. As part of that form, a control ID can be added to the New Control IDs table. My code for that works fine. I am trying to add the ability to delete a record from the New Control IDs table via the Mitigations form.

The user will enter a string in Text55 and after updating that field, the corresponding record in the New Control IDs table should be deleted.

Here is the code I have for that:

Private Sub Text55_AfterUpdate()

'removing record with Archer Control ID from New Control IDs table
Dim dbNewInitiatives As DAO.Database
Dim rstMitigations As DAO.Recordset
Dim strSQL As String

    Set dbNewInitiatives = CurrentDb
    strSQL = "SELECT * FROM [New Control IDs] WHERE ([Mitigation ID] = " & Me.[Mitigation ID].Value & ") AND ([Archer Control ID] = '" & Me.[Text55].Value & "') ORDER BY [ID]"
    Set rstMitigations = dbNewInitiatives.OpenRecordset(strSQL, dbOpenDynaset)

    Do While Not rstMitigations.EOF
        rstMitigations.Delete
        rstMitigations.MoveNext
    Loop

rstMitigations.Close

Set rstMitigations = Nothing
Set dbNewInitiatives = Nothing

End Sub

The above code successfully finds all of the records in New Control IDs table that meet the criteria and deletes them. Thank you!

Consider running a DELETE action query using the Database.Execute method without the need of a DAO recordset:

Set dbNewInitiatives = CurrentDb
strSQL = "DELETE FROM [New Control IDs]" _
           & " WHERE ([Mitigation ID] = " & Me.[Mitigation ID].Value & ")" _
           & " AND ([Control ID] = '" & Me.[Text55].Value & "')"

dbNewInitiatives.Execute strSQL, dbFailOnError

Definitely consider Parfait's answer, but it is always good to understand what the problem was in the first place. To me it looks like the logic in your loop is probably incorrect.

Unless you provide more detail, I'll first assume that [New Control IDs].[ID] is a unique key on that table. If that is the case, then you are storing that unique ID in the variable strID but then moving to the next record before actually attempting to delete the record. In this case, the If statement in the loop will always be false since the current record's [ID] would not equal the previous record's [ID] value.

...
Do While Not rstMitigations.EOF
    '* This compares the previous row's [ID] with the current row's [ID]  '
    If rstMitigations![ID] = strID Then
        rstMitigations.Delete
...

If the [ID] values are not unique , then your code would only delete subsequent rows that matched the first in a series of identical [ID] values. It would leave the first row of each unique [ID] value untouched (ie not deleted). If there is only one of a particular [ID] value, that row will never be deleted. Is that what you want?

Notice that Parfait's action query will delete all of the same records returned by your SELECT query. Just for the record, to accomplish this with the recordset, just simplify it to be

....
Set dbNewInitiatives = CurrentDb
strSQL = "SELECT * FROM [New Control IDs] WHERE ([Mitigation ID] = " _
        & Me.[Mitigation ID].Value & ") AND ([Control ID] = " _
        & Me.[Text55].Value & ") ORDER BY [ID]"
Set rstMitigations = dbNewInitiatives.OpenRecordset(strSQL, dbOpenDynaset)

Do While Not rstMitigations.EOF
    rstMitigations.Delete
    rstMitigations.MoveNext
Loop
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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