Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 获取以前的分期付款日期_Ms Access_Vba - Fatal编程技术网

Ms access 获取以前的分期付款日期

Ms access 获取以前的分期付款日期,ms-access,vba,Ms Access,Vba,我已经创建了一个函数,我将向它传递一个日期和一个术语,结果将是日期,这是传递的日期和术语的前一部分。以下是一些例子 Date (In) | Term (In) | Date (Out) --------------+-----------------+-------------- 22/02/2015 | Monthly | 22/03/2015 22/02/2015 | Quarterly | 22/02/2015

我已经创建了一个函数,我将向它传递一个日期和一个术语,结果将是日期,这是传递的日期和术语的前一部分。以下是一些例子

Date (In)     |    Term (In)    |    Date (Out)
--------------+-----------------+--------------
22/02/2015    |    Monthly      |    22/03/2015
22/02/2015    |    Quarterly    |    22/02/2015
01/01/2015    |    Monthly      |    01/03/2015
24/03/2015    |    Annually     |    24/03/2015
术语值可以是每月、每季度或每年。这是我目前拥有的功能,但没有给我想要的输出

Public Function getPrevInst(tmpDate As Date, tmpPeriod As String) As Date
    Dim monthDiff As Integer, modVal As Integer, tmpFuncDate As Date, tmpMonDiff As Long

    Select Case tmpPeriod
        Case "Monthly"
            modVal = 1
        Case "Quarterly"
            modVal = 3
        Case Else
            modVal = 12
    End Select

    monthDiff = DateDiff("m", tmpDate, LstDayPrevMnth(Date))

    tmpMonDiff = IIf(monthDiff > 0, monthDiff - (monthDiff Mod modVal), IIf(monthDiff < 0, 0, 1))

    tmpFuncDate = DateAdd("m", tmpMonDiff, tmpDate)

    If tmpFuncDate >= Date Then
        getPrevInst = DateAdd("m", monthDiff, tmpDate)
    Else
        getPrevInst = tmpFuncDate
    End If
End Function

Public Function LstDayPrevMnth(InDate As Date) As Date
    LstDayPrevMnth = DateSerial(Year(InDate), Month(InDate), 0)
End Function
如果你有更好的方式找到上一期也会受到欢迎。如果我的代码严重错误

外行术语

彼得向我借了一笔贷款。他计划每月偿还,他的第一笔分期付款是2015年2月22日。如果我打开他的记录,我需要查看最后一笔付款是什么时候到期/支付的。因此,今天(2015年3月24日),我将收到他的第一笔付款
2015年2月22日
2015年3月22日
(3月22日是两天前)。因此,最后一次分期付款日期为2015年3月22日

汤姆向我借了一笔贷款。他计划每月偿还一次,他的第一笔分期付款是2015年1月1日。如果我打开他的记录,我需要查看最后一笔付款是什么时候到期/支付的。因此,今天(2015年3月24日),我将收到他的第一笔付款,即2015年1月1日、2015年2月1日和2015年3月1日(因为3月1日是23天前)。因此,最后一次分期付款日期为2015年3月1日

哈利向我借了一笔贷款。他计划每季度偿还一次,他的第一笔分期付款是2015年2月22日。如果我打开他的记录,我需要查看最后一笔付款是什么时候到期/支付的。因此,今天(2015年3月24日),我将收到他的第一笔付款,即2015年3月22日(也就是说,下一笔付款要到2015年6月22日才到期,这一天还没有发生)。因此,最后一次分期付款日期为2015年3月22日


注意:交叉发布在:

这是一个比你想象的更敏感的问题,因为一个月不是“一个月”。 但是,使用下面的月函数,您可以精确计算日期:

DateLast = DateAdd("m", Months(DateFirst, Date), DateFirst)
DateLast = DateAdd("m", (Months(DateFirst, Date) \ 3) * 3, DateFirst)
DateLast = DateAdd("m", (Months(DateFirst, Date) \ 12) * 12, DateFirst)
注意整数除以\

Public Function Months( _
  ByVal datDate1 As Date, _
  ByVal datDate2 As Date, _
  Optional ByVal booLinear As Boolean) _
  As Integer

' Returns the difference in full months between datDate1 and datDate2.
'
' Calculates correctly for:
'   negative differences
'   leap years
'   dates of 29. February
'   date/time values with embedded time values
'   negative date/time values (prior to 1899-12-29)
'
' Optionally returns negative counts rounded down to provide a
' linear sequence of month counts.
' For a given datDate1, if datDate2 is decreased stepwise one month from
' returning a positive count to returning a negative count, one or two
' occurrences of count zero will be returned.
' If booLinear is False, the sequence will be:
'   3, 2, 1, 0,  0, -1, -2
' If booLinear is True, the sequence will be:
'   3, 2, 1, 0, -1, -2, -3
'
' If booLinear is False, reversing datDate1 and datDate2 will return
' results of same absolute Value, only the sign will change.
' This behaviour mimics that of Fix().
' If booLinear is True, reversing datDate1 and datDate2 will return
' results where the negative count is offset by -1.
' This behaviour mimics that of Int().

' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of months to dates of Feb. 29.
' when the resulting year is a common year.
'
' 2010-03-30. Cactus Data ApS, CPH.

  Dim intDiff   As Integer
  Dim intSign   As Integer
  Dim intMonths As Integer

  ' Find difference in calendar months.
  intMonths = DateDiff("m", datDate1, datDate2)
  ' For positive resp. negative intervals, check if the second date
  ' falls before, on, or after the crossing date for a 1 month period
  ' while at the same time correcting for February 29. of leap years.
  If DateDiff("d", datDate1, datDate2) > 0 Then
    intSign = Sgn(DateDiff("d", DateAdd("m", intMonths, datDate1), datDate2))
    intDiff = Abs(intSign < 0)
  Else
    intSign = Sgn(DateDiff("d", DateAdd("m", -intMonths, datDate2), datDate1))
    If intSign <> 0 Then
      ' Offset negative count of months to continuous sequence if requested.
      intDiff = Abs(booLinear)
    End If
    intDiff = intDiff - Abs(intSign < 0)
  End If

  ' Return count of months as count of full 1 month periods.
  Months = intMonths - intDiff

End Function
公共职能月(_
ByVal DatDate 1作为日期_
ByVal DatDate 2作为日期_
可选的ByVal booLinear(作为布尔值)_
作为整数
'返回datDate1和datDate2之间的整月差值。
'
'正确计算:
"负差异",
“闰年
"29日。二月
'带有嵌入时间值的日期/时间值
'负日期/时间值(1899-12-29之前)
'
'可选地返回向下舍入的负计数,以提供
'月份计数的线性序列。
'对于给定的datDate1,如果datDate2从
'返回正计数到返回负计数,一个或两个
'将返回计数为零的事件。
'如果booLinear为False,则序列为:
'   3, 2, 1, 0,  0, -1, -2
'如果booLinear为真,则序列将为:
'   3, 2, 1, 0, -1, -2, -3
'
'如果booLinear为False,则将返回反向的datDate1和datDate2
'相同绝对值的结果,只有符号会改变。
'此行为模仿Fix()。
'如果booLinear为True,则将返回反向的datDate1和datDate2
'负计数被-1偏移的结果。
'此行为模仿Int()的行为。
'DateAdd()用于检查2月底,因为它正确
2月28日返回。将月份数添加到2月29日的日期时。
“当产生的年份是普通年份时。
'
' 2010-03-30. 仙人掌数据ApS,CPH。
Dim intDiff作为整数
整数形式的整数符号
将整数设为整数
'查找日历月份的差异。
intMonths=DateDiff(“m”,datDate1,datDate2)
“积极响应。负间隔,检查第二个日期
'在交叉日期之前、当天或之后持续1个月
同时对2月29日进行修正。闰年。
如果DateDiff(“d”,datDate1,datDate2)>0,则
intSign=Sgn(日期差(“d”,日期加(“m”,INTMOUNTERS,DATDATATE1),DATDATATE2))
intDiff=Abs(intSign<0)
其他的
intSign=Sgn(日期差(“d”,日期加(“m”,整数月,日期2),日期1))
如果输入0,那么
'如果需要,将月份的负数偏移为连续序列。
intDiff=Abs(booLinear)
如果结束
intDiff=intDiff-Abs(intSign<0)
如果结束
'返回月份计数作为完整1个月期间的计数。
月份=intMonths-intDiff
端函数
Public Function Months( _
  ByVal datDate1 As Date, _
  ByVal datDate2 As Date, _
  Optional ByVal booLinear As Boolean) _
  As Integer

' Returns the difference in full months between datDate1 and datDate2.
'
' Calculates correctly for:
'   negative differences
'   leap years
'   dates of 29. February
'   date/time values with embedded time values
'   negative date/time values (prior to 1899-12-29)
'
' Optionally returns negative counts rounded down to provide a
' linear sequence of month counts.
' For a given datDate1, if datDate2 is decreased stepwise one month from
' returning a positive count to returning a negative count, one or two
' occurrences of count zero will be returned.
' If booLinear is False, the sequence will be:
'   3, 2, 1, 0,  0, -1, -2
' If booLinear is True, the sequence will be:
'   3, 2, 1, 0, -1, -2, -3
'
' If booLinear is False, reversing datDate1 and datDate2 will return
' results of same absolute Value, only the sign will change.
' This behaviour mimics that of Fix().
' If booLinear is True, reversing datDate1 and datDate2 will return
' results where the negative count is offset by -1.
' This behaviour mimics that of Int().

' DateAdd() is used for check for month end of February as it correctly
' returns Feb. 28. when adding a count of months to dates of Feb. 29.
' when the resulting year is a common year.
'
' 2010-03-30. Cactus Data ApS, CPH.

  Dim intDiff   As Integer
  Dim intSign   As Integer
  Dim intMonths As Integer

  ' Find difference in calendar months.
  intMonths = DateDiff("m", datDate1, datDate2)
  ' For positive resp. negative intervals, check if the second date
  ' falls before, on, or after the crossing date for a 1 month period
  ' while at the same time correcting for February 29. of leap years.
  If DateDiff("d", datDate1, datDate2) > 0 Then
    intSign = Sgn(DateDiff("d", DateAdd("m", intMonths, datDate1), datDate2))
    intDiff = Abs(intSign < 0)
  Else
    intSign = Sgn(DateDiff("d", DateAdd("m", -intMonths, datDate2), datDate1))
    If intSign <> 0 Then
      ' Offset negative count of months to continuous sequence if requested.
      intDiff = Abs(booLinear)
    End If
    intDiff = intDiff - Abs(intSign < 0)
  End If

  ' Return count of months as count of full 1 month periods.
  Months = intMonths - intDiff

End Function