I have a form with a date composed of 3 text boxes and a spinner. I used 3 text boxes to solve a problem with international dates, so there is no way the intended date can be misinterpreted by Excel based on different formats.
Anyway... When I click the spinner, the next or previous date appears in the 3 boxes. I.e. if it is Feb 28, 2013 it will go to Mar 01, 2013. It moves one day at a time.
My boss wants it to move based on which text box the cursor is in. So if it is in txtMonth, the date should go from Feb 28, 2013 to Mar 28, 2013, etc.
How can I make the spinner detect which textbox contains the cursor (if any?) Once I know that, I can do the math.
Private Sub spinDate_SpinDown()
Dim bGoodDate As Boolean
Dim iMonth As Integer
Dim dDate As Date
If Not bIsValidDate(bGoodDate) Then ' Check that the 3 boxes make a valid date
GoTo ErrorExit
End If
iMonth = Month(DateValue("01-" & Me.txtMon & "-2014"))
dDate = DateValue(Me.txtDay & "-" & Me.txtMon & "-" & Me.txtYear)
dDate = DateAdd("d", -1, dDate) ' Decrement date
Me.txtDay = Format(Day(dDate), "00")
Me.txtMon = Format(dDate, "mmm")
Me.txtYear = Format(Year(dDate), "0000")
m_clsCRE.ETA = dDate
m_clsCRE.bChanged = True
ErrorExit:
End Sub
* UPDATED WITH SOLUTION BELOW ** ' This solution detects whether the cursor was last in the day, month or year box. ' Spin button increments by the chosen amount (day, month or year) ' The boxes are linked so that spinning takes you to the next valid date based on your choice
Private Sub spinDate_SpinDown() ' Same for SpinUp but used +1
Dim bGoodDate As Boolean
Dim dDate As Date
If Not bIsValidDate(bGoodDate) Then ' check that date user typed in boxes is valid
GoTo ErrorExit
End If
dDate = DateValue(Me.txtDay & "-" & Me.txtMon & "-" & Me.txtYear)
If Len(msDatePart) = 0 Or StrComp(msDatePart, "DAY", vbTextCompare) = 0 Then
dDate = DateAdd("d", -1, dDate)
ElseIf StrComp(msDatePart, "MONTH", vbTextCompare) = 0 Then
dDate = DateAdd("m", -1, dDate)
Else ' Year
dDate = DateAdd("yyyy", -1, dDate)
End If
Me.txtDay = Format(Day(dDate), "00")
Me.txtMon = Format(dDate, "mmm")
Me.txtYear = Format(Year(dDate), "0000")
m_clsCRE.ETA = dDate ' Save real date value (not text form)
m_clsCRE.bChanged = True
ErrorExit:
End Sub
Private Sub txtDay_Enter()
' Used with calendar spinner
msDatePart = "DAY"
End Sub
Private Sub txtMon_Enter()
' Used with calendar spinner
msDatePart = "MONTH"
End Sub
Private Sub txtYear_Enter()
' Used with calendar spinner
msDatePart = "YEAR"
End Sub