This should be easier than it is...

  • CanuckBuck

    Hall of Fame

    Points: 3883

    I have a form listing records in a table.  The form contains an "Edit this record" button and an "Add new Record" button.  Both of these forms opens a form, displaying; either the currently selected record (if "Edit this record" is clicked) or, in data entry mode, enabling a user to create a new record.  On this data entry/edit form there is a "Save" button, an "Add new Record" button and a "Close Button".  Provided the user has saved a newly added record, when the "Close" button is clicked, I would like to close the data entry form and navigate to the saved record in the form listing the records in the table.  I don't want to filter the list.  I just want to navigate to the new record in the list.

    The records are created with an autonumber value (called DB_Key) which should be able to be used to look up the record in the form listing all records.

    This seems like it should be straight forward to do but I'm really struggling.  I know that before I try to find the record in the record list form I have to requery the form.  I've got that working.  I've looked at the DoCmd.OpenForm command and think that I should be passing the value of DB_Key in the OpenArgs parameter and I think I've got that correct (DoCmd.OpenForm "Main List Template", acNormal, , , acFormReadOnly, acWindowNormal, Me.txtDBKey)  but I'm lost after that. I have this code in my Form_Load() subroutine of my list form;


    Private Sub Form_Load()

      If Not IsNull(Me.OpenArgs) Then
       DoCmd.GoToControl "txtDBKey"
       DoCmd.FindRecord Me.OpenArgs, acEntire, False, acSearchAll, False, acCurrent, True
      End If
     
    End Sub

    The first problem is that, although I passed a value in the OpenArgs parameter of the OpenForm command, the IsNull(Me.OpenArgs) call returns true!

    Can someone help me out here?

    Thanks in advance for any direction you can provide.

  • CanuckBuck

    Hall of Fame

    Points: 3883

    The other thing to note is a that with the code in the Form_Load() event it doesn't actually get triggered if the form is open!  I'm on the horns of a dilemma because in order to requery the form it has to be open.  Ugh! I'm sure it's me, right...  There's got to be a super simple way to do this.

    I did have this code in the Form_Activate() event but that doesn't seem right.

  • WILLIAM MITCHELL

    SSChampion

    Points: 13538

    I don't think that requerying the form will fire the Form_Load event, so the open args will always be null. To test that, set a breakpoint at the "If Not IsNull(Me.OpenArgs) Then" and run the app - I believe that it will only hit the breakpoint when the form is first opened.

    Your DB_Key is an autonumber, and as long as it is set to "Increment" and not "Random", the new record will be the highest number, so you should be able to simply go to the last record, using something like DoCmd.RunCommand acCmdRecordsGoToLast

    HTH

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply