Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / May 2005

Tip: Looking for answers? Try searching our database.

me.undo parent if child is empty

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Angi - 08 May 2005 17:57 GMT
Only programmers could look at that subject and know what it means! <g>

I have a form (QuoteMain) with a subform (subQuoteDetails).  I'm trying
to undo the parent form if the form is closed with no records in the
sub.  I've done some research and found that the parent saves as soon
as the user tabs into the subform, but I didn't find how to get rid of
it.  I've tried this:

Private Sub Form_BeforeUpdate(Cancel As Integer)
   If Me.DetailsSub.Form.RecordsetClone Is Empty Then
       Me.Undo
   End If

End Sub

Didn't work, of course.  Should I be using the before_insert instead or
what?  TIA!
Alex White MCDBA MCSE - 08 May 2005 18:08 GMT
Hi,

2 ways to solve this problem, make the forms unbound so that no default
record saving goes on and you write your own function to grab the values off
the form into the database, sometimes I use the tag property of textboxes to
load previous values to allow for undoing on the form.

using dao/ado to copy/clone the recordset so that things can be undone.  If
the parent record is being written for the first time (new record), then why
not use dao/ado to just delete the record if there is no child record.

Signature

Regards

Alex White MCDBA MCSE
http://www.intralan.co.uk

> Only programmers could look at that subject and know what it means! <g>
>
[quoted text clipped - 13 lines]
> Didn't work, of course.  Should I be using the before_insert instead or
> what?  TIA!
Steve Schapel - 08 May 2005 20:40 GMT
Angi,

The Before Update event is not appropriate, but neither is Before
Insert.  Both of these events have already happened.  I suggest the
Unload event of the QuoteMain form would be the one.  And Undo won't
work either... once again, it's too late.  I would do it like this...
 DoCmd.SetWarnings False
 DoCmd.RunCommand acCmdDeleteRecord
 DoCmd.SetWarnings True

Signature

Steve Schapel, Microsoft Access MVP

> Only programmers could look at that subject and know what it means! <g>
>
[quoted text clipped - 13 lines]
> Didn't work, of course.  Should I be using the before_insert instead or
> what?  TIA!
Angi - 09 May 2005 02:58 GMT
Alex and Steve,
Thank you for the replies!  Ok, I tried combining both of your
suggestions, and now get the error:

Run-time error '2046':
The command or action "DeleteRecord" isn't available now.

Which would tell me that the record isn't being saved yet.  Which will
be possible if the user hasn't tabbed into the subform yet, but I tried
it with going into the subform and I still get the error.  Another
thing, in this code, aren't I saying to delete the subform's record?
I'm still learning how to use recordsets so, please, be patient.
Thanks!

Here's my new code:
Private Sub Form_Unload(Cancel As Integer)
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Set dbs = CurrentDb

Set rst = Forms!zquotemain1!DetailsSub.Form.RecordsetClone

   If rst.RecordCount = 0 Then
   'DoCmd.SetWarnings False
   DoCmd.RunCommand acCmdDeleteRecord
   'DoCmd.SetWarnings True
   
   End If

End Sub
Alex White MCDBA MCSE - 09 May 2005 07:25 GMT
Hi Angi,

this should help

change your code to the following

Private Sub Form_Unload(Cancel As Integer)
   on error goto Err_Form_Unload
   Dim dbs As DAO.Database
   Dim rst As DAO.Recordset
   Set dbs = CurrentDb
   Set rst = Forms!zquotemain1!DetailsSub.Form.RecordsetClone

   If rst.RecordCount = 0 Then
       'DoCmd.SetWarnings False
       DoCmd.RunCommand acCmdDeleteRecord
       'DoCmd.SetWarnings True
   End If
Exit_Form_Unload:
   exit sub
Err_Form_Unload:
   if err.number <> 2046 then
       msgbox err.number & " " & err.description
   end if
   resume next
End Sub

this allows your code to silently trap the error and ignore it, should do
the job.

Signature

Regards

Alex White MCDBA MCSE
http://www.intralan.co.uk

> Alex and Steve,
> Thank you for the replies!  Ok, I tried combining both of your
[quoted text clipped - 26 lines]
>
> End Sub
Steve Schapel - 09 May 2005 10:49 GMT
Angi,

Have you put this code on the Unload event of the subform?  I meant the
main form.

Another minor point... I would do it like this:
 Set rst = Me.DetailsSub.Form.RecordsetClone

Signature

Steve Schapel, Microsoft Access MVP

> Alex and Steve,
> Thank you for the replies!  Ok, I tried combining both of your
[quoted text clipped - 26 lines]
>
> End Sub
Angi - 09 May 2005 15:18 GMT
Steve,
Ok, did what you said and it does ignore the 2046, but now I'm getting
an error

3021 No current record

I tried ignoring that one too, but it's not working.  Now what?
Angi - 09 May 2005 15:21 GMT
Sorry about that, I meant Alex.

Steve,
Yes it is in the Unload event of the main form....not the subform.  I
changed it to the me.  I did that before and it didn't like
it...must've been some other error going on at the time.  Thanks for
that!
Angi - 10 May 2005 23:31 GMT
Anyone have any ideas on how to now get rid of the 3021 error?

I have:
Exit_Form_Unload:
   Exit Sub
Err_Form_Unload:
   If Err.Number <> 2046 Or Err.Number <> 3021 Then
       MsgBox Err.Number & " " & Err.Description
   End If
   Resume Next
Douglas J. Steele - 11 May 2005 01:29 GMT
If Err.Number <> 2046 And Err.Number <> 3021 Then

The way you have it, if Err.Number is 3021, then the first part of the
comparison (Err.Number <> 2046) is obviously True.

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

> Anyone have any ideas on how to now get rid of the 3021 error?
>
[quoted text clipped - 6 lines]
>    End If
>    Resume Next
Alex White MCDBA MCSE - 11 May 2005 06:04 GMT
Angi,

To follow Doug,

You Need

either

if err.number = 2046 then
   resume next ' or whatever you want to do
elseif err.number = 3021 then
   resume next ' or whatever you want to do
endif

or the select case

select case err.number
   case 2046
       resume next
   case 3021
       resume next
   case else
end select

Signature

Regards

Alex White MCDBA MCSE
http://www.intralan.co.uk

> If Err.Number <> 2046 And Err.Number <> 3021 Then
>
[quoted text clipped - 11 lines]
>>    End If
>>    Resume Next
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.