Results 1 to 15 of 15
  1. #1
    mainerain is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Location
    Maine, USA
    Posts
    146

    Return Button to previous Form

    I would like to click on a button that takes me back to the previous form no matter where I previously was. What would the code look like with explanation as to what the code means would be appreciated.

  2. #2
    GinaWhipp's Avatar
    GinaWhipp is offline Competent Performer
    Windows 7 64bit Access 2013 32bit
    Join Date
    Jul 2011
    Location
    Ohio, USA
    Posts
    377
    So, here's the issue. How does Access know which Form you were just on? Are you storing that somewhere? In my mind that is the only way that would work.

  3. #3
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Several points missing from your posted goal.
    Define previous form. If you're on a form and a button there takes you to another form, then you close that form, the 1st form is the previous form.
    If you got to form 2 from 1, then activate form 3, which is the "previous" form, the 1st or the 2nd one?

    Then there is the issue as to whether or not the "previous" form has been closed by code or the user. Then what?
    If the previous form is still open, why go to the trouble of coding this when you can simply click on the tab or minimized window, depending on how that option was set?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    I agree with the other responders. Access won't necessarily know what "previous" is. Suggest your logic identifies explicitly where you are going/coming from in some manner and use that to identify your "previous".

  5. #5
    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
    How about this, using the OpenArgs argument of the OpenForm command:

    From the Command Button on the First Form

    Code:
     Private Sub cmdGo2SecondaryForm_Click()
    
    Dim stDocName As String
    Dim stCallingForm As String
        
      stCallingForm = Me.Name
      DoCmd.Close
      stDocName = "SecondaryFormName"
      DoCmd.OpenForm stDocName, , , , , , stCallingForm
    
    End Sub
    Then in the OnClose Event of the Secondary Form that was called

    Code:
     Private Sub Form_Close()
     
    Dim stDocName As String
    Dim PreviousForm As String
      
     If Not IsNull(Me.OpenArgs) Then
      stDocName = Me.OpenArgs
      DoCmd.OpenForm stDocName
     End If
    
    End Sub


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

    All posts/responses based on Access 2003/2007

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Here's the thing. If you open another form after that, which one is 'previous'? No answer on that yet.

  7. #7
    mainerain is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Location
    Maine, USA
    Posts
    146
    First, Thanks for all reply's. To clarify a bit.. I have multiple forms that may go to a particular form and I just want to return to whichever got me there. So instead of multiple buttons to choose from to return I can just hit a return button to simplify the layout.

    What resources do folks utilize to expand their Access knowledge? Forums, YouTube, paid training..... ? What has worked out best for you?

    Missinglinq thanks for the code. Would you give a brief commentary on the code? I am just starting out and have much to learn. Do I just copy the code verbatim and just insert my specific form name in quotes?

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,426
    another option is to have a 'menu path' - simply a list of which forms the user has opened (perhaps to a specific record). User can the jump around by selecting options on the list e.g.

    frmMainMenu
    frmListCustomers
    frmCustomers - CustomerA

    user clicks on frmListCustomers to return and select
    frmCustomers - CustomerB

    list would show
    frmMainMenu
    frmListCustomers
    frmCustomers - CustomerA
    frmCustomers - CustomerB

    user then wants to go to suppliers, clicks on frmMainMenu and selects
    frmListSuppliers and then
    frmSuppliers - SupplierA

    list would show
    frmMainMenu
    frmListCustomers
    frmCustomers - CustomerA
    frmCustomers - CustomerB
    frmListSuppliers
    frmSuppliers - SupplierA

    use needs to go back to customerA - they can just select from the list

    What resources do folks utilize to expand their Access knowledge?
    google/bing

  9. #9
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Dontchathink users would not be as tuned in to the form names? I kinda like the idea, but I think you'd need a 2 column listbox/combo. One column for recognizable names, one invisible column for form names.
    What has worked out best for you?
    Doing - even if it's only practice. While doing, you 'Google' a lot and pick out the ones that not only apply to the question you have, but speak to you in a way you can relate to. Books are good, but often present a lot of information in an order that may not fit your learning curve. Sure, you can skip over sections but I think you'e have to know what it is you're looking for in order to focus on that. Using a search engine allows you to formulate questions that may not be technically accurate but still provide results.

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    In the situation you are describing, I think using 'Open args' is the simplest solution as missinglinq described.

    It will always allow you to return to the original form when the new form is closed .... UNLESS you do any detours first.

    If you want multiple possible paths, then you need a solution like Ajax suggested.

    As well as Google and various Access forums, you may find You Tube videos useful at your current level.
    Recommend the complete courses by either Richard Rost or Steve Bishop
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  11. #11
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,426
    Dontchathink users would not be as tuned in to the form names?
    you can use what you want - a simple table with formname and displayname for example. You then have a table to userID, and say PK for the form plus perhaps a flag to say whether user updated/created the record and you are over halfway to having a log/audit system. I use it as either a vertical list (used to be a listbox, now a subform so I can use conditional formatting) or a horizontal list similar to those used in browsers and web pages such as this one

    Forum Access Forums Programming Return Button to previous Form

    going back to what resources - one of the most effective skills to learn is how to ask the right question of google/bing so you get the relevant pages

  12. #12
    mainerain is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Location
    Maine, USA
    Posts
    146
    Private Sub Form_Close()

    Dim stDocName As String
    Dim PreviousForm As String

    If Not IsNull(Me.OpenArgs) Then
    stDocName = Me.OpenArgs
    DoCmd.OpenForm stDocName
    End If

    End Sub
    How do I make this code operate on a button click?

  13. #13
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    You don't need to. It will run automatically when your form is closed no matter how it is closed.

    If you want to use it on a button instead e.g. Close button just move the code to that event

    Code:
    Private Sub cmdClose_Click()
    
    Dim stDocName As String
    'Dim PreviousForm As String 'NOT NEEDED AS NOT USED
    
    If Not IsNull(Me.OpenArgs) Then
    stDocName = Me.OpenArgs DoCmd.OpenForm stDocName
    End If End Sub
    Bear in mind if you do that & instead close the form by using the small X in the top right or by any other method, that code will be bypassed
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  14. #14
    mainerain is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Location
    Maine, USA
    Posts
    146
    I wish to use a button that returns to the previous form and closes the form the Return button is on.

  15. #15
    mainerain is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Location
    Maine, USA
    Posts
    146
    I finally caught on... I used Missingling's code verbatim except the name of the go to form for my situation, and then added....

    Private Sub Btn8712Return_Click()

    DoCmd.Close

    End Sub

    Success! Thanks so much!
    Last edited by mainerain; 10-29-2018 at 05:51 AM. Reason: Error

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

Similar Threads

  1. Replies: 1
    Last Post: 08-19-2016, 01:40 PM
  2. Return values from Previous Day
    By trock12 in forum Queries
    Replies: 9
    Last Post: 01-15-2016, 11:33 AM
  3. Button to have same data as previous form.
    By ryanmce92 in forum Forms
    Replies: 8
    Last Post: 01-14-2015, 02:49 AM
  4. Replies: 1
    Last Post: 05-31-2013, 08:53 AM
  5. Replies: 3
    Last Post: 10-22-2010, 06:53 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