Results 1 to 10 of 10

Thread: how to pass a form as parameter to a function thats in a public module?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2008
    Posts
    20

    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?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: how to pass a form as parameter to a function thats in a public module?

    Moved From FAQ Section

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    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?

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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:
    1. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    2. Dim frm as object
    3.  
    4. Call SomeExternFunction(frm)
    5. End Sub
    In your Module
    vb Code:
    1. Public Sub SomeExternFunction(frm as object)
    2.  
    3. ...
    4.  
    5. End Sub

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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
      '
      '

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width