Skip to main content
Code Review

Return to Answer

added 284 characters in body
Source Link
TinMan
  • 4.3k
  • 1
  • 13
  • 25
(削除) The year is a leap year if you can divide it evenly by 4 with no remainder.
Private Function IsLeapYear(ByVal Value As Long) As Boolean
 IsLeapYear = Value Mod 4 = 0
End Function
(削除ここまで)

The year is aNote: There are some conditions for leap year if you can divide it evenly by 4 with no remainderthat I did not meet in my original code. However, my IsValidDate() replacement is correct. I simply let the VBA for me.

 Val(MonthValue) = Month(DateSerial(Val(YearValue), Val(MonthValue), Val(DayValue)))
Private Function IsLeapYear(ByVal Value As Long) As Boolean
 IsLeapYear = Value Mod 4 = 0
End Function

The year is a leap year if you can divide it evenly by 4 with no remainder.

Private Function IsLeapYear(ByVal Value As Long) As Boolean
 IsLeapYear = Value Mod 4 = 0
End Function
(削除) The year is a leap year if you can divide it evenly by 4 with no remainder.
Private Function IsLeapYear(ByVal Value As Long) As Boolean
 IsLeapYear = Value Mod 4 = 0
End Function
(削除ここまで)

Note: There are some conditions for leap year that I did not meet in my original code. However, my IsValidDate() replacement is correct. I simply let the VBA for me.

 Val(MonthValue) = Month(DateSerial(Val(YearValue), Val(MonthValue), Val(DayValue)))
Source Link
TinMan
  • 4.3k
  • 1
  • 13
  • 25

Constants

 Private Const DELETE_KEY As Integer = 46
 Private Const BACKSPACE_KEY As Integer = 8

Obviously these constants refer to to KeyCodes, right? Well yeah but I still had to check. I would prefer to use the built in constants "vbKeyDelete" & "vbKeyBack" or "KeyCodeConstants.vbKeyDelete" & "KeyCodeConstants.vbKeyBack".

If you want to use your own names, I would have then refer to the built-in constants.

 Private Const DELETE_KEY As Integer = KeyCodeConstants.vbKeyDelete '46
 Private Const BACKSPACE_KEY As Integer = KeyCodeConstants.vbKeyBack '8

IsLeapYear: Function

This function does not work properly.

Private Function IsLeapYear(ByVal test As Long) As Boolean
 Select Case True
 Case test Mod 400
 IsLeapYear = True
 Case test Mod 100
 Case test Mod 4
 IsLeapYear = True
 End Select
End Function

The year is a leap year if you can divide it evenly by 4 with no remainder.

Private Function IsLeapYear(ByVal Value As Long) As Boolean
 IsLeapYear = Value Mod 4 = 0
End Function

IsValidDate: Property

The Select Case seems designed to exit the property if any condition evaluates as False. The intent is to prevent the property from returning True if the date parts are not the appropriate lengths. This does not work as intended.

Select Case False
 Case Len(YearValue) <> IIf(this.TwoDigitYear, 2, 4)
 Case Len(DayValue) <> 2
 Case Len(MonthValue) <> 2
 Case Else
 Exit Property
End Select

Let's substitute these values and break down the logic case by case:

this.TwoDigitYear = True
YearValue = "18"
DayValue = "01"
MonthValue = "01"

Case 1:

Case Len(YearValue) <> IIf(this.TwoDigitYear, 2, 4)
Case Len("18") <> IIf(True, 2, 4)
Case 2 <> 2
Case False

Since Case 1 evaluates to False the Select Case breaks here skipping the Case Else: Exit Property.

Case 2:

Case Len(DayValue) <> 2
Case Len("01") <> 2
Case 2 <> 2

Case 3:

Case Len(MonthValue) <> 2
Case Len("01") <> 2
Case 2 <> 2

Notice that cases 2 & 3 also evaluate to False; preventing the property from exiting due to improper input.

Both the IsLeapYear() and IsValidDate() can be replaced by ↓this code↓:

Public Function IsValidDate() As Boolean
 IsValidDate = Len(YearValue) = IIf(TwoDigitYear, 2, 4) And _
 Len(DayValue) = 2 And _
 Len(MonthValue) = 2 And _
 Val(MonthValue) = Month(DateSerial(Val(YearValue), Val(MonthValue), Val(DayValue)))
End Function

Order: Property

Changing the date order or delimiter should cause a value update.

This code sample will return an invalid date because the date was set using the default date order is DateOrder.YMD.

With dateInput
 Set .Wrapping = TextBox1
 .Delimiter = "."
 .DateValue = Date
 .Order = DateOrder.MDY
End With

User Experience (UX)

Being able to add dates without using a delimiter is super helpful. If you are used to doing it. Most people are not used to it. You could greatly improve the UX by converting the date parts and delimiter to the correct format and allow delimiters KeyCodes.

As a user I would like to be able to type 2018年01月31日, 2018年1月31日, 2018年01月31日 or 18-01-31 and have the code automatically correct the formats and delimiters.

Just to be consistent with most of my posts, I am going to make a totally ridiculous suggestion. Add a placeholder. Wouldn't it be great to have the empty textbox display it's date format?

lang-vb

AltStyle によって変換されたページ (->オリジナル) /