In 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.
In Part 3 we integrated the calendar into other forms.
Here in part 4 we will switch some text boxes to drop down combo boxes.
(article continues after sponsor spot)
Combo boxes can often make life easier. Let's use the concept for our Month text box.
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.
With the form open in design mode:
- click once on the month's text box
- in the Access menu choose Format, Change To, Combo Box
- open the properties for the new combo box
- change the name to "cboMonth"
- the Control Source and Format properties should be blank
- change the Row Source Type to Value List
- change the Row Source to "1;January;2;February;3;March;4;April; 5;May;6;June;7;July;8;August; 9;September;10;October;11;November;12;December" (Note: I have spaces after April and August only because of space on this page; do NOT include spaces in your property; ALSO the quotes are just for emphasis, do NOT include the quotes)
- Column Count is 2
- Column Widths is 0cm;2.54cm
- Bound Column is 1
- List Rows is 12
- List Width is 1 inch or 2.54 cm
- Limit to List is Yes
- add the following code to the combo box's After Update event
Private Sub cboMonth_AfterUpdate()
myMonth = cboMonth.Value
- now we have to find all the references we had to intMonth and change them to cboMonth, and delete any intMonth code no longer needed as the combo box now takes care of it all automatically
- we also no longer need the strMonth variable or the code that handles it, or any code that references txtMonth
- sorry we have to hack at our code so much, but I needed you to see this in a very obvious way, and what better obvious way than one that undoes a lot of hard work :-(
- after fixing the code, try the Compile command under the Debug menu; it will show you any errors
NOTE: if you really get stuck on code, click here to see all the new code, BUT only do that as a last resort. Struggling to find your own errors will make you a better "coder" in the long run, rather than just copying the answer !!!
Note: This web site dedicated to MS Access database users is an independent publication of Richard W. Killey and is not affiliated with, nor has it been authorized, sponsored, or otherwise approved by Microsoft® Corporation.