How can two different buttons on a form run the same sub?

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

    How can two different buttons on a form run the same sub?

    I have a sub that can do two different tasks based on the value of one
    variable. So I'd like to have two different buttons on the same form run
    this, but each one setting a flag so that the conditional logic knows which
    one to run.

    Here is what I have in mind:

    If Button_One_Was_ Pressed then
    link = PerfArray(iCtr) .strFund_Name & ".Doc"
    else
    link = PerfArray(iCtr) .strTicker & ".Doc"
    end if

    FollowHyperlink link


    Is there a way that both buttons can point to the same sub or do I have to
    have one of them point to its own sub and then call this one?


  • Andrew

    #2
    Re: How can two different buttons on a form run the same sub?

    Hi Alan,

    Put the code you wrote straight into a sub of it's own with an input
    parameter, then call the sub from the Click event of both buttons, passing a
    different parameter.

    e.g.
    Private Sub Button1_Click()
    SetLink("One")
    End Sub

    Private Sub Button2_Click()
    SetLink("Two")
    End Sub

    Private Sub SetLink(pstrWhi chButton as String)
    Select Case pstrWhichButton
    Case "One"
    link = PerfArray(iCtr) .strFund_Name & ".Doc"
    Case "Two"
    link = PerfArray(iCtr) .strTicker & ".Doc"
    End Select
    FollowHyperlink link
    End Sub

    You get the idea.

    Anderw

    "Colleyvill e Alan" <aecharbonneau@ nospam.comcast. net> wrote in message
    news:4Hy1c.1098 44$Xp.484367@at tbi_s54...[color=blue]
    > I have a sub that can do two different tasks based on the value of one
    > variable. So I'd like to have two different buttons on the same form run
    > this, but each one setting a flag so that the conditional logic knows[/color]
    which[color=blue]
    > one to run.
    >
    > Here is what I have in mind:
    >
    > If Button_One_Was_ Pressed then
    > link = PerfArray(iCtr) .strFund_Name & ".Doc"
    > else
    > link = PerfArray(iCtr) .strTicker & ".Doc"
    > end if
    >
    > FollowHyperlink link
    >
    >
    > Is there a way that both buttons can point to the same sub or do I have to
    > have one of them point to its own sub and then call this one?
    >
    >[/color]


    Comment

    • Colleyville Alan

      #3
      Re: How can two different buttons on a form run the same sub?

      Thanks!


      "Andrew" <andrew_DONT_SP AM@webster.org> wrote in message
      news:V6z1c.8596 5$Wa.32966@news-server.bigpond. net.au...[color=blue]
      > Hi Alan,
      >
      > Put the code you wrote straight into a sub of it's own with an input
      > parameter, then call the sub from the Click event of both buttons, passing[/color]
      a[color=blue]
      > different parameter.
      >
      > e.g.
      > Private Sub Button1_Click()
      > SetLink("One")
      > End Sub
      >
      > Private Sub Button2_Click()
      > SetLink("Two")
      > End Sub
      >
      > Private Sub SetLink(pstrWhi chButton as String)
      > Select Case pstrWhichButton
      > Case "One"
      > link = PerfArray(iCtr) .strFund_Name & ".Doc"
      > Case "Two"
      > link = PerfArray(iCtr) .strTicker & ".Doc"
      > End Select
      > FollowHyperlink link
      > End Sub
      >
      > You get the idea.
      >
      > Anderw
      >
      > "Colleyvill e Alan" <aecharbonneau@ nospam.comcast. net> wrote in message
      > news:4Hy1c.1098 44$Xp.484367@at tbi_s54...[color=green]
      > > I have a sub that can do two different tasks based on the value of one
      > > variable. So I'd like to have two different buttons on the same form[/color][/color]
      run[color=blue][color=green]
      > > this, but each one setting a flag so that the conditional logic knows[/color]
      > which[color=green]
      > > one to run.
      > >
      > > Here is what I have in mind:
      > >
      > > If Button_One_Was_ Pressed then
      > > link = PerfArray(iCtr) .strFund_Name & ".Doc"
      > > else
      > > link = PerfArray(iCtr) .strTicker & ".Doc"
      > > end if
      > >
      > > FollowHyperlink link
      > >
      > >
      > > Is there a way that both buttons can point to the same sub or do I have[/color][/color]
      to[color=blue][color=green]
      > > have one of them point to its own sub and then call this one?
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Ray

        #4
        Re: How can two different buttons on a form run the same sub?

        "Andrew" <andrew_DONT_SP AM@webster.org> wrote in message news:<V6z1c.859 65$Wa.32966@new s-server.bigpond. net.au>...[color=blue]
        > Hi Alan,
        >
        > Put the code you wrote straight into a sub of it's own with an input
        > parameter, then call the sub from the Click event of both buttons, passing a
        > different parameter.
        >
        > e.g.
        > Private Sub Button1_Click()
        > SetLink("One")
        > End Sub
        >
        > Private Sub Button2_Click()
        > SetLink("Two")
        > End Sub
        >
        > Private Sub SetLink(pstrWhi chButton as String)
        > Select Case pstrWhichButton
        > Case "One"
        > link = PerfArray(iCtr) .strFund_Name & ".Doc"
        > Case "Two"
        > link = PerfArray(iCtr) .strTicker & ".Doc"
        > End Select
        > FollowHyperlink link
        > End Sub
        >
        > You get the idea.
        >
        > Anderw
        >
        > "Colleyvill e Alan" <aecharbonneau@ nospam.comcast. net> wrote in message
        > news:4Hy1c.1098 44$Xp.484367@at tbi_s54...[color=green]
        > > I have a sub that can do two different tasks based on the value of one
        > > variable. So I'd like to have two different buttons on the same form run
        > > this, but each one setting a flag so that the conditional logic knows[/color]
        > which[color=green]
        > > one to run.
        > >
        > > Here is what I have in mind:
        > >
        > > If Button_One_Was_ Pressed then
        > > link = PerfArray(iCtr) .strFund_Name & ".Doc"
        > > else
        > > link = PerfArray(iCtr) .strTicker & ".Doc"
        > > end if
        > >
        > > FollowHyperlink link
        > >
        > >
        > > Is there a way that both buttons can point to the same sub or do I have to
        > > have one of them point to its own sub and then call this one?
        > >
        > >[/color][/color]


        Hello all. I'm certainly missing something here and perhaps
        it could be explained. Why so much about which button was
        pressed? Can't you just put the code you want behind each
        of the buttons separately? Example:

        Private Sub Button1_Click()
        link = PerfArray(iCtr) .strFund_Name & ".Doc"
        End Sub

        Private Sub Button2_Click()
        link = PerfArray(iCtr) .strTicker & ".Doc"
        End Sub

        This seems to work for me and seems a bit simplier
        to code.

        Regards,

        Ray

        Comment

        • Fletcher Arnold

          #5
          Re: How can two different buttons on a form run the same sub?

          "Ray" <raycait@excite .com> wrote in message
          news:20ba9f4a.0 403040601.3cfca f77@posting.goo gle.com...[color=blue]
          > Hello all. I'm certainly missing something here and perhaps
          > it could be explained. Why so much about which button was
          > pressed? Can't you just put the code you want behind each
          > of the buttons separately? Example:
          >
          > Private Sub Button1_Click()
          > link = PerfArray(iCtr) .strFund_Name & ".Doc"
          > End Sub
          >
          > Private Sub Button2_Click()
          > link = PerfArray(iCtr) .strTicker & ".Doc"
          > End Sub
          >
          > This seems to work for me and seems a bit simplier
          > to code.
          >
          > Regards,
          >
          > Ray[/color]

          Of course when the code is that simple, it doesn't make much difference.
          But what if the code was supposed to do a million and one complicated
          calculations, then create an Excel spreadsheet and save it in a location
          specific to which button was pressed - and this time there were 5 different
          buttons.
          Would you write all of the code and copy it 5 times altering a small part of
          the code specific to each button? What then when you adjust your code, each
          time keep 5 separate versions updated? By writing the bulk of the code only
          once you save duplication with all the bug-risks that introduces and make it
          far easier to read and maintain.


          Fletcher







          Comment

          • Colleyville Alan

            #6
            Re: How can two different buttons on a form run the same sub?

            "Fletcher Arnold" <fletch@home.co m> wrote in message
            news:c27h3v$hh4 $1@sparta.btint ernet.com...[color=blue]
            > "Ray" <raycait@excite .com> wrote in message
            > news:20ba9f4a.0 403040601.3cfca f77@posting.goo gle.com...[color=green]
            > > Hello all. I'm certainly missing something here and perhaps
            > > it could be explained. Why so much about which button was
            > > pressed? Can't you just put the code you want behind each
            > > of the buttons separately? Example:
            > >
            > > Private Sub Button1_Click()
            > > link = PerfArray(iCtr) .strFund_Name & ".Doc"
            > > End Sub
            > >
            > > Private Sub Button2_Click()
            > > link = PerfArray(iCtr) .strTicker & ".Doc"
            > > End Sub
            > >
            > > This seems to work for me and seems a bit simplier
            > > to code.
            > >
            > > Regards,
            > >
            > > Ray[/color]
            >
            > Of course when the code is that simple, it doesn't make much difference.
            > But what if the code was supposed to do a million and one complicated
            > calculations, then create an Excel spreadsheet and save it in a location
            > specific to which button was pressed - and this time there were 5[/color]
            different[color=blue]
            > buttons.
            > Would you write all of the code and copy it 5 times altering a small part[/color]
            of[color=blue]
            > the code specific to each button? What then when you adjust your code,[/color]
            each[color=blue]
            > time keep 5 separate versions updated? By writing the bulk of the code[/color]
            only[color=blue]
            > once you save duplication with all the bug-risks that introduces and make[/color]
            it[color=blue]
            > far easier to read and maintain.[/color]

            Exactly. I already have enough spagetti in this app and I do not need any
            more. The sub was not yet debugged and was about 60 lines when I asked for
            help. By having the flag set, I only had to have one copy of debugged code
            in my app. If I discover a bug later on, I will only have to fix it in one
            place.


            Comment

            Working...