none
Formatting Dates without using Mask in a textbox. RRS feed

  • Question

  • Hello everyone,

    I have a textbox on on an access form bound to a database.
    I know I can use the masked or SQL to get the date formatted that is inserted into the database.

    However, I do not want to use mask for formatting. What I really need is for a user to type the following; example: 100111

    and on Lostfocus, the date which is text will instantly become formatted to the following: 10/01/2011.

    Is this possible?


    Thanks everyone!

     

    SYN

    Saturday, October 1, 2011 9:08 AM

Answers

All replies

  • The following code get request a date in your "short" format and produce both a Date and a String for that date:

     

    Sub demo()
    Dim output1 As Date, output2 As String, input1 As Long
    input1 = Application.InputBox(Prompt:="enter value", Type:=1)
    output1 = DateSerial(2000 + Right(input1, 2), Left(input1, 2), Mid(input1, 3, 2))
    output2 = Left(input1, 2) & "/" & Mid(input1, 3, 2) & "/" & "20" & Right(input1, 2)
    MsgBox output1 & vbCrLf & output2
    End Sub


    gsnu201109
    Saturday, October 1, 2011 5:58 PM
    Moderator
  • Hi Synthologic,

     

    You might use a build-in Input Mask for your Date field, which does the trick for you:

     

    http://office.microsoft.com/en-us/access-help/control-data-entry-formats-with-input-masks-HA010096452.aspx

     

    Hope this helps,

     


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"
    Tuesday, January 31, 2012 4:10 AM
    Moderator