Results 1 to 7 of 7
  1. #1
    RA99 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Location
    Regina, Saskatchewan, Canada
    Posts
    23

    Saving junction table record from subform

    I am resetting my issue in the Forms forum (from Modules) in hopes I can get unstuck.

    In my db, I have a ProgramsT table (pk ProgID), a ProgrammersT table (pk ProgmrID) and a junction table, ProgramRoleT (pk ProgRoleID). I have a form that presents a query of ProgramsT, with a subform to present current Program/Programmer relationships,bound as follows:

    Code:
    SELECT ProgramRoleT.*, ProgrammersT.MemberID, MembersT.MasterID, ContactMasterT.LastName, ContactMasterT.FirstName, ContactMasterT.MiddleName
    FROM ((ContactMasterT INNER JOIN MembersT ON ContactMasterT.MasterID = MembersT.MasterID) INNER JOIN ProgrammersT ON MembersT.MemberID = ProgrammersT.MemberID) INNER JOIN ProgramRoleT ON ProgrammersT.ProgmrID = ProgramRoleT.ProgmrID;
    That part seems to work OK. However, I want to give the user the option of selecting a new Programmer for the Program, who must be selected from amongst Members. I am trying to do this through a second subform bound to ProgramRolesT. The subform is set to data entry. It contains a combo box that draws MemberID from the table MembersT, passes it to a control called MemberID and calls a new form to create the new ProgrammerT record. My current code to do this is:



    Code:
    Private Sub Combo13_AfterUpdate()
    Me.MemberID = Me!Combo13.Column(0)
    DoCmd.Hourglass True
    DoCmd.OpenForm "ProgrammerNewF2"
    DoCmd.Close acForm, "ProgrammerNewF2", acSave
    DoCmd.Hourglass False
    End Sub
    This works perfectly to create the new ProgrammersT record, but I have two problems - capturing the ProgmrID fk from the new ProgrammersT record, and writing the new ProgramRoleT junction table record to link the two. Capturing the new ProgmrID value should, I think, be doable in code, although I've not succeeded yet (I think problems with null values??) but the failure to save the new junction record looks to me like an events issue to my uninitiated brain. The blank subform shows (New) in the junction record pk field, but it does not populate the new pk value as the record is (supposedly) populated (see screenshot).

    Can anyone make any suggestions?
    Attached Files Attached Files

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    Options to 'capture' the ProgmrID and make it available to the junction table subform:

    1. set a globally declared variable

    2. set a TempVars variable

    3. directly populate textbox on the junction subform

    I am confused by which subform is for which table. The bottom-most form instructs to 'Select new programmer from membership'. If this is the junction table subform then shouldn't programmer be selected from ProgrammersT? What is the upper subform for?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    RA99 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Location
    Regina, Saskatchewan, Canada
    Posts
    23
    The upper subform displays the programmer(s) currently attached to the program on the main form. The intent of the lower subform is to allow the user to attach a new programmer to that program. A new programmer must be selected from among current members, which is why the combo box is from MembersT. The programmers table is linked to the members table via MemberID as a foreign key.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    I am still confused.

    If the upper subform displays the programmers attached to the program then that is the junction table form? But the lower subform also shows the ProgramID so that is also bound to junction table?

    Do you want to provide db for analysis?

    I don't understand why you need a separate programmers table. Why not use the members table as source for programmers. If only some members are programmers, then have another field in the table to identify those individuals. Use that field to filter combobox list.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    RA99 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Location
    Regina, Saskatchewan, Canada
    Posts
    23
    It might be possible to use the members table for both, but membership and programmer status each have independent start and stop dates and other info specific to each, and the relationships looked to me to be getting too tangled, given that programs and programmers both have possible many-to-one relationships. Rather than confuse the issue more by trying to explain it, I should let you have a look, the main form in question is ProgramViewF.
    Attached Files Attached Files

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,625
    First, the combobox should probably offer members who are not already in ProgrammersT.

    SELECT MemberActiveQ.MemberID, MemberActiveQ.LastName, MemberActiveQ.FirstName, MemberActiveQ.MiddleName
    FROM ProgrammersT RIGHT JOIN MemberActiveQ ON ProgrammersT.MemberID = MemberActiveQ.MemberID
    WHERE (((ProgrammersT.MemberID) Is Null))
    ORDER BY MemberActiveQ.LastName, MemberActiveQ.FirstName, MemberActiveQ.MiddleName;

    Why do you need the MemberID textbox? Just reference the combobox. Pass new ProgmrID back to the subform.
    Private Sub Form_Current()
    If IsNull(Me!MemberID) Then Me!MemberID = Forms!ProgramViewF!ProgramViewSub2.Combo13
    If IsNull(Me!ProgmrActive) Then Me!ProgmrActive = "Active"
    Forms!ProgramViewF!ProgramViewSub2.Form!ProgmrID = Me.ProgmrID
    End Sub

    Save the record and requery the subforms and the combobox after the programmer form closes.

    Private Sub Combo13_AfterUpdate()
    If Not IsNull(Me.Combo13) Then
    DoCmd.OpenForm "ProgrammerNewF2"
    DoCmd.Close acForm, "ProgrammerNewF2", acSaveNo
    DoCmd.RunCommand acCmdSaveRecord
    Parent.ProgramViewSubF.Requery
    Me.Requery
    Me.Combo13.Requery
    Me.Combo13 = Null
    End If
    End Sub


    Have you run Debug > Compile? There's a lot of code errors.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    RA99 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jul 2014
    Location
    Regina, Saskatchewan, Canada
    Posts
    23
    Wow, does that ever work, and thanks. You also revealed a fairly obvious flaw in my process logic - the user not only has to be able to assign a member as a new programmer, he/she also has to be able to assign a current or past programmer (i.e. someone already in ProgrammersT) to a program. So, I set up (yet) another subform bound to the junction table with a combo box to pick up ProgmrID from a query of ProgrammersT. Without the need to create a new ProgrammersT record, the code needed is simpler than above. Adapting your code:

    Code:
    Private Sub Combo21_AfterUpdate()
    Me.ProgmrID = Me!Combo21.Column(0)
    If Not IsNull(Me.Combo21) Then
     Me.Requery
     Parent.ProgramViewSubF.Requery
     Me.Combo21.Requery
     Me.Combo21 = Null
    End If
    End Sub
    This, I am happy to say, seems to work for me! Thanks again.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 15
    Last Post: 02-27-2014, 12:04 PM
  2. Using form to add record to Junction table
    By SaskiFX in forum Forms
    Replies: 5
    Last Post: 03-19-2013, 12:16 PM
  3. Replies: 3
    Last Post: 01-21-2013, 12:57 AM
  4. Junction table in subform in Access 2007
    By BrizzoRay in forum Forms
    Replies: 1
    Last Post: 12-18-2011, 01:43 AM
  5. record saving twice in table
    By ds_8805 in forum Forms
    Replies: 15
    Last Post: 04-14-2010, 07:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums