訪問,選擇案例 ActiveControl.Name? (Access, Select Case ActiveControl.Name?)


問題描述

訪問,選擇案例 ActiveControl.Name? (Access, Select Case ActiveControl.Name?)

我在表單訪問中有 2 個組合框(c1c2),帶有按鈕 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 JamilaashleedawgEliot K)

參考文件

  1. Access, Select Case ActiveControl.Name? (CC BY‑SA 2.5/3.0/4.0)

#select-case #controls #Database #vba #ms-access






相關問題

SQL 加入 2 個共享列的查詢 (SQL joining 2 queries that share a column)

sql中的條件總和,根據字段中的條件從另一個表中獲取正確的總和 (conditional sum in sql, get correct sum from another table based on a condition in a field)

如何對此查詢進行選擇案例? (How can I do select case to this query?)

聲明一個案例列表在多個地方使用 (Declaring a case list to be used in more than one place)

選擇案例語句中的數組是否可能? (Is an array inside a select case statement possible?)

將 VBA 選擇案例轉換為 C# (Convert VBA select case to C#)

使用 Word VBA 中的宏選擇大小寫將英寸轉換為毫米 (Convert inch to mm using macro select Case in Word VBA)

訪問,選擇案例 ActiveControl.Name? (Access, Select Case ActiveControl.Name?)

選擇不運行每個案例的案例 (Select Case Not Running Every Case)

JustBasic,在代碼中出現“錯誤的嵌套錯誤選擇/案例” (JustBasic, getting "bad nesting error select/case" in code)







留言討論