PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 1.00 average.

Add New SubRecord if one doesn't exist.

 
 
=?Utf-8?B?Q3lkbmV5?=
Guest
Posts: n/a
 
      18th Feb 2005
Another basic question ... and my brain is fried... Please help.
Two tables are linked by RecID (one to many with referential integrity
added). The 1st table has RecID entered by AutoNumber upon the addition of
new records. The 2nd table doesn't have the RecID (number field) filled in
until a new record is added by clicking a button from the first table's form.
I want the "onclick" to FIRST look to see if the record is there and then
move to that record. If it is not there, then it needs to add the new record
and assign the correct RecID to the new record in the 2nd table. I'm so
close, but .... how...??
--
THX cs
 
Reply With Quote
 
 
 
 
Tim Ferguson
Guest
Posts: n/a
 
      18th Feb 2005
=?Utf-8?B?Q3lkbmV5?= <(E-Mail Removed)> wrote in
news:E86BD772-1D4A-44A1-A2A2-(E-Mail Removed):

> I want the "onclick" to FIRST look to see if the record is there and
> then move to that record. If it is not there, then it needs to add the
> new record and assign the correct RecID to the new record in the 2nd
> table. I'm so close, but .... how...??
>


' look to see if it's there already
if 0 = dcount("*", "MyTable2", "IDNumber = " & dwCurrentID) Then

' not there; put it there
strSQL = "insert into MyTable2 (IDNumber) " & vbnewline & _
"values (" & dwCurrentID & ")"

' and run it. The ADODB version is a bit different but you get
' the picture
db.Execute strSQL, dbFailOnError

' you need to force the form to see it
Forms("MyForm2").Refresh

end if

' we need to synchronise the form
with forms("MyForm2").recorsetclone

' go seek the new (or found) record
.findfirst "IDNumber = " & dwcurrentID
' and use the bookmark to place it
forms("MyForm2").bookmark = .bookmark

End with


Hope that helps


Tim F


 
Reply With Quote
 
Steve Schapel
Guest
Posts: n/a
 
      18th Feb 2005
Cydney,

One way this type of situation is often handled is to put a continuous
view form, based on 2nd table, as a subform on the 1st table form. Is
this an option for you? If so, I would expect your problem disappears.

Otherwise, I am not 100% clear in the interpretation of your question.
I assume you mean that at the moment clicking the button opens a
separate form for 2nd table records? And I assume you mean you want the
procedure to add a new table 2 record if there are *no* corresponding
table 2 records? If so, I suppose your Click event code could include
something like this...

If DCount("*","2nd table","[RecID]=" & Me.RecID)=0 Then
CurrentDb.Execute "INSERT INTO [2nd table] ( RecID ) VALUES ( " &
Me.RecID & " )"
End If

--
Steve Schapel, Microsoft Access MVP

Cydney wrote:
> Another basic question ... and my brain is fried... Please help.
> Two tables are linked by RecID (one to many with referential integrity
> added). The 1st table has RecID entered by AutoNumber upon the addition of
> new records. The 2nd table doesn't have the RecID (number field) filled in
> until a new record is added by clicking a button from the first table's form.
> I want the "onclick" to FIRST look to see if the record is there and then
> move to that record. If it is not there, then it needs to add the new record
> and assign the correct RecID to the new record in the 2nd table. I'm so
> close, but .... how...??

 
Reply With Quote
 
=?Utf-8?B?Q3lkbmV5?=
Guest
Posts: n/a
 
      18th Feb 2005
This is great. Both of your answers were helpful. Thank you.
However... and remember my brain was fried.... I resolved the issue myself.
Going back again and looking at the 2nd form (based on a table) I noticed
that the form was set to not allow new records added. Therefore it didn't
allow the referential integrity to take place as it naturally would. Anyway..
resolved. Thanks!

"Steve Schapel" wrote:

> Cydney,
>
> One way this type of situation is often handled is to put a continuous
> view form, based on 2nd table, as a subform on the 1st table form. Is
> this an option for you? If so, I would expect your problem disappears.
>
> Otherwise, I am not 100% clear in the interpretation of your question.
> I assume you mean that at the moment clicking the button opens a
> separate form for 2nd table records? And I assume you mean you want the
> procedure to add a new table 2 record if there are *no* corresponding
> table 2 records? If so, I suppose your Click event code could include
> something like this...
>
> If DCount("*","2nd table","[RecID]=" & Me.RecID)=0 Then
> CurrentDb.Execute "INSERT INTO [2nd table] ( RecID ) VALUES ( " &
> Me.RecID & " )"
> End If
>
> --
> Steve Schapel, Microsoft Access MVP
>
> Cydney wrote:
> > Another basic question ... and my brain is fried... Please help.
> > Two tables are linked by RecID (one to many with referential integrity
> > added). The 1st table has RecID entered by AutoNumber upon the addition of
> > new records. The 2nd table doesn't have the RecID (number field) filled in
> > until a new record is added by clicking a button from the first table's form.
> > I want the "onclick" to FIRST look to see if the record is there and then
> > move to that record. If it is not there, then it needs to add the new record
> > and assign the correct RecID to the new record in the 2nd table. I'm so
> > close, but .... how...??

>

 
Reply With Quote
 
Steve Schapel
Guest
Posts: n/a
 
      18th Feb 2005
Cydney,

Very pleased to know that it is working for you now. However, it may be
worth pointing out that Referential Integrity has got nothing to do with
whether the form allows additions or not, and in fact would not be
related at all to the question. The only way that Referential Integrity
would come into play in this situation would be if you tried to enter a
record on the 2nd table form with a RecID that does not exist in the 1st
table.

--
Steve Schapel, Microsoft Access MVP


Cydney wrote:
> This is great. Both of your answers were helpful. Thank you.
> However... and remember my brain was fried.... I resolved the issue myself.
> Going back again and looking at the 2nd form (based on a table) I noticed
> that the form was set to not allow new records added. Therefore it didn't
> allow the referential integrity to take place as it naturally would. Anyway..
> resolved. Thanks!
>

 
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
Return only one subrecord, the one entered prior to current date AndreasO Microsoft Access VBA Modules 0 7th Jan 2008 11:06 PM
append data to record and subrecord. Mario Microsoft Access Queries 0 11th Jul 2007 02:07 PM
I Cannot Delete a Record with a Subrecord =?Utf-8?B?TWFyeQ==?= Microsoft Access Getting Started 1 5th Sep 2006 05:46 AM
Copy Worksheet to a new Workbook creating if it doesn't exist and add more Worksheets if it does exist pete.bastin@btopenworld.com Microsoft Excel Programming 4 18th Jun 2006 06:08 PM
Help:Why can't I use namespace System.Web? It is said that this namespace doesn't exist. But it should exist. Èý¹â Microsoft ASP .NET 1 29th Jul 2003 05:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:52 AM.