Enter Compact Dates in Microsoft Access Forms with VBA
Question details
Enter compact date values in an Access form and convert them into validated dates.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A user wants to type dates without separators, such as 20190915, into a form.
- Observed behavior
- Access does not interpret compact date values as dates in every form configuration, and an input mask alone may not validate or convert them reliably.
Confirm that the destination field has the Date/Time data type and that the form is editable. The example below accepts exactly eight digits in YYYYMMDD order, so decide on a separate rule if you also need six-digit dates.
Validate YYYYMMDD dates with an Access VBA routine
Use an unbound text box and a button to validate compact input before assigning a date to a bound control.
The routine checks the input length and components, then uses DateSerial to create the date. It also checks the result because DateSerial can roll an invalid day into the next month.
Open the form in Design View. Add an unbound text box named txtCompactDate and a command button named cmdApplyDate. Ensure the form also has an editable text box named txtDate bound to the Date/Time field.
Select cmdApplyDate, open its Property Sheet, and on the Event tab set On Click to [Event Procedure]. Select the ellipsis button to open the VBA editor.
Replace the button's generated Click procedure with: Private Sub cmdApplyDate_Click() Dim s As String Dim y As Integer, m As Integer, d As Integer Dim result As Date On Error GoTo InvalidDate s = Trim$(Nz(Me!txtCompactDate.Value, "")) If Len(s) <> 8 Or s Like "*[!0-9]*" Then GoTo InvalidDate y = CInt(Left$(s, 4)) m = CInt(Mid$(s, 5, 2)) d = CInt(Right$(s, 2)) If y < 1000 Or m < 1 Or m > 12 Or d < 1 Or d > 31 Then GoTo InvalidDate result = DateSerial(y, m, d) If Year(result) <> y Or Month(result) <> m Or Day(result) <> d Then GoTo InvalidDate Me!txtDate.Value = result Exit Sub InvalidDate: MsgBox "Enter a valid date as YYYYMMDD, for example 20190915.", vbExclamation End Sub
Save the form and switch to Form View. Enter 20190915 in txtCompactDate and click cmdApplyDate; then try an invalid value such as 20190230 and confirm that the warning appears.

Work with Office spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface and compatibility with Microsoft Office formats for an easier transition. It does not replace Microsoft Access forms or run the Access VBA routine above.

Frequently Asked Questions
Why doesn't an input mask convert my compact date?
An input mask primarily controls the entry format; it does not guarantee that a compact value will be converted and validated as intended. Use an unbound text box and a VBA routine to parse and validate the components before assigning the date.
Can I enter 150919 with this code?
No. The example accepts eight digits in YYYYMMDD order. To support 150919, first define whether it means DDMMYY or another format and establish how two-digit years should be interpreted, then adapt the parsing and validation code.
Why does DateSerial need an additional validation check?
DateSerial can normalize out-of-range components instead of rejecting them. Comparing Year, Month, and Day of the result with the entered components catches dates such as February 30.
Can I change how the validated date appears?
Yes. In the form's Design View, select the bound date control and adjust its Format property in the Property Sheet. The display format does not change the date value stored in the field.




