PDA

View Full Version : [SOLVED:] Application.Caller return value



simora
07-03-2022, 01:30 PM
I'm trying to have A Calendar Userform return the name of the button that called it, so that it can put the date in a different cell or TextBox, depending on which form or button it was called from, and if called from a UserForm, return the name of the UserForm.
I call that Calendar from different Macros from different locations.
Anyone know if Application.Caller can be used to do something like this.
If so, how would I code it to return the name of the caller as either a string or a variable.

Thanks

SamT
07-03-2022, 02:09 PM
The UserForm should have a Public Variable. ie Public "CalledBy" As String
You will need an intermediary sub to determine the caller and to call the calendar.
The buttons should call the intermediary sub

Sub Intermediary()
Load UserFormCalendar
UserFormCalendar.CalledBy = Application.Caller
UserFormCalendar.Show
End Sub
Edit terminology to suit

simora
07-03-2022, 07:10 PM
Hi:
Thanks for the input.
Still a little lost, but you've pointed me in some direction to investigate.
Thanks

Aflatoon
07-04-2022, 02:43 AM
Application.Caller will not work if you call the code from another form.

simora
07-04-2022, 02:02 PM
Hi Aflatoon (http://www.vbaexpress.com/forum/member.php?24778-Aflatoon) :

Thanks. I figured that bit out. & That's exactly what I was hoping was possible.
I did a kludge work around.
Put a hidden field into both userforms and stored the calling Userform button value there.

I appreciate all of your input.

snb
07-05-2022, 09:06 AM
A Userform control can have a _Click event.
In that case the 'calling control' is evident.


Private Sub Commandbutton1_Click()
msgBox commandbutton1.name
End Sub

simora
07-05-2022, 11:05 PM
snb (http://www.vbaexpress.com/forum/member.php?44644-snb):

On the Calendar, I was trying to find out what was calling the Calendar form.
The Calendar will return the date value to entirely different locations based on what called it.
That's what I was investigating.

snb
07-06-2022, 12:25 AM
No rocket science.

Alternative Code:


Private Sub CommandButton1_Click()
M_snb 1
End Sub

Private Sub CommandButton2_Click()
M_snb 2
End Sub

Sub M_snb(y)
UserForm1.Caption = OLEObjects(y).Name
UserForm1.Show
End Sub

Paul_Hossler
07-06-2022, 10:19 AM
I'm trying to have A Calendar Userform return the name of the button that called it, so that it can put the date in a different cell or TextBox, depending on which form or button it was called from, and if called from a UserForm, return the name of the UserForm.
I call that Calendar from different Macros from different locations.
Anyone know if Application.Caller can be used to do something like this.
If so, how would I code it to return the name of the caller as either a string or a variable.

Thanks



https://trevoreyre.com/portfolio/excel-datepicker/

Why re-invent the wheel?

Trevor Eyre has a free and very flexible date picker that I've found works very well

29912

snb
07-06-2022, 01:12 PM
ISO datepicker:

https://www.snb-vba.eu/VBA_Datepicker_en.html

simora
07-07-2022, 03:00 PM
snb (http://www.vbaexpress.com/forum/member.php?44644-snb)

This is what I did.

I send a value from any button calling the form to a hidden field on the UserForm1.
The form will return the date to a location based on what value the hidden field has.
Works like a charm for my purposes.
Anytime I call the UserForm1.Calendar, I just send it some specific button name or whatever, and it will return the date to the location that I desire.
'Place the date from the calendar into a location based on where its called from.


If UserForm1.TextBox1.Value = "1Form" Then
PForm.TextBox5.Value = UserForm1.Calendar1.Value
Else
MultiCheckForm.TextBox20.Value = UserForm1.Calendar1.Value
End If

'Dismiss the userform
Unload Me

simora
07-07-2022, 03:03 PM
Paul_Hossler (http://www.vbaexpress.com/forum/member.php?9803-Paul_Hossler)
Hi Thanks:

But I am NOT trying to create a Calendar form.
I already have one that I'm using.
My question was about having different buttons call it, and getting it to return the date to a specific location based on the caller.

Paul_Hossler
07-07-2022, 04:27 PM
In each of the Button_Click subs that call the Userform, store the .Caption (or any identifying data) in a Public variable

Use that after you select a date from the userform to put the answer where you want

snb
07-08-2022, 01:03 AM
Did you notice http://www.vbaexpress.com/forum/showthread.php?70050-Application-Caller-return-value&p=415695&viewfull=1#post415695 ??