Forms GotFocus event does not seem to fire
Asked Answered
A

1

4

I'm trying to call an event when the user returns focus to the Access application when a specific form is open. The following event doesn't seem to fire up at all.

Private Sub Form_GotFocus()
    Call crtListDirectory
End Sub 

Does any body have any ideas as to how I could trigger this event to happen, and when/how does the Form_GotFocus event actually get triggered.

thanks in advance for any help

Noel

Attlee answered 11/1, 2011 at 15:45 Comment(7)
Try using the form's Activate event?Scour
@Lord Peter tried using Activate, if you could see my comment below.Attlee
@glinch. The following (old) thread explains the issue - pcreview.co.uk/forums/thread-1078416.php - but does not offer a working solution. Sorry.Scour
Drat! Ahh well I will have to try and figure out a work around, thanks for link to the explanation @Lord PeterAttlee
What does crtListDirectory do and why do you need to do it when you return to Access/or to this particular form?Fruitarian
@David It relates to this question I asked here #2586950 When the user adds files to directories through the windows folder view my intention is to requery the lists when the user returns to the access app. I will have to add a refresh button that the user will need to click to refresh the lists unless I can do it automatically.Attlee
Why does it need to be refreshed? Why not refresh it only when the list is used for something? That if you have a listbox or subform that lists the files, requery it before the user does something that needs to use the list of files. The only alternative to that would be a timer event and I'd hesitate to do that, as it can be quite problematic.Fruitarian
D
1

Access help:

A form can get the focus only if all visible controls on a form are disabled, or there are no controls on the form.

You might like to try Activate.

EDIT re Comments

The only way I can see of doing what you seem to want is with APIs, which is somewhat messy. To demonstrate this you will need a form with two controls Text0 and Text2 (these are the default names). Set the Timer Interval to something suitable, say 2000, and the Timer Event to:

Private Sub Form_Timer()
Dim lngWin As Long
Dim s As String

    'This is just a counter to show that the code is running
    Me.Text2 = Nz(Me.Text2, 0) + 1

    'API
    lngWin = GetActiveWindow()
    s = GetWinName(lngWin)

    If s = "Microsoft Access" Then
        If Me.Text0 = "Lost Focus" Then
            Me.Text0 = "Focus returned"
        End If
    Else
        Me.Text0 = "Lost Focus"
    End If

End Sub

You will now need a module for:

Option Compare Database

Declare Function GetActiveWindow Lib "user32" () As Integer
Declare Function GetWindowText Lib "user32.dll" Alias _
"GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As _
String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias _
"GetWindowTextLengthA" (ByVal hwnd As Long) As Long

Function GetWinName(hw As Long)
    Dim lngText As Long ' receives length of text of title bar
    Dim strWinName As String ' receives the text of the title bar
    Dim lngWinText As Long ' receives the length of the returned string

    lngText = GetWindowTextLength(hw)
    strWinName = Space(lngText + 1)
    lngWinText = GetWindowText(hw, strWinName, lngText + 1)
    strWinName = Left(strWinName, lngWinText)
    GetWinName = strWinName
End Function

This is all very, very rough, but it gives you something to mess about with.

Dendrology answered 11/1, 2011 at 16:11 Comment(2)
Sorry I should have checked the help more thoroughly. Gotfocus isn't really applicable then in this case. I've tried using Activate, but that only seems to fire when I activate the form after returning from another form whilst inside Access. I need the event to fire when I return to using Access from another app.Attlee
thanks for all of the above. Will look into this and hopefully try and figure out a solution, cheers.Attlee

© 2022 - 2024 — McMap. All rights reserved.