Adding Calendar Feature

  • Thread starter Thread starter Rick Cass
  • Start date Start date
R

Rick Cass

I would like to have a calendar option next to a text box
to collect a date.

Can someone help me with adding a calendar feature and
how to get the selected date entered into the text box?

Please help.

Rick Cass
(e-mail address removed)
 
Hi Rick,

Lots of options for you.

You can use the Calendar ActiveX Control that ships with
your Access program. You'll find it under your ActiveX
Controls list.

To help you see how this works I suggest you go to Arvin
Meyer's site and download a couple of his sample databases
that demonstrate how to use the calendar.

http://www.datastrat.com/Download2.html

You may also want to check Tony Toews' site which lists
many calendar options including using forms and even an
API solution. You can find that here:

http://www.granite.ab.ca/access/calendars.htm

This is my personal favorite from Sensei Stephen Lebans:
I HIGHLY RECOMMEND IT!!

http://www.lebans.com/monthcalendar.htm

If you need step-by-step instructions on how to use it in
your database post back and I'll help.

Jeff Conrad
Access Junkie
Bend, Oregon
 
I don't know if Rick needs the step by step instructions but I would
certainly like them. Thanks

Dave
 
Hi Dave,

Stephen posted a new version just this evening so I will
need to change my procedures. I will write up some new
ones and post back tomorrow.

In the meantime, download the latest version and play with
the sample file. This latest version is just awesome; it
even includes the ability to bake a nice loaf of bread in
under 20 minutes. Very slick.

Jeff Conrad
Access Junkie
Bend, Oregon
 
Jeff Conrad said:
Hi Dave,

Stephen posted a new version just this evening so I will
need to change my procedures. I will write up some new
ones and post back tomorrow.

In the meantime, download the latest version and play with
the sample file. This latest version is just awesome; it
even includes the ability to bake a nice loaf of bread in
under 20 minutes. Very slick.

Ah, it's not so great. It doesn't do sourdough, for instance. <g>
 
Hi Dave,

Ok, here are the step-by-step instructions for using Sensei Stephen Lebans MonthCalendar
version 9.6.
You may not need to do all the steps; just skip the ones that don't apply.
Download either the 97 or 2000 version from this location:

http://www.lebans.com/monthcalendar.htm

(Please bow in reverence on your way in.)

1. Import the five modules (File-->Get External Data-->Import) from the Access 97/2000
demo database into your database.

2. Immediately compile your database before doing anything else! You could introduce
several nasty bugs to your database if your Access version is not completely patched and
up to date so don't skip this step!

3. If you do not know how to compile a database follow these steps:

For Access 97:
- You need to get to any code window. You can do this by either opening any form or module
in Design View and click the button on the toolbar that says "Code". Or type Ctrl-G to
open the VBA editor
- Then you go up to the toolbar and find the option that says: "Debug."
- From that menu you select "Compile and Save All Modules."

For Access 2000, 2002, 2003:
- You need to get to any code window. You can do this by either opening any form or module
in Design View and click the button on the toolbar that says "Code". Or type Ctrl-G to
open the VBA editor
- Then you go up to the toolbar and find the option that says: "Debug."
- From that menu you select "Compile <name of your database>."

What this does is go through ALL your code and look for any possible errors. It will
immediately stop on any problems it finds. Keep in mind that you may have errors in your
database code completely unrelated to Stephen's code. You will need to fix those errors on
your own.

You may likely run into a "References" problem with Stephen's code. The arch-nemesis of
Access is missing references. If you receive a message saying something like "Can't find
project or library" or "Undefined Function" it will most likely be due to the fact that
you do not have a reference set to the DAO object library and/or your file locations for
those libraries are in different places.

To fix the References problem follow these steps:
- Open any module in Design view.
- On the Tools menu, click References.
- At least one Reference should say "Missing". WRITE down the one(s) missing, most likely
it will be the DAO library.
- Click to clear the check box for the type library or object library marked as
"Missing:."
- Close the References Window box.
- Open the References Window back up. Hopefully, nothing should say Missing this time.
- Now go find that library/project(s) in the list that was missing.
- If it is the DAO one scroll down to you get to Microsoft DAO 3.xx and check it.
- If you're using Access 97 that should be DAO 3.51 Object Library.
- If you're using Access 2000, 2002, or 2003 that should be DAO 3.6 Object Library.
- If more than one were missing, find the others and check them as well.
- Close the References box again.
- Now re-compile again. Debug--Compile.
- Hopefully you should not see any more compile errors.

If you'd like to read more about References, here's more info than you could possibly ever
want to know:

http://members.rogers.com/douglas.j.steele/AccessReferenceErrors.html

http://members.iinet.net.au/~allenbrowne/ser-38.html

http://www.trigeminal.com/usenet/usenet026.asp

ACC2002: References That You Must Set When You Work with Microsoft Access
http://support.microsoft.com/?id=283115

ACC97: References That You Must Set When You Work with Microsoft Access
http://support.microsoft.com/?id=175484

ACC: VBA Functions Break in Database with Missing References
http://support.microsoft.com/?id=160870

4. Whew! OK, back to setting up the calendar! Now in your database open any form in design
view that you wish to show the calendar. Go to the code window behind the form. In the
Declarations area (very top) you need to add this code. So the very first few lines of
your code window will look like this:

Option Compare Database
Option Explicit

Private mc As clsMonthCal
' This declares the MonthCalendar Class
' Brought to you by Lebans Holdings: We make the Impossible -> Possible

5. Now go the Form's Load event and enter this code:

Private Sub Form_Load()

' Create an instance of our Class
Set mc = New clsMonthCal
' Set the hWndForm Property
mc.hWndForm = Me.hWnd

End Sub

6. Now go to the Form's Unload event and enter the following code:

Private Sub Form_Unload(Cancel As Integer)

' This is required in case user Closes Form with the
' Calendar still open. It also handles when the
' user closes the application with the Calendar still open.

If Not mc Is Nothing Then
If mc.IsCalendar Then
Cancel = 1
Exit Sub
End If
Set mc = Nothing
End If

End Sub

7. Now find the CONTROL on the form that you wish to have the calendar fill the date.
Right Click on the control and
go to Properties from the list. On the "All" or "Event" tab you should see an option that
says "On Dbl Click". Hit
the little (...) button and it will take you to the code window again. We need to add this
code:

Private Sub txtSelectDate_DblClick(Cancel As Integer)

Dim blRet As Boolean
Dim dtStart As Date, dtEnd As Date

dtStart = Nz(Me.txtSelectDate.Value, 0)
dtEnd = 0

blRet = ShowMonthCalendar(mc, dtStart, dtEnd)
If blRet = True Then
Me.txtSelectDate = dtStart
Else
' Add any message here if you want to
' inform the user that no date was selected
End If

End Sub

In my example above the control was a text box called txtSelectDate. You will need to
change those parts of the code to whatever you have named your control. You could also
assign this to the click event for a text box, but I think using the Double Click event is
a much better idea. A small command button next to a text field could also be used.

8. Compile the code and save the form.

9. Now open the form in regular view and double-click that text box. The calendar should
then appear. Selecting a date on the calendar will cause it to disappear and the resulting
date to be filled into the text field. Simply marvelous isn't it? Please take a moment and
wipe the drool off your face before continuing.

10. Stephen's calendar also allows you to select a range of dates. The way Stephen
designed the code there are actually many ways to accomplish this goal. I will only
present one method, but feel free to experiment with other methods.

