SteveL13
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
But if the record already exists I want the record to be deleted from the datasheet. How can I accomplish this?
currentdb.execute "delete tblScheduleRecords where UniqueID = '" & Me!txtUniqueID & "'"
This datasheetview form, is that a subform?
Is this form bound to table tblScheduleRecords?
Is this form bound to table tblScheduleRecords?
ASKER
Gustav,
Yes, the datasheetview form is a sub-form. Yes, it is bound to tblScheduleRecords
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
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
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
ASKER
I put your latest code in the beforeinsert event of the sub-form and it allows for a duplicate entry. ??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.