我正在尝试创建一个动态函数,该函数基于表单上与之交互的下拉列表。当我调用它时,我需要将下拉列表的名称和表单名称一起传递给函数。
这是我当前的设置,它只调用传递表单名称的函数:
Private Sub KitchenMainCode_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call Populate([Form])
End Sub
Populate(frm As Form)
'do stuff
End Function
我有8个下拉菜单,这意味着我必须复制粘贴8批相同的代码,这并不理想,理想情况下我想要这样的东西:
Private Sub KitchenMainCode_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call Populate([Form], [control.name OR "KitchenMainCode"])
End Sub
Populate(frm As Form AND dropdown name as name)
frm.name.value = xyz
End Function
至少,如果我能传递一个字符串,那么制作8个Mousedown事件比8个独特的函数更容易,后者几乎是50行(x8(。
每次我在谷歌上搜索这个,它都会带来很多Excel的东西;由于某种原因,使用的代码与访问不兼容(可能是我的问题(,但它们都是VBA脚本。
问候,
您可以这样做:
Private Sub KitchenMainCode_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Call Populate(Me, Me.ActiveControl, Button, Shift, X, Y)
End Sub
Private Sub Populate(frm As Form, ctl As Control, Button As Integer, Shift As Integer, X As Single, Y As Single)
' do stuff
End Function
或者您可以实现WithEvents
。在我的VBA工程中可以找到一个例子。现代主题。
调用函数:
Private Sub KitchenMainCode_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim Result As Long ' as the function returns.
Result = Populate(Me, Me.ActiveControl, Button, Shift, X, Y)
End Sub
Private Function Populate(frm As Form, ctl As Control, Button As Integer, Shift As Integer, X As Single, Y As Single) As Long
' do stuff
Populate = SomeResultValue
End Function