問題描述
訪問,選擇案例 ActiveControl.Name? (Access, Select Case ActiveControl.Name?)
我在表單訪問中有 2 個組合框(c1
、c2
),帶有按鈕 Search,如果用戶從c1
並點擊 Search 它應該會打開一個 FORM1
,如果用戶從 c2
中選擇一個值並點擊在搜索它應該打開 FORM2
.
Private Sub Commande6_Click()
Select Case ActiveControl.Name
Case "c1"
DoCmd.OpenForm "FORM1"
Case "c2"
DoCmd.OpenForm "FORM2"
Case Else
'traitement
End Select
End Sub
參考解法
方法 1:
If you're clicking a combo box and then clicking your Search button, the a combo box wasn't the last thing click, so it's not Active ‑ the Search button is (Commande6
in your case)
Public lastCBclicked as String
Private Sub c1_Click()
lastCBclicked = ActiveControl.Name
End Sub
Private Sub c2_Click()
lastCBclicked = ActiveControl.Name
End Sub
Private Sub Commande6_Click()
Select Case lastCBclicked
Case "c1"
DoCmd.OpenForm "FORM1"
Case "c2"
DoCmd.OpenForm "FORM2"
Case Else
'traitement
End Select
End Sub
方法 2:
Using ActiveControl won’t work. When you click search, control goes to the search button.
You can handle in a couple ways. You could (a) query both controls to detect which one is not set to the default, or (b) keep track of which one last had focus (using a hidden control, maybe).
EDIT: See ashleydawgs answer for the code.
(by Jamila、ashleedawg、Eliot K)