Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

How delete a record if certain criteria exists?

An expert helped me last week with this code:
    If Me.NewRecord = True Then
        If (Not IsNull(DLookup("[UniqueID]", "tblScheduleRecords", "[UniqueID] ='" & Me!txtUniqueID & "'"))) Then
            MsgBox "This client has already been scheduled for this Group, Schedule Date & Time."
            Cancel = True
        End If
    End If

Open in new window

But if the record already exists I want the record to be deleted from the datasheet.  How can I accomplish this?
Avatar of Jonathan Kelly
Jonathan Kelly
Flag of Ireland image

currentdb.execute "delete tblScheduleRecords  where UniqueID = '" & Me!txtUniqueID & "'" 
This datasheetview form, is that a subform?

Is this form bound to table tblScheduleRecords?
Avatar of SteveL13

ASKER

Gustav,
Yes, the datasheetview form is a sub-form.  Yes, it is bound to tblScheduleRecords
As you already have the record in the form, just delete it from the RecordsetClone without an external call:

Dim Records     As DAO.Recordset

If Me.NewRecord = True Then
    Set Records = Me.RecordsetClone

    Records.FindFirst "[UniqueID] = '" & Me!txtUniqueID.Value & "'"
    If Not Records.NoMatch Then
        Records.Delete
    End If
    Records.Close    
End If

Open in new window

The record would not yet be in the form because the user gets a warning with:
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_Form_BeforeUpdate

    If Me.NewRecord = True Then
        If (Not IsNull(DLookup("[UniqueID]", "tblScheduleRecords", "[UniqueID] ='" & Me!txtUniqueID & "'"))) Then
            MsgBox "This client has already been scheduled for this Group, Schedule Date & Time."
            Cancel = True
        End If
    End If

Exit_Form_BeforeUpdate:
    Exit Sub

Err_Form_BeforeUpdate:
    MsgBox "Error Number: " & Err.Number & vbCrLf & "Error Description: " & Err.Description & vbCrLf & "Error Source: " & Err.Source
    Resume Exit_Form_BeforeUpdate

End Sub

Open in new window

I just want the user to get the warning, and then clear the record entry in the subform.
Then all you need is to cancel the insert of the new record:

Private Sub Form_BeforeInsert(Cancel As Integer)

    Dim Records     As DAO.Recordset

    Set Records = Me.RecordsetClone

    Records.FindFirst "[UniqueID] = '" & Me!txtUniqueID.Value & "'"
    If Not Records.NoMatch Then
        MsgBox "This client has already been scheduled for this Group, Schedule Date & Time."
        Cancel = True
    End If
    Records.Close    

End Sub

Open in new window

I put your latest code in the beforeinsert event of the sub-form and it allows for a duplicate entry.  ??
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial