Calendar control

J

Joanne

Hello,
A portion of a macro I'm writing has a calendar control. I put in the
following code
Public Sub ResetCalendar()
Dim vReset As Date
Load frmCalendar
frmCalendar.Show
vReset = Format(Calendar1, "mmm d, yyyy")
MsgBox vReset
End Sub

And strangley the message box always just gives me the time !! I want to
capture the value of the date in the "mmm d, yyyy" format and put it in the
document in 4 places. Do I need to assign a value to "Calendar1"? I've
assigned vreset in the declarations area as a date value. Thanks in advance
for any help you can provide.
 
J

Jay Freedman

I'm not sure how you got this to run at all; for me it stops with an error.
The problem is that the "Calendar1" the way you've used it is undefined --
VBA doesn't know that it's the name of a control you added to the userform.
To make it work, use the "member-of" dot notation to show that Calendar1 is
a member of frmCalendar:

vReset = Format(frmCalendar.Calendar1, "mmm d, yyyy")

Something that will help in the future is to put the Option Explicit
instruction at the top of every module
(http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm). Then VBA
would have highlighted Calendar1 and told you that it was not defined.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Joanne

Thank you very much. I did define the calendar control earlier in the macro.
I'm sorry, I should have mentioned that. It's just that it keeps displaying
only the time and no date. I don't understand it.
 
G

Gordon Bentley-Mix on news.microsoft.com

Joanne,

I think the problem may be that you've declared vReset as a Date type
variable. The Format function returns a Variant (String), which Word is
forcing into a Date type because of the declaration. Try declaring vReset as
a String and see if that works. (It doesn't really need to be a Date anyway
since it doesn't appear that you're doing any type of date-related operations
with it - at least not in the portion of the code you've provided.)

Also, like Jay, I wonder about that 'Calendar1'. You say it's a calendar
control, and that you've declared it elsewhere in your code. However, it
appears that you're relying on whatever the default property is of the
control to supply the value to the Format function. Perhaps if you explicitly
declared a property - like .Value - it would work.
--
Cheers!

Gordon Bentley-Mix
Word MVP

Please post all follow-ups to the newsgroup.

Read the original version of this post in the Office Discussion Groups - no
membership required!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top