2011-11-12 10 views
0

したがって、各コントロールを検証するためのフォームをループするValidateForm関数があります。私は関数の外に渡される情報の異なるビットをキャプチャするためにValDataと呼ばれる集合を持っています。これは素晴らしい作品です。カスタム関数から複数の値を返す

しかし、関数が返った後、ValDataの各項目にアクセスする方法はわかりません。私はValidateForm()。IsValidのように一度に1つを得ることができますが、各項目を取得するには、関数を再度実行する必要があります。私はこれを避けたい。

関数を一度実行する方法はありますか?返される各項目の値にアクセスしますか?

+1

現在のコードを表示する –

答えて

2

、あなたはあなたの関数からのリターンとしてコレクションを使用して検討するかもしれないが(あなたの質問で明らかにされていない;-)!):シンプル、遅滞として

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 

希望がある!

0

あなたのコードを見ることなく、正確に何をしているのかを言うのは難しいですが、後でクエリに結果を格納できるいくつかの方法の1つです。

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 
関連する問題