Link to home
Start Free TrialLog in
Avatar of lucavilla
lucavillaFlag for Italy

asked on

VBA: how to return a value from a pick-a-date-frmo-calendar sub

I successfully used the following code in a VBA script to let the user pick a date from a calendar:

Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
    On Error Resume Next
	MsgBox (DateClicked)
    Unload Me
End Sub

Open in new window



Question is: how can I read that value DateClicked from another sub?
If I launch the MsgBox (DateClicked) inside the sub above I see a date, while
If I launch it from another sub I see an empty msgbox :(
Avatar of Randy Poole
Randy Poole
Flag of United States of America image

Store the value as a global and then access dClick when you need it
Dim dClick as Date
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
    dClick=DateClicked
End Sub

Open in new window

Avatar of lucavilla

ASKER

A msgbox(dClick) in another sub display a "00:00:00"  :(
Did you activate the click event on MonthView1 before you called the other sub?
hmm no, how do I do it?
click on the MonthView1 component you put on the sheet and select a date
Not sure if I understood, I just created a button and assigned to it a macro named MySub that calls the calendar with this command:
UserForm1.Show

After the user selects a date, the calendar sub MonthView1_DateClick ends with an "Unload Me" and all the other instructions after "UserForm1.Show" of MyMacro are executed.
The problem is that I cannot read the date from these following commands in MySub.
I can only read it from inside the MonthView1_DateClick sub.
ASKER CERTIFIED SOLUTION
Avatar of Randy Poole
Randy Poole
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works wonderfully! Thanks!


In UserForm1 I wrote:
Public Sub MonthView1_DateClick(ByVal dateClicked As Date)
    On Error Resume Next
    dClick = dateClicked
    Unload Me
End Sub


In Module1 I wrote:
Public dClick As Date
Sub Aggiorna_dati_consulenza()
    UserForm1.Show
    MsgBox (dClick)
End Sub



Msgbox correctly show the selected date "17/07/2014"!