Intelligent date Input Method without date control (VB Code Edition)

Source: Internet
Author: User
Tags exit
Control 1, in the text box only accept the 10 characters "0-9". And the month-day separator is automatically generated.
2, VB Year is 100-9999, I limited to 1000-2999, not enough? If it's not enough, you can change it to control it yourself. You might ask: Why don't you have two digits for the year?

My point of view:

1, two-digit years format is not intuitive.

2, if you encounter to record the date of birth, it is likely to be very difficult.

3, the month only from 01-12, and, when the input 3-9, the system will automatically default to 03-09.

4, date if you enter 4-9, the system will automatically default to 04-09. And also:

A: When the month is 1, 3, 5, 7, 8, 10, 12 o'clock, the date cannot exceed 31

B: When the month is 4, 6, 9, 11 o'clock, the date can not exceed 30

C: When the month is 2 o'clock and is a leap year, the date cannot exceed 29

D: When the month is 2 o'clock and is not a leap year, the date cannot exceed 28

With the above "benefits", as long as you enter the end of the month, you will ensure that it is a valid date expression. Friends, quickly paste the following program into the VB project (of course, you may want to change Text1 for other text box number such as: TEXT3) to try it, I believe that you will immediately love her, until forever ...

Private Sub Text1_change ()

Dim A, B, C as String
---------------------------------------------------------------------------
Control of year input
---------------------------------------------------------------------------
Limit first must be "1" or "2", also say the year must be between 1000-2999, enough!

If Len (Text1.Text) = 1 Then

If Left ((Text1.Text), 1) <> "1" and Left ((Text1.Text), 1) <> "2" Then

Text1.Text = ""

End If

End If
Limit 第二、三、四位 must be "1, 2, 3, 4, 5, 6, 7, 8, 9, 0"

If Len (Text1.Text) = 2 or len (text1.text) = 3 Or len (text1.text) = 4 Then

If Right ((Text1.Text), 1) <> "0" and Right ((Text1.Text), 1) <> "1" and _

Right ((Text1.Text), 1) <> "2" and Right ((Text1.Text), 1) <> "3" and _

Right ((Text1.Text), 1) <> "4" and Right ((Text1.Text), 1) <> "5" and _

Right ((Text1.Text), 1) <> "6" and Right ((Text1.Text), 1) <> "7" and _

Right ((Text1.Text), 1) <> "8" and Right ((Text1.Text), 1) <> "9" Then

Text1.Text = Left ((text1.text), Len (Text1.Text)-1)

Text1.selstart = Len (Text1.Text)

End If

End If

If Len (Text1.Text) = 4 Then

Text1.Text = Text1.Text + "-"

Text1.selstart = Len (Text1.Text)

End If The "-" delimiter is automatically added after the correct input
---------------------------------------------------------------------------
Control of Month input
---------------------------------------------------------------------------

If Len (Text1.Text) = 6 Then

If Right ((Text1.Text), 1) <> "0" and Right ((Text1.Text), 1) <> "1" Then

If Right ((Text1.Text), 1) = "2" or Right ((Text1.Text), 1) = "3" or _

Right ((Text1.Text), 1) = "4" or Right ((Text1.Text), 1) = "5" or _

Right ((Text1.Text), 1) = "6" or Right ((Text1.Text), 1) = "7" or _

Right ((Text1.Text), 1) = "8" Or Right ((Text1.Text), 1) = "9" Then

A = Right ((text1.text), 1)

Text1.Text = Left ((text1.text), 5) + "0" + A + "-"

If so, the following section of If Len (Text1.Text) =7 's judgment automatically jumps past.

Text1.selstart = Len (Text1.Text)

Else limit can only enter "0-9"

Text1.Text = Left ((text1.text), Len (Text1.Text)-1)

Text1.selstart = Len (Text1.Text)

End If

End If

End If

If Len (Text1.Text) = 7 Then

If left (right (Text1.Text, 2), 1) = "0" Then if the first digit of the month is "0"

If Right ((Text1.Text), 1) <> "1" and Right ((Text1.Text), 1) <> "2" and _

Right ((Text1.Text), 1) <> "3" and Right ((Text1.Text), 1) <> "4" and _

Right ((Text1.Text), 1) <> "5" and Right ((Text1.Text), 1) <> "6" and _

Right ((Text1.Text), 1) <> "7" and Right ((Text1.Text), 1) <> "8" and _

Right ((Text1.Text), 1) <> "9" Then

Text1.Text = Left ((text1.text), Len (Text1.Text)-1)

Text1.selstart = Len (Text1.Text)

Else

Text1.Text = Text1.Text + "-" Automatically add a "-" separator after the current month is entered correctly

Text1.selstart = Len (Text1.Text)

The Exit Sub is indispensable! If less, the month is "01", immediately if ... End If is

Set up, this will appear in the dead loop here, and overflow stack space error!
Note: This procedure can be used in several places on the exit Sub, to add your own to fill it!

End If

End If

If left ((Text1.Text), 2, 1) = "1" Then if the first digit of the month is "1"

