Subform displaying existing record won't display new record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • postman
    New Member
    • Nov 2008
    • 63

    Subform displaying existing record won't display new record

    I have a subform which is bound to a query which will display records using an ID# supplied from a listbox in the parent form.

    That same subform is also used to create new records using the following code in a "New Record" commandbutton:
    Code:
    DoCmd.GoToRecord , , acNewRec
    Both functions work fine. However, if I have a selected record displayed, and I click the "New Record" button, it does not move to a new record. It just stays on the selected record and any edits I make affect that selected record.

    Any help you can provide is appreciated!
  • puppydogbuddy
    Recognized Expert Top Contributor
    • May 2007
    • 1923

    #2
    If your button is executing from the main form, I believe you have to set focus on the subform to execute the new record command for the subform.
    Try your code this way:
    Code:
    Me.YourSubformControlName.Form.SetFocus
    DoCmd.GoToRecord , , acNewRec

    Comment

    • postman
      New Member
      • Nov 2008
      • 63

      #3
      Thanks for the suggestion, puppydogbuddy. I tried it, but I get the following error:

      Run-time error '2449': There is an invalid method in an expression.

      It highlights the line with the "SetFocus" method for the form. I tried various approaches for the SetFocus, but they all end in the same error.

      Any idea why?

      Thanks for your help!

      Comment

      • puppydogbuddy
        Recognized Expert Top Contributor
        • May 2007
        • 1923

        #4
        oops, I forgot to set focus on a control on the subform source object(the form embedded in the subform control).

        See this link: How to select controls on a subform with setFocus method in Access 2002
        Code:
        Me!YourSubformControlName.Form![ID].SetFocus 
        DoCmd.GoToRecord , , acNewRec

        Comment

        • postman
          New Member
          • Nov 2008
          • 63

          #5
          Thanks for the info. It wasn't working, but after looking at the table, I found that new records were being created, but the bound fields in the subform weren't clearing to display the new record--they were still displaying the selected record.

          Ideas?

          Thanks!

          Comment

          • puppydogbuddy
            Recognized Expert Top Contributor
            • May 2007
            • 1923

            #6
            Is the subform's "allow additions" property set to "Yes"?

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Originally posted by postman
              Ideas?
              Perhaps you need to requery the subform after the item has been added.

              Comment

              • postman
                New Member
                • Nov 2008
                • 63

                #8
                I just realized what the problem probably is. The subform is bound to a query which uses a listbox value as the criteria. If a record is selected in the listbox, the subform is going to display those contents regardless of what's happening in the table.

                I'll try a few things on that track and post the results...or more questions.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  That's about the size of it. The requery should help ;)

                  Comment

                  • postman
                    New Member
                    • Nov 2008
                    • 63

                    #10
                    The simplest fix to the problem was just changing the subform's "DataEntry" property to True when the "Create New Record" button is clicked, and to False when the "Display Records" button is clicked.

                    But that created the opposite problem, now:
                    If I go first to Display Records, it will display the selected records fine.
                    But if I first Create a new Record, then go to Display Records, the subform won't display the selected record from the listbox.

                    (The subform's recordsource is a query that uses the listbox value as criteria. The Listbox's click event does requery the subform.)

                    Not sure why this is happening.

                    Comment

                    • ChipR
                      Recognized Expert Top Contributor
                      • Jul 2008
                      • 1289

                      #11
                      From DataEntry Property:
                      The Data Entry property doesn't determine whether records can be added; it only determines whether existing records are displayed.

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32633

                        #12
                        Originally posted by postman
                        The simplest fix to the problem was just changing the subform's "DataEntry" property to True when the "Create New Record" button is clicked, and to False when the "Display Records" button is clicked.

                        But that created the opposite problem, now:
                        If I go first to Display Records, it will display the selected records fine.
                        But if I first Create a new Record, then go to Display Records, the subform won't display the selected record from the listbox.
                        I suggest that your solution possibly wasn't one then.

                        I would try looking at the .Requery route.

                        Comment

                        • postman
                          New Member
                          • Nov 2008
                          • 63

                          #13
                          Originally posted by NeoPa
                          I suggest that your solution possibly wasn't one then.

                          I would try looking at the .Requery route.
                          The .Requery route did not work.

                          After further testing, it appears to be a focus issue. I tried the earlier suggestions for moving the focus and I can't get the focus to move to the subform.

                          I even made test buttons on the parent form with code to just move the focus to a control on the subform and it won't work. The focus stays with the parent form. Not sure why that won't work.

                          Comment

                          • NeoPa
                            Recognized Expert Moderator MVP
                            • Oct 2006
                            • 32633

                            #14
                            Originally posted by postman
                            After further testing, it appears to be a focus issue. I tried the earlier suggestions for moving the focus and I can't get the focus to move to the subform.
                            Could we see the code you tried.

                            Comment

                            • postman
                              New Member
                              • Nov 2008
                              • 63

                              #15
                              Code:
                               
                              Private Sub Command173_Click()
                              
                                   Form_AttDetail.aiDate.SetFocus
                              
                              End Sub
                              Command173 is the command button in the parent form.
                              Form_AttDetail is the subform object.
                              aiDate is a control in that subform.

                              Thanks.

                              Comment

                              Working...