calendar

T

TomS

Hello,
I am preparing small application in VBA for Word (Office 2000/XP) and need
a calendar to pick a date to text field. I tried to use MonthView control,
but it occured that it is not available on some machines. Installing this
ActiveX control on all machines is a big problem - application must work
without installing any additional software.
So I try to make it so, that this control will be shown only when it is
available. I have loaded it dynamically and check if it goes ok. But I
have problem: getting events from control doen't work.
Example code:

Private ctlCalendar As Control
Private Sub UserForm_Activate()
On Error Resume Next
Set ctlCalendar = Me.Controls.Add("MSComCtl2.MonthView.2",
"ctlCalendar", true)
end sub

Private Sub ctlCalendar_DateClick(ByVal DateClicked As Date)
txtDate = FormatDateTime(DateClicked, vbShortDate)
ctlCalendar.Visible = False
End Sub

Above event is not launched. When I have added a control in "normal way",
on a form - it works. What should be done to get events from dynamically
created control?


Second question: is there any other calendar-control that is more popular?
Application will be installed on systems with Windows2000/Office XP.

Thanks in advance for any help.
TomS
 
J

Jezebel

That's the problem with dynamically loaded controls ... you can't trap their
events.
 
T

TomS

That's the problem with dynamically loaded controls ... you can't trap
their

sad news.... :-(
I have found some info about catching events for controls created in
runtime, but it was for VB (it used VBControlExtender that raised
ObjectEvent with EventInfo parameter).
Are you 100% sure that it is not possible in VBA?

And, as I suppose, I will not find any other calendar-control that will be
commonly available in Windows2000/OfficeXP...?

Thanks for information,
TomS
 
T

Thomas Winter

TomS said:
Hello,
I am preparing small application in VBA for Word (Office 2000/XP) and need
a calendar to pick a date to text field. I tried to use MonthView control,
but it occured that it is not available on some machines. Installing this
ActiveX control on all machines is a big problem - application must work
without installing any additional software.
So I try to make it so, that this control will be shown only when it is
available. I have loaded it dynamically and check if it goes ok. But I
have problem: getting events from control doen't work.
Example code:

Private ctlCalendar As Control
Private Sub UserForm_Activate()
On Error Resume Next
Set ctlCalendar = Me.Controls.Add("MSComCtl2.MonthView.2",
"ctlCalendar", true)
end sub

Private Sub ctlCalendar_DateClick(ByVal DateClicked As Date)
txtDate = FormatDateTime(DateClicked, vbShortDate)
ctlCalendar.Visible = False
End Sub

Above event is not launched. When I have added a control in "normal way",
on a form - it works. What should be done to get events from dynamically
created control?


Second question: is there any other calendar-control that is more popular?
Application will be installed on systems with Windows2000/Office XP.

Thanks in advance for any help.
TomS

Here is a solution that appears to work for me!

What you need to do is:

1. Go to TOOLS | REFERENCES, then click BROWSE. Change FILES OF TYPE to OCX
files and then go find the file MSCOMCT2.OCX in your system folder and add
it. You can also get what you need here by adding it as an additional
control on the Controls toolbox, but then it appears you actually have to
put one on the form to get the reference to work.

2. Change your ctlCalendar defintion as follows. Note the WITHEVENTS keyword
and that we've defined the control as exactly what we want it to be. This
will get you the events.

Private WithEvents ctlCalendar As MSComCtl2.MonthView

3. Change the code as follows:

Private Sub UserForm_Activate()
On Error Resume Next
Set ctlCalendar = Me.Controls.Add("MSComCtl2.MonthView.2",
"ctlCalendar", True)
End Sub

Private Sub ctlCalendar_DateClick(ByVal DateClicked As Date)
txtDate = FormatDateTime(DateClicked, vbShortDate)

Dim oControl As Control
Set oControl = ctlCalendar
oControl.Visible = False
End Sub

Note that in order to access the "normal" control properties provided by
VBA, like VISIBLE, we have to reference the control as a CONTROL object.

This works fine when the MSCOMCT2.OCX is registered on the system. I tested
it by unregistering the OCX and then running the form. The control did not
appear and no errors where generated. You just have to make sure you have
the control registered on your machine during development.

Hope that helps!

-Tom
 
T

TomS

2. Change your ctlCalendar defintion as follows. Note the WITHEVENTS
keyword
and that we've defined the control as exactly what we want it to be. This
will get you the events.

Private WithEvents ctlCalendar As MSComCtl2.MonthView

I have tried it and it worked. But while MSCOMCT2.OCX control was
unregistered, in VBA project still MSComCtl2.MonthView type reference was
available. On machine without mscomctl2 oabove line will (probably) fail.
But this I will test tomorrow...

Thanks for very interesting post.

TomS.
 
J

Jonathan West

Hi Tom,

If you don't want to rely on external controls, its not that hard to whip up
a calender control out of spinbuttons and labels.

On your UserForm, create 3 labels, called lbDay, lbMonth and lbYear. (I
format the labels as sunken and with a white background as well.) Put a
spinbutton next to each one, called spDay, spMonth and spYear.

Add a module-level variable as follows

Dim mDate as Date

and add the following event procedures

Private Sub spDay_SpinUp()
mDate = DateAdd("d", 1, mDate)
SetDate
End Sub

Private Sub spDay_SpinDown()
mDate = DateAdd("d", -1, mDate)
SetDate
End Sub

Private Sub spMonth_SpinUp()
mDate = DateAdd("m", 1, mDate)
SetDate
End Sub

Private Sub spMonth_SpinDown()
mDate = DateAdd("m", -1, mDate)
SetDate
End Sub

Private Sub spYear_SpinUp()
mDate = DateAdd("yyyy", 1, mDate)
SetDate
End Sub

Private Sub spYear_SpinDown()
mDate = DateAdd("yyyy", -1, mDate)
SetDate
End Sub

Private Sub UserForm_Initialize()
mDate = Now
SetDate
End Sub

Private Sub SetDate()
lbDay.Caption = Format(mDate, "d")
lbMonth.Caption = Format(mDate, "MMMM")
lbYear.Caption = Format(mDate, "yyyy")
End Sub

You end up with a date which is initialized to today (you can of course
change that my changing the value of mDate set in the UserForm-Initialize
event) and which you can increment or decrement by a day, month or year. To
get the current date as shown by the control, just read the current value of
mDate.

--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup
 

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

Similar Threads


Top