Sub-Form Object ID's

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

    #1

    Sub-Form Object ID's

    I'm new to Access. But I've been ooping for about twelve or thirteen
    years in another language.

    I've got a form with two instances of the same sub-form. It works
    quite well, or at least it will, when I find out how the source form
    can identify which of its two instances has invoked its after_insert
    event handler.

    Any ideas on this?

    Thanks.
  • Allen Browne

    #2
    Re: Sub-Form Object ID's

    From the parent form, it's very easy to tell the difference: the two subform
    controls have different names (even though they have the same SourceObject).

    From the subform, it's much more difficult: they both have the same
    properties, including the same parent. One solution is to place a hidden
    text box on the subform. Then use the Load event of the main form to set the
    value of the text box in each subform, e.g.:
    Me.[Sub1].Form.[Text1] = "Instance 1"
    Me.[Sub2].Form.[Text1] = "Instance 2"
    That allows you do identify instance from within it. (In practice, you may
    need to set a different Default Value for a foreign key in each instance, so
    that can be used to achieve the same result.)

    Presumably you're okay with passing something back to a proc in the main
    form, once you have a way to distinguish the subform instance.

    --
    Allen Browne - Microsoft MVP. Perth, Western Australia.
    Tips for Access users - http://allenbrowne.com/tips.html
    Reply to group, rather than allenbrowne at mvps dot org.

    "Weaver" <we.aver@verizo n.net> wrote in message
    news:667211a4.0 409061843.5209c 917@posting.goo gle.com...[color=blue]
    > I'm new to Access. But I've been ooping for about twelve or thirteen
    > years in another language.
    >
    > I've got a form with two instances of the same sub-form. It works
    > quite well, or at least it will, when I find out how the source form
    > can identify which of its two instances has invoked its after_insert
    > event handler.
    >
    > Any ideas on this?
    >
    > Thanks.[/color]


    Comment

    • Weaver

      #3
      Re: Sub-Form Object ID's

      > From the parent form, it's very easy to tell the difference<

      But how can I respond to an event such as AfterInsert from the parent
      form? I see in the list of properties for the subforms only OnEnter
      and OnExit.

      I will proceede using the technique you described in your post.

      Thanks.


      "Allen Browne" <AllenBrowne@Se eSig.Invalid> wrote in message news:<413d2a84$ 0$22786$5a62ac2 2@per-qv1-newsreader-01.iinet.net.au >...[color=blue]
      > From the parent form, it's very easy to tell the difference: the two subform
      > controls have different names (even though they have the same SourceObject).
      >
      > From the subform, it's much more difficult: they both have the same
      > properties, including the same parent. One solution is to place a hidden
      > text box on the subform. Then use the Load event of the main form to set the
      > value of the text box in each subform, e.g.:
      > Me.[Sub1].Form.[Text1] = "Instance 1"
      > Me.[Sub2].Form.[Text1] = "Instance 2"
      > That allows you do identify instance from within it. (In practice, you may
      > need to set a different Default Value for a foreign key in each instance, so
      > that can be used to achieve the same result.)
      >
      > Presumably you're okay with passing something back to a proc in the main
      > form, once you have a way to distinguish the subform instance.
      >
      > --
      > Allen Browne - Microsoft MVP. Perth, Western Australia.
      > Tips for Access users - http://allenbrowne.com/tips.html
      > Reply to group, rather than allenbrowne at mvps dot org.
      >
      > "Weaver" <we.aver@verizo n.net> wrote in message
      > news:667211a4.0 409061843.5209c 917@posting.goo gle.com...[color=green]
      > > I'm new to Access. But I've been ooping for about twelve or thirteen
      > > years in another language.
      > >
      > > I've got a form with two instances of the same sub-form. It works
      > > quite well, or at least it will, when I find out how the source form
      > > can identify which of its two instances has invoked its after_insert
      > > event handler.
      > >
      > > Any ideas on this?
      > >
      > > Thanks.[/color][/color]

      Comment

      • Allen Browne

        #4
        Re: Sub-Form Object ID's

        In the subform's AfterInsert event procedure, you can call a routine all
        tell it which subform instance is calling, e.g.

        Private Sub Form_AfterInser t()
        If Me.Text1 = "Instance 1" Then
        Call Me.Parent.MyFun ction(1)
        Else
        ...
        End If
        End Sub

        Then, in the main form's module:
        Function MyFunction(intI nstance As Integer)
        'do your stuff
        End Function

        Alternative way to call the parent routine in a form named "Form1":
        Call Form_Form1.MyFu nction(1)

        --
        Allen Browne - Microsoft MVP. Perth, Western Australia.
        Tips for Access users - http://allenbrowne.com/tips.html
        Reply to group, rather than allenbrowne at mvps dot org.
        "Weaver" <we.aver@verizo n.net> wrote in message
        news:667211a4.0 409070826.72825 434@posting.goo gle.com...[color=blue][color=green]
        >> From the parent form, it's very easy to tell the difference<[/color]
        >
        > But how can I respond to an event such as AfterInsert from the parent
        > form? I see in the list of properties for the subforms only OnEnter
        > and OnExit.
        >
        > I will proceede using the technique you described in your post.
        >
        > Thanks.
        >
        >
        > "Allen Browne" <AllenBrowne@Se eSig.Invalid> wrote in message
        > news:<413d2a84$ 0$22786$5a62ac2 2@per-qv1-newsreader-01.iinet.net.au >...[color=green]
        >> From the parent form, it's very easy to tell the difference: the two
        >> subform
        >> controls have different names (even though they have the same
        >> SourceObject).
        >>
        >> From the subform, it's much more difficult: they both have the same
        >> properties, including the same parent. One solution is to place a hidden
        >> text box on the subform. Then use the Load event of the main form to set
        >> the
        >> value of the text box in each subform, e.g.:
        >> Me.[Sub1].Form.[Text1] = "Instance 1"
        >> Me.[Sub2].Form.[Text1] = "Instance 2"
        >> That allows you do identify instance from within it. (In practice, you
        >> may
        >> need to set a different Default Value for a foreign key in each instance,
        >> so
        >> that can be used to achieve the same result.)
        >>
        >> Presumably you're okay with passing something back to a proc in the main
        >> form, once you have a way to distinguish the subform instance.
        >>
        >> --
        >> Allen Browne - Microsoft MVP. Perth, Western Australia.
        >> Tips for Access users - http://allenbrowne.com/tips.html
        >> Reply to group, rather than allenbrowne at mvps dot org.
        >>
        >> "Weaver" <we.aver@verizo n.net> wrote in message
        >> news:667211a4.0 409061843.5209c 917@posting.goo gle.com...[color=darkred]
        >> > I'm new to Access. But I've been ooping for about twelve or thirteen
        >> > years in another language.
        >> >
        >> > I've got a form with two instances of the same sub-form. It works
        >> > quite well, or at least it will, when I find out how the source form
        >> > can identify which of its two instances has invoked its after_insert
        >> > event handler.
        >> >
        >> > Any ideas on this?
        >> >
        >> > Thanks.[/color][/color][/color]


        Comment

        • PinkPanther

          #5
          Re: Sub-Form Object ID's

          Hi Gents,

          What about responding to the parent forms events by delcaring the
          parent form as a module level variable with events in the subforms?

          eg Private WithEvents mfrm as Form

          This way you have purely event driven code in which the parent form
          doesn't need to know anything about the subforms.

          Haven't tested it but can't see why it wouldn't work...

          Regards,

          Peter
          [color=blue][color=green]
          > > But how can I respond to an event such as AfterInsert from the parent
          > > form? I see in the list of properties for the subforms only OnEnter
          > > and OnExit.
          > >
          > > I will proceede using the technique you described in your post.
          > >
          > > Thanks.[/color][/color]

          Comment

          • Weaver

            #6
            Re: Sub-Form Object ID's

            Thanks Allen. This is a good technique to know about.

            As it turns out, this only reason I needed to know which instance
            invoked the event was to know which of two values to use in processing
            the data. I just put that value in the forms hidden field. So
            instead of doing the
            if ... then ... else
            I just set ThisYear to the appropriate value ahead of time then stated
            Me.Year = ThisYear
            in the event handler.

            Thanks again.


            "Allen Browne" <AllenBrowne@Se eSig.Invalid> wrote in message news:<413e604f$ 0$22801$5a62ac2 2@per-qv1-newsreader-01.iinet.net.au >...[color=blue]
            > In the subform's AfterInsert event procedure, you can call a routine all
            > tell it which subform instance is calling, e.g.
            >
            > Private Sub Form_AfterInser t()
            > If Me.Text1 = "Instance 1" Then
            > Call Me.Parent.MyFun ction(1)
            > Else
            > ...
            > End If
            > End Sub
            >
            > Then, in the main form's module:
            > Function MyFunction(intI nstance As Integer)
            > 'do your stuff
            > End Function
            >
            > Alternative way to call the parent routine in a form named "Form1":
            > Call Form_Form1.MyFu nction(1)
            >
            > --
            > Allen Browne - Microsoft MVP. Perth, Western Australia.
            > Tips for Access users - http://allenbrowne.com/tips.html
            > Reply to group, rather than allenbrowne at mvps dot org.
            > "Weaver" <we.aver@verizo n.net> wrote in message
            > news:667211a4.0 409070826.72825 434@posting.goo gle.com...[color=green][color=darkred]
            > >> From the parent form, it's very easy to tell the difference<[/color]
            > >
            > > But how can I respond to an event such as AfterInsert from the parent
            > > form? I see in the list of properties for the subforms only OnEnter
            > > and OnExit.
            > >
            > > I will proceede using the technique you described in your post.
            > >
            > > Thanks.
            > >
            > >
            > > "Allen Browne" <AllenBrowne@Se eSig.Invalid> wrote in message
            > > news:<413d2a84$ 0$22786$5a62ac2 2@per-qv1-newsreader-01.iinet.net.au >...[color=darkred]
            > >> From the parent form, it's very easy to tell the difference: the two
            > >> subform
            > >> controls have different names (even though they have the same
            > >> SourceObject).
            > >>
            > >> From the subform, it's much more difficult: they both have the same
            > >> properties, including the same parent. One solution is to place a hidden
            > >> text box on the subform. Then use the Load event of the main form to set
            > >> the
            > >> value of the text box in each subform, e.g.:
            > >> Me.[Sub1].Form.[Text1] = "Instance 1"
            > >> Me.[Sub2].Form.[Text1] = "Instance 2"
            > >> That allows you do identify instance from within it. (In practice, you
            > >> may
            > >> need to set a different Default Value for a foreign key in each instance,
            > >> so
            > >> that can be used to achieve the same result.)
            > >>
            > >> Presumably you're okay with passing something back to a proc in the main
            > >> form, once you have a way to distinguish the subform instance.
            > >>
            > >> --
            > >> Allen Browne - Microsoft MVP. Perth, Western Australia.
            > >> Tips for Access users - http://allenbrowne.com/tips.html
            > >> Reply to group, rather than allenbrowne at mvps dot org.
            > >>
            > >> "Weaver" <we.aver@verizo n.net> wrote in message
            > >> news:667211a4.0 409061843.5209c 917@posting.goo gle.com...
            > >> > I'm new to Access. But I've been ooping for about twelve or thirteen
            > >> > years in another language.
            > >> >
            > >> > I've got a form with two instances of the same sub-form. It works
            > >> > quite well, or at least it will, when I find out how the source form
            > >> > can identify which of its two instances has invoked its after_insert
            > >> > event handler.
            > >> >
            > >> > Any ideas on this?
            > >> >
            > >> > Thanks.[/color][/color][/color]

            Comment

            Working...