使用 2 个变量调用函数

Call a function with 2 Variables

我正在尝试创建一个基于您在表单上与之交互的下拉菜单的动态函数。当我调用它时,我需要将下拉列表的名称连同表单名称一起传递给该函数。

这是我当前的设置,它只调用传递表单名称的函数:

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)。

每次我 google 这个,它都会带来很多 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.ModernTheme.

中找到示例

调用函数:

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