Today I am going to explain how custom functions can be utilized in calculated form controls, without needing to invoke them with VBA itself.
Let’s begin by looking at the custom function we are going to use.
Custom Function to get Week Commencing
1 |
getWeekCommencing (weekNumber, Year) |
The code I used to create this function is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Public Function getWeekCommencing(intWeekNo, intYear) On Error GoTo myError Dim intJan1 As Integer Dim varStartDate As Variant intJan1 = Weekday(DateSerial(intYear, 1, 1), vbMonday) varStartDate = DateAdd("d", -(intJan1 - 1), DateSerial(intYear, 1, 1)) getWeekCommencing = DateAdd("ww", intWeekNo - 1, varStartDate) leave: Exit Function myError: MsgBox Error$ Resume leave End Function |
If you are unfamiliar with VBA you can still copy this code into your database and use it within a calculated control. There are instructions for doing this below:
Instructions for adding the function to your database
- Copy the code above onto your clipboard.
- Open the database you are going to add the function to.
- Select the CREATE ribbon.
Above: The MODULE icon is located on the right-hand side of the CREATE ribbon. - Click the MODULE icon from the MACROS AND CODE group. This opens the VBA editor and creates a new module ready for the code to be added:
- Paste the code into the VBA editor as in the screenshot below:
Above: The VBA Editor:
You may notice that I have changed the default name of the Module (see the PROJECT EXPLORER in the
left-hand side bar) from Module 3 to “myFunctions“. You can do the same by highlighting the module name in the
project explorer and hitting the F4 button to bring up the PROPERTIES window. You may then change the name property
to one of your choice. However, this is not essential.
|
Setting up the Test Data for this Exercise
Above: tblWeeks in Design View |
Above: tblWeeks in Form View with test data entered. |
Once the table is set up, we can add our test data as I have done in the screenshot above. Although there are 52 week in the year, the first week may not start on a Monday, so the data entered in WeekOfYear field can be anything between 1 and 53 (so the possibility of a partial first week is offset by Week 53).
The Calculated Control
Now we have added the getWeekCommencing function along with test data to our database, we can create the form containing the calculated control.
Above: The Week Commencing Calculator form in Design View. |
To do this we need to create a form bound to the tblWeeks table. We then need to add three text boxes: the first text box (txtWeekOfYear) is bound to the WeekOfYear field in tblWeeks, and the second (txtYear) is bound to the Year field. The third text box (txtWeekCommencing) is unbound. This is where we are going to add our calculated control which returns the week commencing date (based on the data displayed in the WeekOfYear and Year textboxes).
Here are the instructions for creating the calculated control
- Create the third text box that we are going to use for the calculated control as in the screenshot above.
- Display the PROPERTY SHEET if it is not already visible. The PROPERTY SHEET icon is located in the TOOLS group of the DESIGN ribbon.
- Select the new text box by clicking on it.
- Click the OTHER tab of the PROPERTY SHEET, and change the NAME property to txtWeekCommencing. (I also suggest you change the names of the first textbox to txtWeekOfYear and the second to txtYear by selecting them and changing the NAME properties).
- Click the DATA tab on the PROPERTY SHEET (having first selected txtWeekCommencing). We are now going to enter an expression which uses our getWeekCommencing custom function.
- Enter the expression below directly into the CONTROL SOURCE property:
1 |
=getWeekCommencing([txtWeekNumber],[txtYear]) |
The txtWeekNumber parameter references the value in the txtWeekNumber textbox, and the txtYear parameter does the same for the txtYear text box.
In this exercise, the expression was entered directly into the CONTROL SOURCE
property. Alternatively, we could have clicked the elipse button at the end of the CONTROL SOURCE
property row to open the EXPRESSION BUILDER. Using the Expresison Builder is, however, something I would need to cover another day, in another post!
|
NB – Just to clarify, the format for the week commencing date in this screenshot is for the UK (ie dd/mm/yyyy).
The same date using the US format would be 01/07/2019 (mm/dd/yyyy). However, your computer should be set up
to display the date format appropriate for your location automatically. |