Button that returns to previous form

JokerUSN

New member
Local time
Today, 11:21
Joined
Dec 30, 2009
Messages
7
I'm working on a db that tracks actionable items for an office department. I'd like to be able to get to some forms from multiple locations/forms and then be able to automatically return to the previously viewed form without having to create separate forms/buttons/macros that only specify one form.

Here's an example:

I have a form that allows you to input a new organization a worker belongs to. I want to be able to get to this form from the New Worker form, the Switchboard, the New Task Form and an Administration form without having to create 4 forms and buttons that correspond to reversing the path that got me to that form in the first place.

Is there a code I can put under a button that will return me to the last open form as well as refresh that form to reflect the new information that was just entered?
 
I'd look into the Screen object, maybe the PreviousControl member for instance. When your form opens I expect you'll be able to automatically maintain a reference to the control and it's parent (control.parent) which, if it's a form, you can requery in the unload or close events of the form.

Code:
private m_ctr as control  [COLOR="Green"]'the control that opened Me[/COLOR]

sub form_load()
[COLOR="Green"]  'save a reference to the control that opened this form[/COLOR]
  set m_ctr = screen.previouscontrol
end sub

sub form_unload(cancel as integer)
[COLOR="Green"]  'if the parent of the control that opened this form is a form, requery it[/COLOR]
  if typename(m_ctr.parent) = "Form" then m_ctr.parent.requery
end sub
Here's some untested code for examples.
And welcome to the forum,
Cheers,
 
  • Like
Reactions: dcb
Thanks for the help. I thought it might have to be something like that. I'm pretty new to VBA and still learning so some of your code was a little beyond my level of comprehension at the moment. Would you mind explaining it a little more? Specifically, what those codes do in Access.
 
Well, the previous form should be the first form that appears when the current form is closed. This might not be the case if the user has multiple forms open and switches between them before closing the form in question.
Explain it more? Every meaningful line of code has a descriptive comment. It's more efficient for me if you ask questions about what you don't get.
To ensure the calling form receives the focus you could change to this ...
Code:
sub form_unload(cancel as integer)
[COLOR="Green"]  'check the object type of the parent of the 'previous control'[/COLOR]
  if typename(m_ctr.parent) = "Form" then 
[COLOR="Green"]    'if it is a form, then requery and setfocus[/COLOR]
    m_ctr.parent.requery
    m_ctr.parent.setfocus
  end if
end sub
Let me know,
Cheers,
 

Users who are viewing this thread

Back
Top Bottom