2014-06-27 70 views
1

我試圖在更新後在表單中設置焦點。當我在表單類模塊中執行此操作時,我沒有任何問題。但是,我需要以幾種形式完成此操作,因此我試圖將其寫入模塊中。我的問題是我不能讓.SetFocus工作,除非我在類模塊中對錶單名稱進行硬編碼。 WHno是我試圖設置焦點的控件的名稱。。模塊VBA中的SetFocus

我已經嘗試了一些選項,似乎沒有工作。

這是子。除了.SetFocus過程以外,一切都很好用。

Sub ValidateWHNO() 

Dim EnteredWHNO As Integer 
Dim actForm As String 
Dim deWHNO As Variant 
msg As Integer 
Dim ctrlWHNO As Control 

EnteredWHNO = Screen.ActiveControl.Value 
actForm = Screen.ActiveForm.Name 
Set ctrlWHNO = [Forms]![frmEnterBookData]![WHno] 
deWHNO = DLookup("[WHno]", "tblDataEntry", "[WHno] = " & EnteredWHNO) 

If EnteredWHNO = deWHNO Then 
    msg = MsgBox("You have already entered " & EnteredWHNO & " as a WHNO. The next number is " & DMax("[WHno]", "tblDataEntry") + 1 & ", use this?", 4 + 64, "Already Used WHno!") 
    If msg = 6 Then 
     Screen.ActiveControl.Value = DMax("[WHno]", "tblDataEntry") + 1 
    Else 
     Screen.ActiveControl.Value = Null 
     ctrlWHNO.SetFocus 'CODE THAT WONT RUN 
    End If 
End If 

End Sub 

我已經嘗試了許多其它方法來設置焦點,如: Forms(actForm).WHno.SetFocusForms(actForm).Controls(WHno).SetFocusScreen.ActiveControl.SetFocus

當前結果是,如果沒有在選定MSGBOX,該值是清除,但焦點轉移到下一個控件。

感謝您提供任何可能提供的幫助。

+0

是否啓用了WHno控制? (即不禁用)? – Shiva

回答

1

以下是否有所作爲?

Sub ValidateWHNO(frm as Access.Form) 

    Dim EnteredWHNO As Integer 
    Dim actForm As String 
    Dim deWHNO As Variant 
    msg As Integer 

    EnteredWHNO = frm.ActiveControl.Value 
    actForm = frm.Name 
    deWHNO = DLookup("[WHno]", "tblDataEntry", "[WHno] = " & EnteredWHNO) 

    If EnteredWHNO = deWHNO Then 
     msg = MsgBox("You have already entered " & EnteredWHNO & " as a WHNO. The next number is " & DMax("[WHno]", "tblDataEntry") + 1 & ", use this?", 4 + 64, "Already Used WHno!") 
     If msg = 6 Then 
      frm.ActiveControl.Value = DMax("[WHno]", "tblDataEntry") + 1 
     Else 
      frm.ActiveControl.Value = Null 
      frm![WHno].SetFocus 
     End If 
    End If 

End Sub 

而從每個表單您的呼叫將是:

VaidateWHNO Me 

代替使用相對引用的形式(Screen.ActiveForm)的,碼通過直接傳遞形式參考並使用該基準作爲.setFocus方法的父級。

+0

謝謝,這完全符合我的需求。 – Mitch