Full Version: how long does me.newrecord last
UtterAccess Forums > Microsoft® Access > Access Forms
I have a main form with a couple of subforms , if the user enters something in either the mainform or the subform but chooses to close the form before updating , how can i get rid of the records added , i was thinking about using on my close button something like the following.
if me.newrecord then delete all from the tables but then i'm not sure if it will delete the corresponding records in the subform tables....could i use me.undo for something like this?
If you will use the form's Before Insert event, then you can trap when a new record is getting saved
like to use something like:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.CompanyName) Then
   MsgBox "You must enter a company name.", vbCritical, "Data entery error..."
   DoCmd.GoToControl "CompanyName"
   Cancel = True
   Exit Sub
End If
If Me.NewRecord Then
    If MsgBox("Data will be saved, Are you Sure?", vbYesNo, "Confirm") = vbNo Then
        Cancel = True
        ' run code for new record before saving
    End If
    If MsgBox("Data will be modified, Are you Sure?", vbYesNo, "Confirm") = vbNo Then
        Cancel = True
       ' run code before an existing record is saved
       ' example: update date last modified
    End If
End If
' if the save has been canceled, then ask to Undo changes
If Cancel Then
    If MsgBox("Do you want to undo any changes?", vbYesNo, "Confirm") = vbYes Then
    End If
End If
End Sub

Note: If a sub form is linked to a parent form so that Access can automatically set the foreign then the parent record MUST be saved before an records can be add into the sub form.
Onormally add a delete button that will delete the child records then the parent form's record. This way once records are entered int the sub form, a user can easily delete the parent record and all the child/related records.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.