(please see my comments and questions in-line)
carriey said:
Thanks for your resopnse although I'm a little disheartened to learn I'm
way
off track. I did get a little guidance here before but could never get it
working properly and you're right, I have tried a lot of stuff.
There is Tbl_MAIN (pk = Record_ID) for Frm_MAIN_AB
Subtbl_Internal_Inspections (pk = IntInsp) for Frm_Internal_Inspections
(Record_ID is the parent, one-to-many)
Subtbl_IntInsp_Deficiencies (pk = IntDefID) for
Subfrm_Internal_Insp_Deficiencies (IntInsp is the parent, one-to-many)
Subtbl_Obligations_MAIN (pk = Oblig_ID) for Frm_Obligations_MAIN_AB
Tbl_Junction (Oblig_ID and Record_ID for a many-to-many)
Subtbl_Obligation_Deficiencies (pk = ObligDefID) for
Subfrm_Oblig_Deficiencies
Let me restate some things to see if I understand. Where I have questions,
could you answer them, please?
Tbl_MAIN defines ... something ... for which there are inspections and
obligations. The primary key of this table is Record_ID.
Tbl_MAIN has a child table named Subtbl_Internal_Inspections, with primary
key IntInsp. The field Record_ID in this table is the foreign key to
Tbl_MAIN.
Subtbl_Internal_Inspections has a child table named
Subtbl_IntInsp_Deficiencies, with primary key IntDefID. The field IntInsp
in this table is the foreign key to Subtbl_Internal_Inspections.
There is another table, Subtbl_Obligations_MAIN, with primary key Oblig_ID.
You say that Tbl_Junction establishes a many-to-many relationship; is that
relationship between Subtbl_Obligations_MAIN and Tbl_Main (as is implied by
the foreign keys Oblig_ID and Record_ID that you mention)? Or have I
misunderstood? You may have meant that Tbl_Junction joins
Subtbl_Obligations_MAIN and Subtbl_Obligation_Deficiencies, but in that case
it doesn't make sense that Tbl_Junction would have foreign key field
Record_ID.
There is a table, Subtbl_Obligation_Deficiencies, with primary key
ObligDefID. Is this table a child table of Subtbl_Obligations_MAIN, with a
foreign key field Oblig_ID? Or is it in a different, many-to-many
relationship, as I was wondering above?
Could you explain in more detail what sorts of entities and relationships
these tables are meant to represent?
Both Frm_Internal_Inspections and Frm_Obligations_MAIN_AB are subforms of
Frm_MAIN_AB.
Is Frm_Obligations_MAIN_AB based directly on Subtbl_Obligations_MAIN, or is
it based on Tbl_Junction? If the former, I'd have thought the relationship
between Tbl_MAIN and Subtbl_Obligations_MAIN to be one-to-many, rather than
many-to-many.
When the user enters the Internal Inspection,
On Frm_Internal_Inspections, the subform on Frm_MAIN_AB?
they are to click the button
Is that button on the main form, or on the subform?
to create the Obligation (new Oblig_ID) and add some fields to the
Subtbl_Obligations_MAIN
All that is to be done by the button code, correct? Is this supposed to
both create a new record in Subtbl_Obligations_MAIN, and also create a
record in Tbl_Junction to link that record to the main record? This is
where I am uncertain of the relationships involved, and can't see my way
clear to the correct procedure for your button.
and then where the check box Self_Dec = true in
Subtbl_IntInsp_Deficiencies then a deficiency record should also be added
to
Subtbl_Obligation_Deficiencies.
By "check box", I assume you mean that Self_Dec is a Yes/No (Boolean) field.
As I (maybe) understand what you've written, you need to select from
Subtbl_IntInsp_Deficiencies all those records which (a) are related to any
record in Subtbl_Internal_Inspections which is related to the current record
on the main form, and (b) have their Self_Dec field = True, and use them to
create records in Subtbl_Obligation_Deficiencies.
There might be 20 deficiencies on the internal inspection but only 5 are
required to be self-declared as an obligation.
Where I kept running into trouble was with the Tbl_Junction. I originally
had my code to the tables instead of queries but no matter what I did, it
would say that there was no record in the other table. When I finally had
what I thought was the brilliant idea to change it to the queries, it
worked!
Obviously not 100% though because it only takes the first deficiency.
Here's the whole code, your help is greatly appreciated. My two users
have
been waiting on me for weeks now to get this working so thanks for your
time!
--------------------------------------------------------
Private Sub cmd_sendto_SD_Click()
Dim db As DAO.Database
Dim rsTJ As DAO.Recordset
Dim rsTO As DAO.Recordset
Dim rsTOD As DAO.Recordset
Dim Oblig_ID As Integer
'Open a recordset using a table
Set db = CurrentDb
Set rsTO = db.OpenRecordset("Qry_Obligations_MAIN", dbOpenDynaset)
Set rsTOD = db.OpenRecordset("Subtbl_Obligation_Deficiencies",
dbOpenDynaset)
With rsTO
If Not rsTO.EOF And Not rsTO.BOF Then
Do While Not rsTO.EOF
rsTO.AddNew 'Add Obligation Record
'Set Fields
rsTO!Record_ID = Me!RecordID
rsTO!Oblig_Rcvd = Date 'Set today's date
rsTO!Oblig_Status = "Open"
rsTO!Obligation_Type = "Self-Declaration"
rsTO!Obligation_Subtype = "7"
rsTO!Oblig_Subtype2 = "70"
rsTO!Company = "Penn West"
rsTO!Coordinator = "12"
rsTO!Govt_Agency = "ERCB"
rsTO!Internal_Insp = True
rsTO!Oblig_Date =
Forms!Frm_MAIN_AB!Frm_Internal_Inspections.Form!IntInsp_Date
rsTO!Response_Due =
Forms!Frm_MAIN_AB!Frm_Internal_Inspections.Form!Response_Due
rsTO!Locations = "1"
rsTO!Employee =
Forms!Frm_MAIN_AB!Frm_Internal_Inspections.Form!Employee
rsTO.Update 'Save the New Record
rsTO.Bookmark = rsTO.LastModified
Exit Do
Loop
End If
With rsTOD
If
Forms!Frm_MAIN_AB!Frm_Internal_Inspections!Subfrm_Internal_Insp_Deficiencies.Form!Self_Dec
= True Then
If Not rsTOD.EOF And Not rsTOD.BOF Then
Do While Not rsTOD.EOF
rsTOD.AddNew 'Add Deficiency Record
'Set Fields
rsTOD!Oblig_ID = rsTO!Oblig_ID
rsTOD!Deficiency =
Forms!Frm_MAIN_AB!Frm_Internal_Inspections!Subfrm_Internal_Insp_Deficiencies.Form!Deficiency
rsTOD!Deficiency_Comments =
Forms!Frm_MAIN_AB!Frm_Internal_Inspections!Subfrm_Internal_Insp_Deficiencies.Form!Deficiency_Comments
rsTOD.Update
rsTOD.Bookmark = rsTOD.LastModified
rsTOD.MoveNext
Exit Do
Loop
End If
End If
End With
rsTO.MoveNext
End With
rsTO.Close
rsTOD.Close
Set rsTO = Nothing
Set rsTOD = Nothing
Set db = Nothing
MsgBox "The Self-Declaration has been created. Thank you."
End Sub
I don't see why you have Do While loops in two places which exit (using Exit
Do) after the first iteration of the loop. I think this must represent a
misunderstanding on your part, but there's no point in trying to address
that until I understand what it is you really need to be doing.
I apologize for all the questions, but the logic here is going to be
crucial.