[AccessD] Can a function return an object?

Stuart McLachlan stuart at lexacorp.com.pg
Fri Mar 17 05:26:44 CDT 2023


On 17 Mar 2023 at 5:56, Arthur Fuller wrote:

> Can a VBA function return an object, such as a form or DAO.database?
> 
> Can a function return a collection?
> 

Form? Yes, but you need to "Set"  the return, not just use "="
and the form must be open.

Function TestGet() As Long
  Debug.Print getForm("frmCustomers").Caption
End Function

Function getForm(frmname As String) As Form
Dim frm As Form
Set frm = Application.Forms(frmname)
Set getForm = frm
End Function

Collection: Yes

Function makecoll() As Collection
Dim coll As New Collection
coll.Add "First String", "key1"
coll.Add "Second String", "key2"
Set makecoll = coll
End Function

Function Testcoll() As Long
Dim col As Collection
Set col = makecoll()
Debug.Print col.Item("Key2")
End Function




More information about the AccessD mailing list