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?
Microsoft Access

Avatar of undefined
Last Comment
Gustav Brock
Avatar of Jonathan Kelly
Jonathan Kelly
Flag of Ireland image

currentdb.execute "delete tblScheduleRecords  where UniqueID = '" & Me!txtUniqueID & "'" 
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

This datasheetview form, is that a subform?

Is this form bound to table tblScheduleRecords?
Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

Gustav,
Yes, the datasheetview form is a sub-form.  Yes, it is bound to tblScheduleRecords
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

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

Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

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.
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

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

Avatar of SteveL13
SteveL13
Flag of United States of America image

ASKER

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo