|
-
May 17th, 2008, 12:40 AM
#1
Thread Starter
Junior Member
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?
-
May 17th, 2008, 04:52 AM
#2
Re: how to pass a form as parameter to a function thats in a public module?
-
May 17th, 2008, 05:09 AM
#3
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?
-
May 17th, 2008, 05:16 AM
#4
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
-
May 17th, 2008, 09:24 AM
#5
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)
Last edited by jmsrickland; May 17th, 2008 at 09:50 AM.
-
May 17th, 2008, 11:47 AM
#6
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
Last edited by RobDog888; May 17th, 2008 at 11:51 AM.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 17th, 2008, 12:04 PM
#7
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?
Last edited by jmsrickland; May 17th, 2008 at 12:14 PM.
-
May 17th, 2008, 12:58 PM
#8
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 17th, 2008, 01:50 PM
#9
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
'
'
-
May 17th, 2008, 02:46 PM
#10
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|