Proper focusing of windows in MS Access (natively or by a code)

I would think this is a pretty easy functionality to create.

As people said you can put a property on the forms or use the openargs property to store who you were called by. Then set focus when you close to the form you were called by if you were called by another form.

you even can't set focus to them because they think that they are always in focus.
I think if you are having problems setting focus you could roll your own something like

Code:
Public Sub MySetFocus(frmName As String)
  'Ensure it is open
  Dim IsOpen As Boolean
  Dim frm As Access.Form
  For Each frm In Forms
    If frm.Name = frmName Then
      IsOpen = True
      Exit For
    End If
  Next frm
  If IsOpen Then DoCmd.OpenForm frmName
End Sub

Of course you have the Forms collection and forms are loaded to the collection in order. so you can always close a form and set focus to the last form in the forms collection.
 
Via ribbon, via buttons, events... it doesn't matter... the behavior for the popups is ALWAYS the same
It seems that you've been struggling, but I doubt the only way to do it is sending a key combination. There must be something else going on, which is why I'm asking for your code, but a reproducible example would be best.

You even can't set focus to them because they think that they are always in focus
I'm not sure what you mean

Isladogs and Pat explained very good why is that.
I'm getting a different behavior than the one described by Colin. Read post #14, I'm getting the active form correctly every time, the only variation occurs when I switch to the code window.

I'm just disappointed that this isn't solved natively.
I insist, there must be something else going on. I could tell you what it is if you post a file.
 
I was soooooo disappointed in the tab view
i always use them together with some pop-up forms for lookups.
it gives your db a "modern" look (most apps nowadays use tab documents, acrobat, acad, etc.)
unlike the overlapping which makes your app looks so Jurassic (they originated from the old vb).
 
The examples by both @Pat Hartman and @arnelgp both use OpenArgs to track the name of the previous calling form.
That approach works well

However, there is another approach which I don't think has been mentioned until now and is worth considering.
You can also use Screen.ActiveForm to determine the name of the previous (calling) form by running it in the Form_Load event
i.e. before the current form has become fully loaded

Thanks are due to Richard Rost for reminding me of this approach in a recent video.

Code:
Private Sub Form_Load()
    Me.Caption = "Called from " & Screen.ActiveForm.Name (see comment below)
    TempVars!PrevForm = Screen.ActiveForm.Name
End Sub

You can then load the calling form using:

Code:
Private Sub cmdOpenPrevForm_Click()
    DoCmd.OpenForm TempVars!PrevForm
    DoCmd.Close acForm, Me.Name
End Sub

This approach will also work even if most/all forms are popups. Perhaps it will solve the OP's problems?
Perhaps the following will explain why @Edgar_ got different results

NOTE:
1. With popups, you will still get error 2475 typing ?Screen.ActiveForm.Name from the immediate window after the form is fully loaded as there is no ActiveForm (as defined by Access).

2. As already stated by the OP, the form activate and deactivate events are not triggered for popup forms as they are never 'active'

3. If you only have one form (normal or popup) open, the Me.Caption line in Form_Load will also fail with error 2475 as there is at that time no active window. However, you can easily use error handling to bypass that.
 
1. With popups, you will still get error 2475 typing ?Screen.ActiveForm.Name from the immediate window after the form is fully loaded as there is no ActiveForm (as defined by Access).
Trigger the code from the popup form and you will get it correctly.

2. As already stated by the OP, the form activate and deactivate events are not triggered for popup forms as they are never 'active'
I confirm those two events do not trigger.
 
Trigger the code from the popup form and you will get it correctly.
Yes, that's what I'm doing with my example using the Screen.ActiveForm code to determine the calling form
As already stated, that is working for both standard and popup forms when the code runs from the form
 
It seems that you've been struggling, but I doubt the only way to do it is sending a key combination. There must be something else going on, which is why I'm asking for your code, but a reproducible example would be best.


I'm not sure what you mean


I'm getting a different behavior than the one described by Colin. Read post #14, I'm getting the active form correctly every time, the only variation occurs when I switch to the code window.


I insist, there must be something else going on. I could tell you what it is if you post a file.

Thanks for your devotion to help, but there is no need for file posting. I tested what guys told me, and it's true, if you do not use tabs and instead use just overlapping windows, and no popups, this problem doesn't exist at all. In my case I then have some other problems with navigation form and some other things :). But there is no need to bother anyone here about that.

As Pat said above:
Access doesn't "see" popups. Therefore it brings you to what it does "see".

I'll just continue with what I already have (because it works) 🤷‍♂️
 
Access doesn't "see" popups. Therefore it brings you to what it does "see".
That verbiage is incorrect IMO, as other have already shown. The issue is not that access does not "see" it, it is simply because it is not the active form.

Access sees the Pop up as the active form when it is the active form. So when it has focus it is the active form.
But if you call the code from VBE, from a Menu, from another form it loses focus and no longer an active form.
This is easily demonstrated. Call a pop up and then try to launch code from anywhere (except the form) that closes the active form. The act of trying to run the code will force the form to lose focus and no longer be active. You can usually see it lose focus.
But from the form itself run.
DoCmd.Close acForm, Screen.ActiveForm.Name
and it will close. Not because "Access Sees" or "Access does not See", but because it is in fact the active form at the time code executes.
 
Here is a better test where you can close a popup by using ActiveForm
Have a tabbed form such as navigation form.
Set the timer interval to 0
Pop open a pop up form.
Have the calling form close it after a period of time.

Code:
Private Sub Command0_Click()
  DoCmd.OpenForm "form2_Popup"
  Me.TimerInterval = 3000
  loadList
End Sub

Private Sub Form_Timer()
  DoCmd.Close acForm, Screen.ActiveForm.Name
End Sub

This act of calling the code does not force the pop up to lose being the active form.
 

Users who are viewing this thread

Back
Top Bottom