Solved

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

Posted on 2014-07-22
8
914 Views
Last Modified: 2014-07-23
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 :(
0
Comment
Question by:lucavilla
  • 4
  • 4
8 Comments
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40211793
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

0
 

Author Comment

by:lucavilla
ID: 40211811
A msgbox(dClick) in another sub display a "00:00:00"  :(
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40211834
Did you activate the click event on MonthView1 before you called the other sub?
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:lucavilla
ID: 40211846
hmm no, how do I do it?
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40211895
click on the MonthView1 component you put on the sheet and select a date
0
 

Author Comment

by:lucavilla
ID: 40211939
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.
0
 
LVL 21

Accepted Solution

by:
Randy Poole earned 500 total points
ID: 40212023
Gotcha, add a module to the VBA code, add the following code:
Public dClick as Date

Open in new window


then in your user form:
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
    On Error Resume Next
        Module1.dClick=DateClicked
	MsgBox (DateClicked)
    Unload Me
End Sub

Open in new window

You can now access Module1.dClick from any macro
0
 

Author Closing Comment

by:lucavilla
ID: 40213662
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"!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question