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.
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.
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.
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.
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".
How about this, using the OpenArgs argument of the OpenForm command:
From the Command Button on the First Form
Then in the OnClose Event of the Secondary Form that was calledCode: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
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
Here's the thing. If you open another form after that, which one is 'previous'? No answer on that yet.
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?
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
google/bingWhat resources do folks utilize to expand their Access knowledge?
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.
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.What has worked out best for you?
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
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 oneDontchathink users would not be as tuned in to the form names?
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
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?
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
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 bypassedCode:Private Sub cmdClose_Click() Dim stDocName As String 'Dim PreviousForm As String 'NOT NEEDED AS NOT USED If Not IsNull(Me.OpenArgs) ThenstDocName = Me.OpenArgs DoCmd.OpenForm stDocNameEnd If End Sub
I wish to use a button that returns to the previous form and closes the form the Return button is on.
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