By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,841 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,841 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?
Nov 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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(pstrWhichButton 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

"Colleyville Alan" <ae***********@nospam.comcast.net> wrote in message
news:4Hy1c.109844$Xp.484367@attbi_s54...
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?

Nov 12 '05 #2

P: n/a
Thanks!
"Andrew" <an**************@webster.org> wrote in message
news:V6******************@news-server.bigpond.net.au...
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(pstrWhichButton 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

"Colleyville Alan" <ae***********@nospam.comcast.net> wrote in message
news:4Hy1c.109844$Xp.484367@attbi_s54...
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?


Nov 12 '05 #3

P: n/a
Ray
"Andrew" <an**************@webster.org> wrote in message news:<V6******************@news-server.bigpond.net.au>...
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(pstrWhichButton 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

"Colleyville Alan" <ae***********@nospam.comcast.net> wrote in message
news:4Hy1c.109844$Xp.484367@attbi_s54...
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?

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
Nov 12 '05 #4

P: n/a
"Ray" <ra*****@excite.com> wrote in message
news:20**************************@posting.google.c om...
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


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



Nov 12 '05 #5

P: n/a
"Fletcher Arnold" <fl****@home.com> wrote in message
news:c2**********@sparta.btinternet.com...
"Ray" <ra*****@excite.com> wrote in message
news:20**************************@posting.google.c om...
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
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.


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.
Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.