Accessing the Created Instance of a form in my modules

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stone

    Accessing the Created Instance of a form in my modules

    Hello,

    I need to create a single (new) instance of a FORM and have that instance
    available to be called from my modules and some other forms. This is how I
    launch it, from <FormAnyform>

    Option Explicit
    Dim WithEvents frmNew as myExistingForm
    ---------------------------

    Private Sub InitializeNewSt uff()
    Set frmNew = New frmSomething
    frmNew.Hide
    End Function

    I can't use frmSomething.Sh ow, as I normally do and need to
    create an instance (to work with a UserControl and Events)
    but now I cannot access frmNew form my modules...

    For example frmNew.Caption= "Hello" cannot happen as
    frmNew is not "public".

    If I try frmSomething.Ca ption="Hello" then I launch a NEW, second
    instance of frmSomething... . this is bad.

    How can I set frmNew to a Global so I can just use frmNew after it
    is created throughout my program?

    Thanks
    -stone

    because of a UserControl and requirement of RaiseEvents)






  • Grinder

    #2
    Re: Accessing the Created Instance of a form in my modules


    "Stone" <x@y.com> wrote in message
    news:t4Feb.38$o X5.326558@news4 .srv.hcvlny.cv. net...[color=blue]
    > Hello,
    >
    > I need to create a single (new) instance of a FORM and have[/color]
    that instance[color=blue]
    > available to be called from my modules and some other forms.[/color]
    This is how I[color=blue]
    > launch it, from <FormAnyform>
    >
    > Option Explicit
    > Dim WithEvents frmNew as myExistingForm
    > ---------------------------
    >
    > Private Sub InitializeNewSt uff()
    > Set frmNew = New frmSomething
    > frmNew.Hide
    > End Function
    >
    > I can't use frmSomething.Sh ow, as I normally do and need to
    > create an instance (to work with a UserControl and Events)
    > but now I cannot access frmNew form my modules...
    >
    > For example frmNew.Caption= "Hello" cannot happen as
    > frmNew is not "public".
    >
    > If I try frmSomething.Ca ption="Hello" then I launch a NEW,[/color]
    second[color=blue]
    > instance of frmSomething... . this is bad.
    >
    > How can I set frmNew to a Global so I can just use frmNew[/color]
    after it[color=blue]
    > is created throughout my program?[/color]

    I suppose you can also set a reference to a global variable in
    that module. Be sure to clear it, though, as it could prevent
    the form from being unloaded.


    Comment

    • Stone

      #3
      Re: Accessing the Created Instance of a form in my modules

      Sorry... I'm kind stupid..

      I tried setting in a module

      Public frmNew as frmSomething

      in a module but it never gets referenced to the instance I made.
      How do I set the tie this Public to the new instance?

      Thanks-
      -stone

      Grinder wrote in message ...[color=blue]
      >
      >
      >I suppose you can also set a reference to a global variable in
      >that module. Be sure to clear it, though, as it could prevent
      >the form from being unloaded.
      >
      >[/color]


      Comment

      • Ken Halter

        #4
        Re: Accessing the Created Instance of a form in my modules

        Here's one way....
        '============== ===Form1 Code
        Option Explicit

        Private WithEvents frmNew As myExistingForm

        Private Sub Command1_Click( )
        Set frmNew = NewForm
        frmNew.Show
        End Sub

        Private Sub frmNew_Test()
        MsgBox "Fired Event!!!"
        End Sub

        Private Sub frmNew_UnloadFi red()
        Set NewForm = Nothing 'clear the refs
        Set frmNew = Nothing
        End Sub
        '============== ===myExistingFo rm Code
        Option Explicit

        Public Event Test()
        Public Event UnloadFired()

        Private Sub Command1_Click( )
        RaiseEvent Test
        End Sub

        Private Sub Form_Unload(Can cel As Integer)
        RaiseEvent UnloadFired
        End Sub
        '============== ===Module1 Code
        Option Explicit

        Private mobjNewForm As myExistingForm

        Public Property Get NewForm() As myExistingForm
        If mobjNewForm Is Nothing Then
        Set mobjNewForm = New myExistingForm
        End If
        Set NewForm = mobjNewForm
        End Property

        Public Property Set NewForm(ByRef NewSetting As myExistingForm)
        'This is basically to clear the object variable but
        'will also allow you to do other stuff with it if you want.
        Set mobjNewForm = NewSetting
        End Property
        '============== ===


        --
        Ken Halter - MS-MVP-VB - http://www.vbsight.com
        Please keep it in the groups..


        "Stone" <x@y.com> wrote in message news:t4Feb.38$o X5.326558@news4 .srv.hcvlny.cv. net...[color=blue]
        > Hello,
        >
        > I need to create a single (new) instance of a FORM and have that instance
        > available to be called from my modules and some other forms. This is how I
        > launch it, from <FormAnyform>
        >
        > Option Explicit
        > Dim WithEvents frmNew as myExistingForm
        > ---------------------------
        >
        > Private Sub InitializeNewSt uff()
        > Set frmNew = New frmSomething
        > frmNew.Hide
        > End Function
        >
        > I can't use frmSomething.Sh ow, as I normally do and need to
        > create an instance (to work with a UserControl and Events)
        > but now I cannot access frmNew form my modules...
        >
        > For example frmNew.Caption= "Hello" cannot happen as
        > frmNew is not "public".
        >
        > If I try frmSomething.Ca ption="Hello" then I launch a NEW, second
        > instance of frmSomething... . this is bad.
        >
        > How can I set frmNew to a Global so I can just use frmNew after it
        > is created throughout my program?
        >
        > Thanks
        > -stone
        >
        > because of a UserControl and requirement of RaiseEvents)
        >
        >
        >
        >
        >
        >[/color]


        Comment

        • Grinder

          #5
          Re: Accessing the Created Instance of a form in my modules


          "Stone" <x@y.com> wrote in message
          news:giGeb.516$ oX5.558055@news 4.srv.hcvlny.cv .net...[color=blue]
          > Sorry... I'm kind stupid..
          >
          > I tried setting in a module
          >
          > Public frmNew as frmSomething
          >
          > in a module but it never gets referenced to the instance I[/color]
          made.[color=blue]
          > How do I set the tie this Public to the new instance?[/color]

          I wouldn't name it frmNew, if that's also the name of a form in
          your project. Be that as it may:

          Set frmNew = MyNewInstance



          Comment

          • Stone

            #6
            Re: Accessing the Created Instance of a form in my modules

            Thanks..

            When I set this I can access the form Methods via myNewInstance
            (good!) however at runtime I get

            Object Variable or With Variable not set (ERR91)

            in forms and modules within the program when the code it executed.

            myNewInstance.C aption="Hello"

            Is an example:

            Thanks for the info, I must have missed something in your answer....
            b.t.w. .. I impleted the code from Ken Halter in this post and it worked
            perfectly..

            Grinder wrote in message <##JvfpFiDHA.20 56@TK2MSFTNGP09 .phx.gbl>...
            [color=blue][color=green]
            >> How do I set the tie this Public to the new instance?[/color]
            >
            >I wouldn't name it frmNew, if that's also the name of a form in
            >your project. Be that as it may:
            >
            >Set frmNew = MyNewInstance
            >
            >
            >[/color]


            Comment

            • Stone

              #7
              Re: Accessing the Created Instance of a form in my modules

              Ken... Thanks, worked like a charm.
              To unload without crashing I needed to change the exit strategy a bit.

              I the sequence to get rid of the program I used...

              Set NewForm = Nothing
              Unload frmNew

              if I used

              Set NewForm = Nothing
              Set frmNew=Nothing

              then VB crashed on exist.
              Other than that EVERY is perfecty and my User Control looks good now!

              Ken Halter wrote in message ...[color=blue]
              >Here's one way....
              >'============= ====Form1 Code
              >Option Explicit
              >
              >Private WithEvents frmNew As myExistingForm[/color]
              .......


              Comment

              • Grinder

                #8
                Re: Accessing the Created Instance of a form in my modules


                "Stone" <x@y.com> wrote in message
                news:utWeb.4184 $E95.1252012@ne ws4.srv.hcvlny. cv.net...[color=blue]
                > Thanks..
                >
                > When I set this I can access the form Methods via[/color]
                myNewInstance[color=blue]
                > (good!) however at runtime I get
                >
                > Object Variable or With Variable not set (ERR91)
                >
                > in forms and modules within the program when the code it[/color]
                executed.[color=blue]
                >
                > myNewInstance.C aption="Hello"[/color]

                That means that myNewInstance has not be set. Surprisingly,
                the error message is accurate *and* explicit. If that is your
                global variable, then you'll have to set it after you
                instantiate the form.

                In your example code, you were using a local variable to
                instantiate your form. I suggested that you set it to a global
                variable, as per your requirements, so that it would fit in
                with what you were already doing. You can simply instantiate
                the form by using the global variable though:

                Set myNewInstance = New WhateverYourFor mTypeIs



                Comment

                Working...