Results 1 to 3 of 3
  1. #1
    Bdowns is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Feb 2012
    Posts
    28

    Make Form open to the record it was on when it was last closed


    I have a navigation form with tabs for different data entry forms. I am trying to make it so that when you switch from one form to another and then back to the original form it opens to the record you were last on rather than re-setting to the first record. The form that is most commonly used (original form in the mentioned scenario) is a form with a subform. It has a one-to-many relationship. As it is, it is too easy to switch forms, come back, and then start logging data on the subform with the wrong id from the main form. Any ideas?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    By 'switching' do you mean opening and closing and opening? Otherwise, I don't understand how just changing focus between forms will cause 're-setting'. Are you doing a requery of forms?

    If you want to provide db for analysis, follow instructions at bottom of my post.
    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
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    I assume from your description you actually are speaking of closing a Form, then re-opening it later, to the same Record that was last viewed when the Form was closed.

    For information to persist between the opening and closing of a Form, it has to be stored in a Table, and to identify a given Record, you must have a Unique Field defined for Table the Form is based on; normally this Field would be the one designated as the Primary Key for the Table.

    1. Create a new Table
    2. Add a single Field
    3. Name the Field LastRecordViewed
    4. Set its Datatype to match your Unique Field
    5. Save the Table and name it UtilityTable

    Now use these Subs in the Code Module of the Form in question:

    Code:
    Private Sub Form_Load()
    
    Dim TargetField As Variant
     
     If DCount("*", "UtilityTable") > 0 Then
      
      TargetField = DLookup("LastRecordViewed", "UtilityTable")
    
      UniqueFieldName.SetFocus
    
      DoCmd.FindRecord TargetField
     
     End If
    
    End Sub


    Code:
    Private Sub Form_Unload(Cancel As Integer)
     
     If DCount("*", "UtilityTable") > 0 Then
      
      CurrentDb.Execute "UPDATE UtilityTable SET UtilityTable.LastRecordViewed = " & Me.UniqueFieldName, dbFailOnError
     
     Else
      
      CurrentDb.Execute "INSERT INTO UtilityTable (LastRecordViewed) VALUES ('" & Me.UniqueFieldName & "')"
     
     End If
    
    End Sub

    UniqueFieldName must be just that, a Field that is unique to a given Record.

    When this Form is closed, this unique identifier will be written to the UtilityTable.

    When the Form is next opened, it will retrieve this unique identifier from UtilityTable and find the Record on the current Form that contains it.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Replies: 1
    Last Post: 10-26-2012, 12:52 PM
  2. Replies: 1
    Last Post: 05-03-2012, 02:25 PM
  3. How to make a Database Form open
    By rovman in forum Access
    Replies: 25
    Last Post: 10-31-2011, 06:52 PM
  4. Replies: 4
    Last Post: 08-17-2011, 05:30 AM
  5. Replies: 2
    Last Post: 09-24-2009, 08:07 AM

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