This is my personal preference for selecting a date range.
- To do this you will need two text boxes: one to hold the starting date and one to hold
the ending date. Let's call them txtBeginDate and txtEndDate.
- Now create a command button and select cancel if using the wizard. Call it cmdDateRange
and either set a caption property on it or use the little calendar icon for a picture.
(I'll leave any formatting to your tastes).
- Now Right Click on the command button and go to Properties from the list. On the "All"
or "Event" tab you should see an option that says "On Click". Hit the little (...) button
and it will take you to the code window again. We need to add this code:

Private Sub cmdDateRange_Click()

' Retrieve the currently selected date(s).
' Call our Function to display the Calendar
Dim blRet As Boolean
Dim DateStart As Date
Dim DateEnd As Date
' If the control is NULL then use Today's date.
DateStart = Nz(Me.txtBeginDate.Value, Date)
DateEnd = DateStart + 7

' Use named parameters for clarity
blRet = ShowMonthCalendar(clsMC:=mc, StartSelectedDate:=DateStart, _
EndSelectedDate:=DateEnd)

If blRet = True Then
Me.txtBeginDate.Value = DateStart
Me.txtEndDate.Value = DateEnd
Else
' Add any message here if you want to
' inform the user that no date was selected
Me.txtBeginDate.Value = Null
Me.txtEndDate.Value = Null
End If

End Sub

- Compile the code and save the form.

- Now open the form in regular view and click the command button. The calendar should
then appear.

**Important Note**
In order to select a range of dates, Single-Click interface must be enabled on the
Calendar Properties List.

Click where it says Properties on the calendar, slide down to the option that says "Single
or Double Click" and then move to the right to select "Single Click to Select Date." It
may also help to display more than one month at a time for this procedure. To do that go
to the Properties button again, slide down to where it says "Viewable Months", and then
move to the right and select the number of months you wish to display.
**

- Now click on a beginning date and hold down the mouse button. Slide the mouse over to
whatever ending date you desire and the selected dates will be highlighted as you move.
Let go of the mouse, the calendar will disappear, and the text boxes will fill in with the
respective dates. Pinch yourself, it's not a dream. Lift your chin off the keyboard before
it starts hitting some keys.

11. Stephen has hard coded a maximum date range limit of 31 days. There may be an easier
way to do this (and please let me know if there is Stephen), but you can change this by
modifying one line in his class code.
**Warning-Tread carefully here**

- Open the clsMonthCal in Design View.
- Go up to on the toolbar and select Edit--Find.
- Copy/paste this text in the box that says "Find What": m_MaxSelectRangeofDays = 31
- Make sure "Current Module" is selected and then hit the button that says "Find Next."
- The editor should take you right to the line we need to change.
- Just enter a new number in place of 31.
- Compile the code and save the module changes.
- Go back to your form and do a date range again. You should now be able to select a range
of dates matching your criteria.

12. The really great thing about the calendar (besides the fact that is totally API driven
and therefore no versioning problems like ActiveX Controls) is that it can be customized
to the user's desires via the Preferences options. Each user can choose what options they
wish to use to suit their own likes/dislikes. Try out each of the options in the
Properties list to see what difference it makes to the calendar. You can choose how many
months to view, show week numbers, select different fonts and colors, you can show and
circle the current day for quick retrieval, specify the location where the calendar will
appear, and finally choose between double-clicking a date or single clicking a date. And
the developer doesn't have to do any additional coding. Simply beautiful isn't it?
.........I need a moment.............sniff..........Ok I'm fine now.

That should do it (I think).

Good luck and have fun with it!
Jeff Conrad
Access Junkie
Bend, Oregon
 
Ah, it's not so great. It doesn't do sourdough, for instance. <g>

ROFL!!!
Yes, I agree, but I don't want to press the issue at the moment.
<vbg>

Jeff Conrad
Access Junkie
Bend, Oregon
 
Jeff I will place this post on the MonthCalendar Webite. Thank you!

As for the MaxRangeOfDays property, you do not have to go into the Init
event of the class to change the private
variable(m_MaxSelectRangeofDays ). Just set it in the Form's Load event
or before you call the Calendar via the exposed MaxSelectRangeofDays
property.

mc.MaxSelectRangeofDays = 365

Thanks again Jeff!
:-)

