当组合框具有焦点时显示下拉列表 [英] Show dropdown list when combobox has focus
本文介绍了当组合框具有焦点时显示下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在Access表单上放一点代码,以便在表单上的任何组合框都具有焦点时自动显示下拉列表?
Is it possible to put a single bit of code on an Access form that would automatically show the dropdown list whenever any combobox on the form has focus?
我知道只要组合框具有焦点,就可以使用以下事件自动显示下拉列表.但是,如果可能的话,我希望不必在每个组合框上都放这行代码,因为我有这么多.
I know I can automatically show the dropdown list whenever a combobox has focus by using the event below. But, if possible, I would prefer to not have to put that line of code on each combobox since I have so many.
Private Sub combobox_GotFocus()
'When the combobox receives focus
'display in drop down position
Me.combobox.Dropdown
End Sub
我的数据库中包含数十种表单,每种表单上都有许多组合框.
My database has dozens of forms in it with dozens of comboboxes on each form.
推荐答案
创建一个名为cComboBox的类
Create a class named cComboBox
Option Compare Database
Option Explicit
Private WithEvents mComboBox As Access.ComboBox
Private Sub mComboBox_GotFocus()
mComboBox.Dropdown
End Sub
Public Function AddCtl(nCtl As Access.ComboBox) As Access.ComboBox
Set mComboBox = nCtl
mComboBox.OnGotFocus = "[Event Procedure]"
'mComboBox.OnChange = "[Event Procedure]"
Set AddCtl = mComboBox
End Function
并将以下代码添加到您的表单
and add the following code to your Form
Option Compare Database
Option Explicit
Dim myCBs As New Collection
Private Sub Form_Load()
Dim myCB As cComboBox
Dim ctl As Access.Control
For Each ctl In Me.Controls
If TypeName(ctl) = "ComboBox" Then
Set myCB = New cComboBox
myCB.AddCtl ctl
myCBs.Add myCB
End If
Next
End Sub
这篇关于当组合框具有焦点时显示下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文