Different response for button click

USAMax

Well-known Member
Joined
May 31, 2006
Messages
855
Office Version
  1. 365
Platform
  1. Windows
I have a subroutine that works the way I want but I also want to tie it to a button with a message, "Are you sure you want to..."

Is there is a way to tell if the program is called from another routine or from the button click.
 
And if it's called from another sub Application.Caller will return an error. Here's a simple example. Put these two routines in as separate modules. Run Test1 from View>Macros. Assign Test2 to a button so you can also run it directly from the button.
Code:
Sub Test1()
MsgBox "Hello - 1"
Call Test2
End Sub
Code:
Sub Test2()
MsgBox "Hello - 2"
If IsError(Application.Caller) Then
    MsgBox "I've been called from another sub"
Else
    MsgBox "I've been called from " & Application.Caller
End If
End Sub
 
Upvote 0
Hey Joe,

It looks like I spoke too soon. While testing my other program it was working just fine until I started it using another button. The subroutine that I added your code to is about 3 or 4 levels down, meaning The main program calls another program, that calls another program, that calls the program with your code. I thought it only looked at what called the current routine and not what started everything.

Any other suggestions?
 
Upvote 0
Hey Joe,

It looks like I spoke too soon. While testing my other program it was working just fine until I started it using another button. The subroutine that I added your code to is about 3 or 4 levels down, meaning The main program calls another program, that calls another program, that calls the program with your code. I thought it only looked at what called the current routine and not what started everything.

Any other suggestions?
I'm not sure I understand what your problem is. Can you elaborate a bit?
 
Upvote 0
I have a subroutine that has been working for a while called CreateCustomITL. It has a button to run it but while I test the program I don't use the button I just run it within the editor. CreateCustomITL calls DeleteTodaysBreaksAndOffsets that I have used for a while as well and it calls EraseCurEng where your code is.

The user wanted to be able to Erase the Current Engineer by himself so I created a button to that function that would ask, "Are you sure you want to Erase the Current Engineer" if routine was started using the button directly. The problem is that it also detects when the button for CreateCustomITL is clicked and that is not what I want.

Thank you,
 
Upvote 0
I have a subroutine that has been working for a while called CreateCustomITL. It has a button to run it but while I test the program I don't use the button I just run it within the editor. CreateCustomITL calls DeleteTodaysBreaksAndOffsets that I have used for a while as well and it calls EraseCurEng where your code is.

The user wanted to be able to Erase the Current Engineer by himself so I created a button to that function that would ask, "Are you sure you want to Erase the Current Engineer" if routine was started using the button directly. The problem is that it also detects when the button for CreateCustomITL is clicked and that is not what I want.

Thank you,
If called from a button you get the name of the button from Application.Caller, so you should be able to use this information to distinguish between the two buttons (make sure you assign different names to them) and have your code react accordingly.
 
Upvote 0
Thank you again Joe, the way you had it in the IsError I just assumed and did not attempt to try it that way.

I like to learn something every day but thanks to you I learned two things!
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top