PC Review


Reply
Thread Tools Rate Thread

Inserting a beginning record in a subform

 
 
DaveJJ
Guest
Posts: n/a
 
      7th Feb 2011
I have the following procedure that runs when I create or update a
record on a main-form and when an attached sub-form has zero records
Its purpose is to insert a default beginning record in the sub-form
table when a new record is created on the main form. This action is
only supposed to occur once on the sub-form so it is not a default
value in the normal sense. The following code works well but to be
honest I'm not sure why or if its the best approach.

The query that is fired is an append query. I was initially got
update errors with this procedure until I added DBEngine.Idle
dbRefreshCache, DoEvents in the code.

Private Sub Form_AfterUpdate()

If Nz(DCount("*", "tblRouting", "[JobEnvelopeNo] = " & Me.txtID),
0) = 0 Then
DoCmd.SetWarnings False
DoCmd.OpenQuery "qryInsertRoutingJobReview"
DoCmd.SetWarnings True
DBEngine.Idle dbRefreshCache
DoEvents
Me.sfrmRouting1.Form.Requery
Me.sfrmRoutingQuickLook.Form.Requery
End If

End Sub

Would appreciate any insights to better understand what is happening
here and if this is fully optimized code.
 
Reply With Quote
 
 
 
 
John W. Vinson
Guest
Posts: n/a
 
      7th Feb 2011
On Mon, 7 Feb 2011 08:52:23 -0800 (PST), DaveJJ <(E-Mail Removed)> wrote:

>I have the following procedure that runs when I create or update a
>record on a main-form and when an attached sub-form has zero records
>Its purpose is to insert a default beginning record in the sub-form
>table when a new record is created on the main form. This action is
>only supposed to occur once on the sub-form so it is not a default
>value in the normal sense. The following code works well but to be
>honest I'm not sure why or if its the best approach.
>
>The query that is fired is an append query. I was initially got
>update errors with this procedure until I added DBEngine.Idle
>dbRefreshCache, DoEvents in the code.
>
>Private Sub Form_AfterUpdate()
>
> If Nz(DCount("*", "tblRouting", "[JobEnvelopeNo] = " & Me.txtID),
>0) = 0 Then
> DoCmd.SetWarnings False
> DoCmd.OpenQuery "qryInsertRoutingJobReview"
> DoCmd.SetWarnings True
> DBEngine.Idle dbRefreshCache
> DoEvents
> Me.sfrmRouting1.Form.Requery
> Me.sfrmRoutingQuickLook.Form.Requery
> End If
>
>End Sub
>
>Would appreciate any insights to better understand what is happening
>here and if this is fully optimized code.


The problem is that a) a Subform opens *before* its mainform, and b) if you're
enforcing referential integrity, a record cannot be created in the child table
unless there is already a record in the parent table.

What real-life need is served by creating an (empty? placeholder?) record in
the child table on any change in a parent record?
--

John W. Vinson [MVP]
Microsoft's replacements for these newsgroups:
http://social.msdn.microsoft.com/For...-US/accessdev/
http://social.answers.microsoft.com/.../en-US/addbuz/
and see also http://www.utteraccess.com
 
Reply With Quote
 
 
 
 
DaveJJ
Guest
Posts: n/a
 
      7th Feb 2011
On Feb 7, 3:31*pm, John W. Vinson <jvinson@STOP_SPAM.WysardOfInfo.com>
wrote:
> On Mon, 7 Feb 2011 08:52:23 -0800 (PST), DaveJJ <garypalm...@gmail.com> wrote:
> >I have the following procedure that runs when I create or update a
> >record on a main-form and when an attached sub-form has zero records
> >Its purpose is to insert a default beginning record in the sub-form
> >table when a new record is created on the main form. *This action is
> >only supposed to occur once on the sub-form so it is not a default
> >value in the normal sense. *The following code works well but to be
> >honest I'm not sure why or if its the best approach.

>
> >The query that is fired is an append query. *I was initially got
> >update errors with this procedure until I added *DBEngine.Idle
> >dbRefreshCache, DoEvents in the code.

>
> >Private Sub Form_AfterUpdate()

>
> > * *If Nz(DCount("*", "tblRouting", "[JobEnvelopeNo] = " & Me.txtID),
> >0) = 0 Then
> > * * * *DoCmd.SetWarnings False
> > * * * *DoCmd.OpenQuery "qryInsertRoutingJobReview"
> > * * * *DoCmd.SetWarnings True
> > * * * *DBEngine.Idle dbRefreshCache
> > * * * *DoEvents
> > * * * *Me.sfrmRouting1.Form.Requery
> > * * * *Me.sfrmRoutingQuickLook.Form.Requery
> > * *End If

>
> >End Sub

>
> >Would appreciate any insights to better understand what is happening
> >here and if this is fully optimized code.

>
> The problem is that a) a Subform opens *before* its mainform, and b) if you're
> enforcing referential integrity, a record cannot be created in the child table
> unless there is already a record in the parent table.
>
> What real-life need is served by creating an (empty? placeholder?) recordin
> the child table on any change in a parent record?
> --
>
> * * * * * * *John W. Vinson [MVP]
> *Microsoft's replacements for these newsgroups:
> *http://social.msdn.microsoft.com/For...-US/accessdev/
> *http://social.answers.microsoft.com/.../en-US/addbuz/
> *and see alsohttp://www.utteraccess.com


This after-update event occurs in the main form not the sub form. The
query does not insert an empty record. It enters the beginning
station in a manufacturing routing plan and only on a new record. It
does actually work quite well but my sense it might cause problems
down the road that's why I posted it. I haven't enforced referential
integrity yet but I was planning to.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
symbols in the beginning of the line and at the beginning of theline in style news.microsoft.com Microsoft Word New Users 3 11th Apr 2007 12:12 PM
Double click record in subform to go to new record in Main (and subform) Jpipher@gmail.com Microsoft Access 14 3rd Feb 2007 08:06 AM
Wierd subform behaviour, subform not cycling through after updating a subform record Mikal Microsoft Access Forms 2 25th May 2006 07:15 PM
code to delete a record in one subform when a record in a related subform is deleted Helen Microsoft Access Form Coding 3 3rd Apr 2005 10:01 PM
Urgent !!! - Values from Subform - #Error if no records in Subform and Only grabs first subform record Greg Microsoft Access Forms 0 17th Feb 2005 02:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:31 AM.