Home All Groups Group Topic Archive Search About

Which Form Has Invoked The Sub!

Author
7 Oct 2005 11:52 PM
Arpan
A VB6 application has 2 Forms. The 1st Form includes a Public
sub-routine for the Click event of a CommandButton. The 2nd Form also
accesses this sub-routine (which is why it is Public & not Private)
through a BAS Module. Other than performing the same actions for both
the Forms, I want the sub-routine to do some extra actions when it is
invoked by the 2nd Form, something like this:

Public Sub cmdButton1_Click()
    'If(invoked by Form1) Then
    '    Do Action1
    '    Do Action2
    'ElseIf(invoked by Form2) Then
    '    Do Action1
    '    Do Action2
    '    Do Action3
    '    Do Action4
    'End If
End Sub

To implement this, I need to know which Form has invoked the
sub-routine. Now how do I find out which Form has invoked the
sub-routine?

Note that at any given time, either the 1st Form will be visible or
both the Forms will be visible. At no time will the 2nd Form be visible
without the 1st Form. It goes without saying that when both the Forms
are visible, the sub-routine can be invoked by either of the 2 Forms.

Thanks,

Arpan

Author
8 Oct 2005 12:45 AM
Bob Butler
"Arpan" <arpan***@hotmail.com> wrote in message
news:1128729177.228375.256030@g43g2000cwa.googlegroups.com
> A VB6 application has 2 Forms. The 1st Form includes a Public
> sub-routine for the Click event of a CommandButton. The 2nd Form also
> accesses this sub-routine (which is why it is Public & not Private)
> through a BAS Module. Other than performing the same actions for both
> the Forms, I want the sub-routine to do some extra actions when it is
> invoked by the 2nd Form, something like this:
>
> Public Sub cmdButton1_Click()

A sub in a BAS module would not normally be named linke that but you can't
tell (at least not without some serious hacking.  Use an optional argument:

Public Sub DoActions(Optional ByVal Do3And4 As Boolean=False)

>     '    Do Action1
>     '    Do Action2

  If Do3And4 Then

>     '    Do Action3
>     '    Do Action4
>     'End If
> End Sub

From form 1 use:  Call DoActions

From form 2 use: Call DoActions(True)

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
10 Oct 2005 10:49 AM
Phill. W
"Arpan" <arpan***@hotmail.com> wrote in message
news:1128729177.228375.256030@g43g2000cwa.googlegroups.com...
> A VB6 application has 2 Forms. The 1st Form includes a Public
> sub-routine for the Click event of a CommandButton. The 2nd Form also
> accesses this sub-routine (which is why it is Public & not Private)
> through a BAS Module. Other than performing the same actions for both
> the Forms, I want the sub-routine to do some extra actions when it is
> invoked by the 2nd Form, something like this:

I would suggest that you're putting things the wrong way around.

Extract /common/ code into subroutines.
Leave [Form-] specific code /in/ the form, as in

[Form1]
Private Sub cmdButton1_Click()
    Call CommonButtonCode()
    ' Do the specific stuff here
End Sub

[Form2]
Private Sub cmdButton1_Click()
    Call CommonButtonCode()
    ' Do the specific stuff here
End Sub

[Module1]
Public Sub CommonButtonCode()
    ' Shared code goes in here
End Sub

> To implement this, I need to know which Form has invoked the
> sub-routine. Now how do I find out which Form has invoked the
> sub-routine?

If you /really/ want to do this, pass the caller as an argument to the
method, as in

Public Sub CommonButtonCode( ByVal oaCaller as Form )
    If Typeof oaCaller is Form1 Then
        ' Do Form1-specific stuff here
    ElseIf Typeof oaCaller is Form2 Then
        ' Do Form2-specific stuff here
    End If
End Sub

HTH,
    Phill  W.