how to pass a form as parameter to a function thats in a public module?
hi ,
i have a requirement that when the form is closing, i want to perform some actions. I need to do this for all forms. so i have written a function in a public module doing.
but now when i want to call this function in every form, i need to pass the form as a parameter to the function.
when i declare the parameter of the function as ' a as form'
and try to perform a checking operation, it says it cannot support this property.
could u help pls?
Re: how to pass a form as parameter to a function thats in a public module?
Re: how to pass a form as parameter to a function thats in a public module?
Can you post the (Module) Function, and a calling code example?
Re: how to pass a form as parameter to a function thats in a public module?
Use the QueryUnload Event for this for if you use the Unload Event you very well may reload the form when you reference it.
vb Code:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim frm as object
Call SomeExternFunction(frm)
End Sub
In your Module
vb Code:
Public Sub SomeExternFunction(frm as object)
...
End Sub
Re: how to pass a form as parameter to a function thats in a public module?
Don't you think that the argument should be
Call SomeExternFunction(Me)
instead of
Call SomeExternFunction(frm)
Also in the module function you can use this
Public Sub SomeExternFunction(frm As Form)
instead of
Public Sub SomeExternFunction(frm As Object)
Re: how to pass a form as parameter to a function thats in a public module?
Yes, you can and is better to pass the form as a form so it adheres to typecasting.
Code:
'Behind Form
Option Explicit
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If Something(Me) = True Then
Cancel = -1 'True
Else
Cancel = 0 'False
End If
End Sub
'Inside Module
Option Explicit
Public Function Something(ByRef frm As Form)
'Do some action on the form
If blah = 1 Then
Something = True
Else
Something = False
End If
End Function
Re: how to pass a form as parameter to a function thats in a public module?
Both As Object and As Form work. I just made that statement because I think As Form is more meaningful than As Object.
However As frm does not work unless you Set frm = Form1 but why do that when Me takes care of it all?
Re: how to pass a form as parameter to a function thats in a public module?
If you are passing the current Form then it should already be instanciated so no need to Set unless you are passing some other form that may not yet be initialised.
Using "As Form" is strong naming to avoid a type casting errors if you accidentally pass any object that is not a Form type. Using "As Object" is late binding and not as efficient as declaring the type.
Yes, "Me" would be preferred. :)
Re: how to pass a form as parameter to a function thats in a public module?
If you are passing the current Form then it should already be instanciated so no need to Set unless you are passing some other form that may not yet be initialised.
Not quite sure what you are talking about here. I stated that As frm or more percisely I should have said just frm as it was indicated in post #4 does not work. You have to do Set frm = Form1 to make it work
Code:
'
'
Dim frm As Object
Call SomeExternFunction(frm) '<---- Does not work
'
'
but.......
Code:
'
'
Dim frm As Object
Set frm = Form1
Call SomeExternFunction(frm) '<------ Now it will work
'
'
Re: how to pass a form as parameter to a function thats in a public module?
Yea, Im not meaning that code since its not really correct. ;)