Microsoft access input masks code#
However, the code origins from Access 2.0, so it should be adoptable with minor changes for any version of Access. It is a simple form created in Access 2013 that should work as is for all versions of Access from 2007 to 2016.
Microsoft access input masks download#
To test it for yourself, download and run the demo application. Note the conversion of the time value as a formatted string expression, as DefaultValue is a string. Me!Logon.DefaultValue = Format(ThisTime, "\#hh:nn:00 AM/PM\#") Private Sub SetDefaultTime(ThisTime As Date) ' Rem this line out to allow the textbox to be cleared. When done, you may set a new default value: Note, that it will always leave some time value in the control. Mid(Text, 1 + 3) = CStr(TimeMinuteTenMax) ' Length of Text is larger than two hour digits and the kolon. ' BackSpace, Tab, Ctrl+Enter, Enter, Escape, Ctrl+BackSpace Private Sub Logon_KeyPress(KeyAscii As Integer)Ĭase vbKeyBack, vbKeyTab, Asc(vbLf), vbKeyReturn, vbKeyEscape, vbKeyF16 Now comes the fun part - to try to be smart, helping the user by correcting invalid values while still allowing the normal key entries for navigating inside the control as well as away from the control: ' Move the cursor off the separator (colon) Second, when clicking in the control, avoid ever having to select the separator (colon): This may be dynamic (controlled by other code), but here it is static for simplicity: That's surprisingly little, but then comes the code behind for the control.įirst, set a default time. The input mask is, as the first digit is optional:
The first and the last item are quite simple. That may seem like a lot, but the outcome is a highly optimised control demonstrating the true power of an input mask.