--
HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Hi Sensei,
Jeff I will place this post on the MonthCalendar Webite. Thank you!

You're welcome, it's the least I could do.
As for the MaxRangeOfDays property, you do not have to go into the Init
event of the class to change the private
variable(m_MaxSelectRangeofDays ). Just set it in the Form's Load event
or before you call the Calendar via the exposed MaxSelectRangeofDays property.

mc.MaxSelectRangeofDays = 365

Ohhhhhh said the idiot.
Well that's much better than messing with your code!

Humm...I suppose I could even ask for the user's input before bringing up the calendar.
Something like this (somewhat clumsy) code works fine with error handling:

Private Sub cmdDateRange_Click()
On Error GoTo ErrorPoint

Dim strMaxDays As String

strMaxDays = InputBox("Please enter the maximum number of days " _
& " to use for the range.", "Enter Range Number")

StartOver:
mc.MaxSelectRangeofDays = CInt(strMaxDays)

' Retreive the currently selected date(s).
' Call our Function to display the Calendar
Dim blRet As Boolean
Dim DateStart As Date
Dim DateEnd As Date
' If the control is NULL then use Todays date.
DateStart = Nz(Me.txtBeginDate.Value, Date)
DateEnd = DateStart + 7

' Use named parameters for clarity
blRet = ShowMonthCalendar(clsMC:=mc, StartSelectedDate:=DateStart, _
EndSelectedDate:=DateEnd)

If blRet = True Then
Me.txtBeginDate.Value = DateStart
Me.txtEndDate.Value = DateEnd
Else
' Add any message here if you want to
' imform the user that no date was selected
Me.txtBeginDate.Value = Null
Me.txtEndDate.Value = Null
End If

ExitPoint:
Exit Sub

ErrorPoint:
If Err.Number = 13 Then
strMaxDays = 365
GoTo StartOver
Else
Resume ExitPoint
End If

End Sub

Works pretty nicely, but I would prefer to make a special pop-up box in Dialog mode for
the user just for this situation to enter the number. I could more easily control what
type of data they enter instead of the clumsy error handling code with an InputBox.

Very slick Sensei, thanks for the additional information!
Jeff Conrad
Access Junkie
Bend, Oregon
 
"Stephen Lebans"
Jeff I will place this post on the MonthCalendar Webite. Thank you!

Dunno about Stephen but I, for one, can't write good documentation.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
 
Same for me Tony. Especially in the case here where I am producing
freeware stuff. The last thing I want to do is take the time to sit down
and write out docs... way too boring!
I want to go onto the next project.
:-)

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Thank you both. I followed the instructions and it worked first time, which
is very good for this learner. One last thing. Once the date field has the
selected date I would like it to automatically TAB to the next field. I've
tried the properties auto tab but it doesn't seem to work. Can we add a line
of code?

Dave B
 
Hi Dave,
Thank you both. I followed the instructions and it worked
first time, which is very good for this learner.

That's great news!
One last thing. Once the date field has the selected date
I would like it to automatically TAB to the next field.
I've tried the properties auto tab but it doesn't seem to
work. Can we add a line of code?

Sure, use the SetFocus command to move the focus to
another field just before exiting the procedure. Something
like so (look at the last line):

Private Sub StartingDate_DblClick(Cancel As Integer)

Dim blRet As Boolean
Dim dtStart As Date, dtEnd As Date

mc.MaxSelectRangeofDays = 1

dtStart = Nz(Me.StartingDate.Value, 0)
dtEnd = 0

blRet = ShowMonthCalendar(mc, dtStart, dtEnd)
If blRet = True Then
Me.StartingDate = dtStart
Else
' Add any message here if you want to
' inform the user that no date was selected
End If

Me.txtSomeOtherField.SetFocus

End Sub

Just change txtSomeOtherField to the name of the next text
field you would like to jump to. That should do it.

Hope that helps,
Jeff Conrad
Access Junkie
Bend, Oregon
 
Back
Top