首先讓我說我對 VBA 和一般的編碼都是新手。我的公司運行一個 Access 資料庫來跟蹤我們的大約 500 個存盤客戶和批次。每個批次都有一個支付日期、到期日期和與之相關的到期日期,每月(通常)更新一次。
我們過去只是在提取批次后使用日期選擇器手動選擇每個日期。但是,我創建了一個運行以下命令的按鈕:
Private Sub Extend_One_Month_Click()
Me.[Date Paid Actual] = Date
Me.[Date_Paid] = DateAdd("m", 1, [Date_Paid])
Me.[Date_Paid_Expiry] = DateAdd("m", 1, [Date_Paid_Expiry])
Me.Recalc
End Sub
在大多數情況下,這很好用。我的問題是它不保留任何月末資訊。因此,如果客戶通常的截止日期是每個月的 29 日(或更晚),而我在 2 月使用此功能,則他們的截止日期將變為每個月的 28 日(無論如何在 Access 上)。是否有任何功能可以保留此資訊?
謝謝!
uj5u.com熱心網友回復:
我的功能DateAddMonth正是這樣做的:
' Returns a date added a number of months.
' Result date is identical to the result of DateAdd("m", Number, Date1)
' except that if an ultimo date of a month is passed, an ultimo date will
' always be returned as well.
' Optionally, days of the 30th (or 28th of February of leap years) will
' also be regarded as ultimo.
'
' Examples:
' 2020-02-28, 1, False -> 2020-03-28
' 2020-02-28, 1, True -> 2020-03-31
' 2020-02-28,-2, False -> 2019-12-28
' 2020-02-28,-2, True -> 2019-12-31
' 2020-02-28, 4, False -> 2020-06-28
' 2020-02-28, 4, True -> 2020-06-30
' 2020-02-29, 4, False -> 2020-06-30
' 2020-06-30, 2, False -> 2020-08-31
' 2020-06-30, 2, True -> 2020-08-31
' 2020-07-30, 2, False -> 2020-08-30
' 2020-07-30, 1, True -> 2020-08-31
'
' 2015-11-30. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateAddMonth( _
ByVal Date1 As Date, _
Number As Double, _
Optional Include2830 As Boolean) _
As Date
Dim DateNext As Date
' Add number of months the normal way.
DateNext = DateAdd("m", Number, Date1)
If Day(Date1) = MaxDayValue Then
' Resulting day will be ultimo of the month.
ElseIf IsDateUltimoMonth(Date1, Include2830) = True Then
' Months are added to month ultimo.
' Adjust resulting day to be ultimo if not.
If IsDateUltimoMonth(DateNext, False) = False Then
' Resulting day is not ultimo of the month.
' Set resulting day to ultimo of the month.
DateNext = DateThisMonthUltimo(DateNext)
End If
End If
DateAddMonth = DateNext
End Function
它使用了幾個輔助函式和常量——太多了,無法在此處發布——它們都可以在我的GitHub專案中找到:
VBA.日期
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/535582.html
標籤:ms-access
