PC Review


Reply
Thread Tools Rate Thread

Avoiding verbose code

 
 
Geoff
Guest
Posts: n/a
 
      30th May 2008
Hi
Two custom menu buttons execute the same basic code but with one additional
function provided by the second button.
To avoid repeating the basic (lengthy) code in two modules it would be great
if I could detect which button was selected.
If this is possible then, noting the additional function is executed part
way through the basic code, the pseudo code would look like this:

Sub MenuSetUp()
btn1.OnAction = "MainProc"
btn2.OnAction = "MainProc"
End Sub

Sub MainProc()
basic code
If btn2 selected then do additional function
rest of basic code
End Sub

I'm guessing each button must have a click event to trigger its OnAction
statement but how do I trap this? I'd appreciate any help on this.

Geoff


 
Reply With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      30th May 2008
You have pushed the unique actions too far down.

Sub Button1code()
Call Commonstuff
End Sub

Sub Button2code()
Call Commonstuff
do unique stuff
End Sub

There is no reason that Commonstuff must do actions that the specific button
code can perform.
--
Gary''s Student - gsnu200789


"Geoff" wrote:

> Hi
> Two custom menu buttons execute the same basic code but with one additional
> function provided by the second button.
> To avoid repeating the basic (lengthy) code in two modules it would be great
> if I could detect which button was selected.
> If this is possible then, noting the additional function is executed part
> way through the basic code, the pseudo code would look like this:
>
> Sub MenuSetUp()
> btn1.OnAction = "MainProc"
> btn2.OnAction = "MainProc"
> End Sub
>
> Sub MainProc()
> basic code
> If btn2 selected then do additional function
> rest of basic code
> End Sub
>
> I'm guessing each button must have a click event to trigger its OnAction
> statement but how do I trap this? I'd appreciate any help on this.
>
> Geoff
>
>

 
Reply With Quote
 
stefan onken
Guest
Posts: n/a
 
      30th May 2008
hi Geoff,
you could use

Application.Caller(1) or
Application.CommandBars.ActionControl.Index

to get the Index of the pressed button.

Sub MainProc()
basic code
If Application.Caller(1) =2 then do additional function
rest of basic code
End Sub

stefan

On 30 Mai, 11:13, Geoff <Ge...@discussions.microsoft.com> wrote:
> Hi
> Two custom menu buttons execute the same basic code but with one additional
> function provided by the second button.
> To avoid repeating the basic (lengthy) code in two modules it would be great
> if I could detect which button was selected.
> If this is possible then, noting the additional function is executed part
> way through the basic code, the pseudo code would look like this:
>
> Sub MenuSetUp()
> * *btn1.OnAction = "MainProc"
> * *btn2.OnAction = "MainProc"
> End Sub
>
> Sub MainProc()
> * *basic code
> * *If btn2 selected then do additional function
> * *rest of basic code
> End Sub
>
> I'm guessing each button must have a click event to trigger its OnAction
> statement but how do I trap this? *I'd appreciate any help on this.
>
> Geoff


 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      30th May 2008
If I follow you correctly you are suggesting there should be 3 procs so that:

Sub Button1code()
Call Commonstuff1
Call Commonstuff2
End Sub

and

Sub Button2code()
Call Commonstuff1
Uniquestuff
Call Commonstuff2
End Sub

I could do that but it would still be nice to know how to recognise which
button was clicked. <g>

Geoff


"Gary''s Student" wrote:

> You have pushed the unique actions too far down.
>
> Sub Button1code()
> Call Commonstuff
> End Sub
>
> Sub Button2code()
> Call Commonstuff
> do unique stuff
> End Sub
>
> There is no reason that Commonstuff must do actions that the specific button
> code can perform.
> --
> Gary''s Student - gsnu200789
>
>
> "Geoff" wrote:
>
> > Hi
> > Two custom menu buttons execute the same basic code but with one additional
> > function provided by the second button.
> > To avoid repeating the basic (lengthy) code in two modules it would be great
> > if I could detect which button was selected.
> > If this is possible then, noting the additional function is executed part
> > way through the basic code, the pseudo code would look like this:
> >
> > Sub MenuSetUp()
> > btn1.OnAction = "MainProc"
> > btn2.OnAction = "MainProc"
> > End Sub
> >
> > Sub MainProc()
> > basic code
> > If btn2 selected then do additional function
> > rest of basic code
> > End Sub
> >
> > I'm guessing each button must have a click event to trigger its OnAction
> > statement but how do I trap this? I'd appreciate any help on this.
> >
> > Geoff
> >
> >

 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      30th May 2008
Hi Stefan

ActionControl.index was what I was looking for.

Thank you for that.

Geoff

"stefan onken" wrote:

> hi Geoff,
> you could use
>
> Application.Caller(1) or
> Application.CommandBars.ActionControl.Index
>
> to get the Index of the pressed button.
>
> Sub MainProc()
> basic code
> If Application.Caller(1) =2 then do additional function
> rest of basic code
> End Sub
>
> stefan
>
> On 30 Mai, 11:13, Geoff <Ge...@discussions.microsoft.com> wrote:
> > Hi
> > Two custom menu buttons execute the same basic code but with one additional
> > function provided by the second button.
> > To avoid repeating the basic (lengthy) code in two modules it would be great
> > if I could detect which button was selected.
> > If this is possible then, noting the additional function is executed part
> > way through the basic code, the pseudo code would look like this:
> >
> > Sub MenuSetUp()
> > btn1.OnAction = "MainProc"
> > btn2.OnAction = "MainProc"
> > End Sub
> >
> > Sub MainProc()
> > basic code
> > If btn2 selected then do additional function
> > rest of basic code
> > End Sub
> >
> > I'm guessing each button must have a click event to trigger its OnAction
> > statement but how do I trap this? I'd appreciate any help on this.
> >
> > Geoff

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code for avoiding slashes, ampersands, etc. when naming sheets pickytweety Microsoft Excel Programming 6 15th Jul 2009 05:48 PM
Avoiding page breaks across merged cells - Code not working as expected Alan Microsoft Excel Programming 2 26th Sep 2005 03:55 AM
Avoiding 400 Error code =?Utf-8?B?bm9zcGFtaW5saWNo?= Microsoft Excel Programming 2 2nd Feb 2005 07:55 PM
Avoiding Duplication of Code Guadala Harry Microsoft C# .NET 2 18th Aug 2004 05:42 PM
Avoiding adding another code group Rodney Microsoft Dot NET Framework 5 3rd Feb 2004 05:33 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:09 PM.