Passing a collection to/from Functions

CAlexander
CAlexander used Ask the Experts™
on
Ive tried different ways to do this, but can't seem to find any that work.

Dim MyCollection As New Collection

' This causes Compile Error: Argument Not Optional
MyCollection = PassCollection ("SomeStringData")



Private Function PassCollection(ByVal strData As String) As Collection

   Dim TmpCollection As New Collection

   'Perform some stuff with strData

   TmpCollection.Add "Some Data"

   PassCollection = TmpCollection

End Function

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try this

Add set keyword in front


Option Explicit




Private Function PassCollection(ByVal strData As String) As Collection

  Dim TmpCollection As New Collection

  'Perform some stuff with strData

  TmpCollection.Add "Some Data"

  Set PassCollection = TmpCollection

End Function
Private Sub Command1_Click()
Dim MyCollection As New Collection

' This causes Compile Error: Argument Not Optional
Set MyCollection = PassCollection("SomeStringData")
End Sub

Commented:
You need the "Set" since Collection is an object:

Dim MyCollection As New Collection

Set MyCollection = PassCollection ("SomeStringData")


Private Function PassCollection(ByVal strData As String) As Collection

  Dim TmpCollection As New Collection

  'Perform some stuff with strData

  TmpCollection.Add "Some Data"

  Set PassCollection = TmpCollection

End Function

Author

Commented:
I knew it was something simple. :c)
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial