在VBA事件传递参数 [英] Passing parameters in VBA events
问题描述
我是比较新的访问,虽然我有在VB中的一些经验,我的问题很可能是非常简单的,虽然我好像不知道为了找到答案,我可以用它来搜索术语。
I am relatively new to Access though I do have some experience in VB, my question is probably very simple though I don't seem to know the terminology to search in order to find an answer I can use.
我是在创造一个的OnChange事件,我使用一个标签控制的过程中,我想传递一个整数数量不明的功能。 IE浏览器:
= myFunction的(1,4,6)
或 = myFunction的(ArrayList中[1,2,4])
I am in the process of creating an "OnChange" event for a tab control I am using, I would like to pass an undetermined amount of integers to the function. IE:
=myFunction(1,4,6)
OR =myFunction(ArrayList[1,2,4])
我可以创建一个重载函数,这些数字的工作,或者如果可能的话,我想将它们作为一个整数数组。虽然我的生活中,我想不出究竟是如何做到这一点。我已经采取了这条道路的原因是为了让我的功能尽可能具有普遍性,基本上只需要改变什么号码我发送到函数改变其行为。
I would either create an overloaded function to work with these numbers, or if possible I would like to pass them as an array of integers. Though for the life of me I cannot figure out exactly how to do this. The reason I have taken this path is to make my function as universal as possible, basically just having to change what numbers I send to the function to change its behaviour.
这是什么,我尝试着做一些粗略的编码,但我不知道怎么打发,除了像 = myFunction的([表])
什么P>
This is some rough coding of what I am try to do, though I have no idea how to pass anything besides something like =myFunction([Form])
Public Function Refresh(tabsToCheck As ArrayList)
For Each o In tabsToCheck
If Me.DevForm.Value = o Then
RefreshAllForms
End If
Next o
End Function
Public Function RefreshAllForms()
Dim f As Form
For Each f In Access.Forms
f.Refresh
Next
End Function
更新
Update
我想我会用我的敲定code更新,以防有人需要这对你有所帮助未来的感谢!
I thought I would update with my finalized code in case anyone needs this in the future thanks for your help!
Public Function RefreshControlTab(ctrl As Access.Control, ParamArray TabsToRefresh())
Dim i As Long
Dim lngUBound As Long
If UBound(TabsToRefresh) >= 0 Then
lngUBound = UBound(TabsToRefresh)
For i = 0 To lngUBound
If ctrl.Value = (TabsToRefresh(i) - 1) Then
RefreshAllForms
End If
Next
End If
End Function
Public Function RefreshAllForms()
Dim f As Form
For Each f In Access.Forms
f.Refresh
Next
End Function
所以一变,你会说'= RefreshControlTab([DevForm],3,4),并在第3或第4个选项卡中选择刷新将被执行。
So one change you would say '=RefreshControlTab([DevForm],3,4)' and when the 3rd or 4th tab is selected a refresh will be performed.
推荐答案
我想通过一些整数功能的量无法确定。的
这听起来像一个 ParamArray参数
给我。见简单的功能如下。它会返回一组数字的总和。
That sounds like a ParamArray
to me. See the simple function below. It will return the sum of a set of numbers.
Public Function AddThem(ParamArray MyNumbers()) As Long
Dim i As Long
Dim lngReturn As Long
Dim lngUBound As Long
If UBound(MyNumbers) >= 0 Then
lngUBound = UBound(MyNumbers)
For i = 0 To lngUBound
lngReturn = lngReturn + MyNumbers(i)
Next
End If
AddThem = lngReturn
End Function
注意 ParamArray参数
是一个Variant值的数组。所以,在函数中,你需要验证的值数,以避免麻烦...麻烦的一个例子将是一个的类型不匹配的调用函数的字符串值时出错: AddThem(A,B)
Note the ParamArray
is an array of Variant values. So within the function you would need to verify the values are numbers to avoid trouble ... one example of trouble would be a "type mismatch" error when calling the function with string values: AddThem("a", "b")
这篇关于在VBA事件传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!