473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?
Nov 12 '05 #1
5 3232
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
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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JR | last post by:
Hi. I have a CGI script that will need to call itself an unknown number of times, to add rows, run queries, etc. At the bottom of the output that is produced by the script, there are four...
5
by: Lyn | last post by:
Hi, I hope someone can help. I have a main form which mostly fills the Access window. In the bottom half of this form I have a tab control to display various types of data related to the main...
5
by: Axel | last post by:
An Access 2000 question Hi is is possible to have (as a subform) a continous form with 0..n buttons which have different images in each row. (Personally I would have preferred a button...
5
by: Girish | last post by:
I have TWO submit buttons of type IMAGE on my asp form. This renders as <input type="image">. I need to be able to eble the ENTER button for both buttons. Yes, I know that for the input type...
13
by: axlq | last post by:
How do I create a form that two submit buttons, where each one submits the form input data to a different server? Consider this situation: I have a form where users can check various options to...
1
by: markroworth | last post by:
Hi there, I want to create a form with two buttons on that navigate to two different pages. The code I have at the moment looks like this: <form action="login.php" method="post"> <table...
3
by: Ryan Liu | last post by:
Hi, I have a big urgent problem to solve. I used to use Windows 2000 Chinese version, now I installed Windows XP (English) system. The problem is not about 2000 or XP, it is about English...
1
by: tarabztk | last post by:
MSACCESS 2003 : How to use command buttons to display different subforms I have an application that display information about Pipelines and these pipelines have many other equipments attached to...
2
by: Peter Oliphant | last post by:
I'm developing a program at home on a Windows Vista computer. In it I create a form with a few buttons on it that fill up the form's visible area. So, I took the application to work, where I use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.