2011-01-11 81 views
3

我想在用戶在特定窗體打開時將焦點返回到Access應用程序時調用事件。以下事件似乎根本沒有起火。Forms GotFocus事件似乎不會觸發

Private Sub Form_GotFocus() 
    Call crtListDirectory 
End Sub 

任何機構是否有任何想法,我怎麼能觸發該事件發生,而當/如何在Form_GotFocus事件實際上被觸發。

在此先感謝您的幫助

諾埃爾

+0

嘗試使用窗體的激活事件? – 2011-01-11 16:09:45

+0

@彼得勳爵試着使用Activate,如果你能看到我的評論如下。 – noelmcg 2011-01-11 16:43:49

回答

1

訪問幫助:

一個表單可以獲取焦點只有一個窗體上的所有 可見控件 殘疾,或有沒有控制 的表格。

您可能想嘗試激活。

編輯評論重新

我可以看到你在做什麼似乎想的唯一方法是使用的API,這是有點凌亂。爲了證明這一點,您需要一個帶有兩個控件Text0和Text2的表單(這些是默認名稱)。設置計時器的時間間隔,以合適的東西,比方說2000年,和定時器事件到:

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 

現在,您將需要一個模塊:

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 

這是非常,非常粗糙,但它給你有什麼可以搞砸的。