In Access, you can calculate the number of specific days of the week between two dates by using an expression. For example, you can enter a start date and an end date on a form and display the number of Wednesdays between the two dates. The following steps demonstrate this technique by creating a form, inserting text boxes and a combo box, and entering the expression.
- In the Database window, click Forms under Objects, and then click New.
- In the New Form dialog box, click Design View, and then click OK.
- Using the Text Box tool in the toolbox, add three unbound text boxes to your form.
- Set the Name property for the first text box to StartDate, the second to EndDate, and the third to NumOfDays.
- Set the Format property for the StartDate and EndDate text boxes to Short Date.
- Set the Control Source property for the NumOfDays text box to the following expression:
=DateDiff("ww",[StartDate],[EndDate],[DayOfWeek])-Int([DayOfWeek]=Weekday([StartDate]))
- With the Control Wizards button in the toolbox pressed in, use the Combo Box tool to add a combo box to your form.
- On the first page of the Combo Box Wizard, click I will type in the values that I want.
- On the second page of the wizard, type 2 into the Number of columns text box.
- In the two columns, enter the following values:
| Col1 |
Col2 |
| Sunday |
1 |
| Monday |
2 |
| Tuesday |
3 |
| Wednesday |
4 |
| Thursday |
5 |
| Friday |
6 |
| Saturday |
7 |
- On the third page of the wizard, click Col2 in the Available Fields box, and then click Finish.
- Set the Name property for the combo box to DayOfWeek.
- Switch to Form view.
- In the StartDate text box, type a start date (mm/dd/yy).
- In the EndDate text box, type an end date (mm/dd/yy).
- In the DayOfWeek combo box, click a day of the week in the drop-down list.
The NumOfDays text box displays the number of occurrences of the weekday selected in the combo box.