Ms access 从自定义函数返回多个值
所以,我有一个ValidateForm函数,它通过一个表单循环来验证每个控件。我有一个名为ValData的集合设置,用于捕获要传递给函数的不同信息位。这很有效 但是,我不知道在函数返回后如何访问ValData中的每个项。我可以一次获取一个,如:ValidateForm().IsValid,但为了获取每个项,我必须再次运行该函数。我想避免这种情况Ms access 从自定义函数返回多个值,ms-access,vba,Ms Access,Vba,所以,我有一个ValidateForm函数,它通过一个表单循环来验证每个控件。我有一个名为ValData的集合设置,用于捕获要传递给函数的不同信息位。这很有效 但是,我不知道在函数返回后如何访问ValData中的每个项。我可以一次获取一个,如:ValidateForm().IsValid,但为了获取每个项,我必须再次运行该函数。我想避免这种情况 是否有一种方法可以只运行一次函数,然后访问返回的每个项目的值?如果不查看您的代码,很难说您到底想做什么,但这里有几种方法之一,您可以存储结果以供以后查询
是否有一种方法可以只运行一次函数,然后访问返回的每个项目的值?如果不查看您的代码,很难说您到底想做什么,但这里有几种方法之一,您可以存储结果以供以后查询 在模块顶部声明一个公共变量,以表示ValData函数中的项。填充数组后,可以通过普通函数访问这些项 显然,您可以做更复杂的事情,特别是如果您使用集合对象。您甚至可以存储一个计数器并创建一个GetNext()函数。我希望这能给你一个好的开始
Public Results(1 To 2) As String
Sub CreateTestArray()
Results(1) = "Foo"
Results(2) = "Bar"
End Sub
Function GetResult(ByVal i As Long) As String
If i <= UBound(Results) Then
GetResult = Results(i)
End If
End Function
Sub ProofOfConcept()
MsgBox GetResult(2) ' will show "Bar"
End Sub
Public结果(1到2)作为字符串
子CreateTestArray()
结果(1)=“Foo”
结果(2)=“巴”
端接头
函数GetResult(ByVal i长度)作为字符串
如果i 取决于您的要求(在您的问题中还不清楚!!),您可以考虑使用集合作为函数返回:
Private Function MyResultsFunction() As Collection
Dim output As Collection
Set output = New Collection
'Hydrate your collection with data by whatever means necessary:
With output
'Stupid example code:
.Add "Item1"
.Add "Item2"
.Add "Item3"
.Add "Item4"
End With
'Return a reference to the collection as the function output:
Set MyResultsFunction = output
End Function
作为上述简单的延迟测试:
Private Sub Form_Load()
'A variable to receive the results from your function:
Dim Results As Collection
'An iterator to loop through the results contained in the collection:
Dim CurrentItem As Variant
'For this example, a string to toss the results into to display in the
'MsgBox:
Dim output As String
'Use the local Collection defined above to access the reference returned by
'your function:
Set Results = MyResultsFunction
'Iterate through the collection and do something with the results
'per your project requirements:
For Each CurrentItem In Results
'This is stupid example code:
output = output & CurrentItem & vbCrLf
Next
'I am just displayng the results to show that it worked:
MsgBox output
'Clean up:
Set Results = Nothing
End Sub
希望赫普斯