If Right ((Text1.Text), 1) <> "0" and Right ((Text1.Text), 1) <> "1" and _

Right ((Text1.Text), 1) <> "2" Then

Text1.Text = Left ((text1.text), Len (Text1.Text)-1)

Text1.selstart = Len (Text1.Text)

Else

Text1.Text = Text1.Text + "-" Automatically add a "-" separator after the current month is entered correctly

Text1.selstart = Len (Text1.Text)

End If

End If

End If
---------------------------------------------------------------------------
Control of date input.
---------------------------------------------------------------------------

If Len (text1.text) = 9 Then

If Right ((Text1.Text), 1) <> "0" and Right ((Text1.Text), 1) <> "1" and _

Right ((Text1.Text), 1) <> "2" and Right ((Text1.Text), 1) <> "3" Then
If Right ((Text1.Text), 1) = "4" or Right ((Text1.Text), 1) = "5" or _
Right ((Text1.Text), 1) = "6" or Right ((Text1.Text), 1) = "7" or _
Right ((Text1.Text), 1) = "8" Or Right ((Text1.Text), 1) = "9" Then
A = Right ((text1.text), 1)
Text1.Text = Left ((text1.text), 8) + "0" + A
Text1.selstart = Len (Text1.Text)
Exit Sub
When the date is less than 10 o'clock the following character length is 10 of course the judgment is correct. What if we let it execute?
Else
Text1.Text = Left ((text1.text), Len (Text1.Text)-1)
Text1.selstart = Len (Text1.Text)
End If
End If
End If
control when you want to modify the last time of the date.
If Len (Text1.Text) = Ten Then
B = Left (right (Text1.Text, 5), 2) takes the month value for the following date correctness judgment!
c = Left (Text1.Text, 4) takes the year value for the following date correctness judgment!
If Right ((Text1.Text), 1) <> "0" and Right ((Text1.Text), 1) <> "1" and _
Right ((Text1.Text), 1) <> "2" and Right ((Text1.Text), 1) <> "3" and _
Right ((Text1.Text), 1) <> "4" and Right ((Text1.Text), 1) <> "5" and _
Right ((Text1.Text), 1) <> "6" and Right ((Text1.Text), 1) <> "7" and _
Right ((Text1.Text), 1) <> "8" and Right ((Text1.Text), 1) <> "9" Then
Text1.Text = Left ((text1.text), Len (Text1.Text)-1)
Text1.selstart = Len (Text1.Text)
End If restricts the entry of illegal characters.
If Right (text1.text, 2) = "Then"
Text1.Text = Left ((text1.text), Len (Text1.Text)-2)
Text1.selstart = Len (Text1.Text)
End If Constraint date cannot be 0
If (b = "" "and Val (Right (Text1.Text, 2)) >) Or _
(b = "2" and Val (right (Text1.Text,)) > 31) Or _
(b = "A" and Val (Right (Text1.Text, 2)) > 31) Or _
(b = "Modified" and Val (Right (Text1.Text, 2)) > 31) Or _
(b = "2" and Val (right (Text1.Text,)) > 31) Or _
(b = "Ten" and Val (Right (Text1.Text, 2)) > 31) Or _
(b = "2" and Val (right (Text1.Text,)) > 31) Then
Text1.Text = Left ((text1.text), Len (Text1.Text)-2)
Text1.selstart = Len (Text1.Text)
End If the month is a large month, the date cannot be greater than 31.
If (b = "the" and Val (Right (Text1.Text, 2)) > Or _
(b = "2" and Val (right (Text1.Text,)) > 30) Or _
(b = "A" and Val (Right (Text1.Text, 2)) > 30) Or _
(b = "One" and Val (Right (Text1.Text, 2)) > 30) Then
Text1.Text = Left ((text1.text), Len (Text1.Text)-2)
Text1.selstart = Len (Text1.Text)
End If the month is a small month, the date cannot be greater than 30.
If B = "Then"
If Val (c) Mod 4 <> 0 and Val (right (Text1.Text, 2)) > Then
Text1.Text = Left ((text1.text), Len (Text1.Text)-2)
Text1.selstart = Len (Text1.Text)
The end If non-leap year date must not exceed 28.
If Val (c) Mod 4 = 0 and Val (right (Text1.Text, 2)) > Then
Text1.Text = Left ((text1.text), Len (Text1.Text)-2)
Text1.selstart = Len (Text1.Text)
The end If leap year date must not exceed 29.
End If The date of the month for 2 o'clock correctness judgment!
End If
---------------------------------------------------------------------------
When the date is entered, the other characters are no longer accepted. The method is as follows:
---------------------------------------------------------------------------
The first method:
In the TEXT1 Properties window, set maxlength=10
The second method:
Text2.setfocus the next object with the focus by setting a jump statement where appropriate.
The third method:
If Len (Text1.Text) = one Then
Text1.Text = Left ((text1.text), Len (Text1.Text)-1)
Text1.selstart = Len (Text1.Text)
End If
End Sub




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.