Formatting MM/DD/YYYY dates in textbox in VBA

前端 未结 9 1667
故里飘歌
故里飘歌 2020-11-22 10:47

I\'m looking for a way to automatically format the date in a VBA text box to a MM/DD/YYYY format, and I want it to format as the user is typing it in. For instance, once the

9条回答
  •  感情败类
    2020-11-22 11:05

    I never suggest using Textboxes or Inputboxes to accept dates. So many things can go wrong. I cannot even suggest using the Calendar Control or the Date Picker as for that you need to register the mscal.ocx or mscomct2.ocx and that is very painful as they are not freely distributable files.

    Here is what I recommend. You can use this custom made calendar to accept dates from the user

    PROS:

    1. You don't have to worry about user inputting wrong info
    2. You don't have to worry user pasting in the textbox
    3. You don't have to worry about writing any major code
    4. Attractive GUI
    5. Can be easily incorporated in your application
    6. Doesn't use any controls for which you need to reference any libraries like mscal.ocx or mscomct2.ocx

    CONS:

    Ummm...Ummm... Can't think of any...

    HOW TO USE IT (File missing from my dropbox. Please refer to the bottom of the post for an upgraded version of the calendar)

    1. Download the Userform1.frm and Userform1.frx from here.
    2. In your VBA, simply import Userform1.frm as shown in the image below.

    Importing the form

    enter image description here

    RUNNING IT

    You can call it in any procedure. For example

    Sub Sample()
        UserForm1.Show
    End Sub
    

    SCREEN SHOTS IN ACTION

    enter image description here

    NOTE: You may also want to see Taking Calendar to new level

提交回复
热议问题