| DatabaseLessons.com |
'Serving the Microsoft® Access |
|
|
Custom Calendar Form in MS Access - Part 3In Part 1 of this article on creating a "pure" Access date picker (aka calendar), we got as far as labelling all the days of the current month. In Part 2 we made our calendar look better, and learned how to use some VBA to allow the user to change the displayed month and year. Now in Part 3 we will learn how to integrate the calendar into other forms. (article continues after sponsor spot) In order to get the chosen date back to another form, normally for use in a text box, I chose to use a global variable. This is a variable that is available to every object in your Access database that can handle variables. I did this by defining it in a module. NOTE: All the VBA code segments on the Database Lessons site assume that you have DAO references active. If you are not sure what this means, and you are using Microsoft Access 2000 or higher, click here. If you already have a common module for other purposes, just add the one required line to it. If you have no modules, create one, add the code, and save it, possibly with a name of "DateModule".
Next, we have to get our calendar form to put a value in the variable. This is where the Choosedate function comes in. Remember it? We arranged to have the On Click event for all the little buttons on our calendar form set to =Choosedate(). The key to the ChooseDate function is its ability to know which button was clicked. For this, I used the following code segment.
This will tell me the name of the active control on the form. Since we are clicking once on a button, then that button is the active control. When I inserted the following code in the module of the calendar form ...
... and clicked on one of the buttons, the message box showed me "cmd12". Now we just need to determine what date that is. We know how we calculated the captions when we created the form, so we just need to reverse that logic. Or do we? If we can display the control's name, why not just display its caption property. So, I tested Screen.ActiveForm.ActiveControl.Caption and it worked! The following code determines the chosen date, places that date in the global variable "gChosenDate" and then closes the calendar form.
Let's go back to the original form that needs to allow us to pick a date.
The "acDialog" at the end forces the user to complete the date picking process before moving on. Once the date has been chosen, control comes back to the calling form and you can place the chosen date (held in variable gChosenDate) into a text box, or use it in a calculation, etc. One little detail remains. What if the user clicks the date choosing button on the original form by mistake? How do they cancel the whole process?
In Part 4 of this series on Creating a Custom Calendar, I will look at converting the text box for the month to a combo box. It saves a lot of VBA code. Any other questions about this article? |
|
|
© 2006, 2007, 2008 Richard W. Killey. All Rights Reserved. - Privacy Policy |