我试图创建一个循环(其中包括)插入日期,然后在每一行中添加一个月到该日期。它没有正确附加第一个日期。日期是查询中日期字段的DLookup,所以我认为它应该作为日期工作。而且我的SQL语句没有任何问题。但是当这个运行时,日期会在表格中显示为12/30/1899,如果你点击它,它会变为12:03:34 AM。它应该是5/1/15。我没有试图让这个工作起来给我带来任何其他结果。
这是我的代码,请注意:我的整体代码可能还有其他一些问题我确定,但我现在关注此日期问题。不过,请随意指出你发现的一切。
Private Sub AmortButton_Click()
DoCmd.SetWarnings False
Dim Account As Long: Account = DLookup("[L#]", "qry_info4amort") 'working
Dim StartDate As Date: StartDate = CDate(DLookup("PaidToDate", "qry_info4amort")) 'NOT WORKING
Dim InterestRate As Double: InterestRate = DLookup("IntCur", "qry_info4amort") 'working
Dim piConstant As Integer: piConstant = DLookup("[P&IConstant]", "qry_info4amort")
Dim UPB As Currency: UPB = DLookup("UPB", "qry_info4amort") 'working
Dim tempUPB As Currency: tempUPB = UPB 'working (just to establish variable)
Dim AmortInterest As Currency: AmortInterest = 0 'working (just to establish variable)
Dim AmortPrincipal As Currency: AmortPrincipal = 0 'working (just to establish variable)
Dim Ranking As Integer: Ranking = 1 'working (just to establish variable)
Dim PaymentLoop As Integer: PaymentLoop = 1 'working (just to establish variable)
Dim PaymentNumber As Integer: PaymentNumber = DLookup("NumPmts", "qry_info4amort") 'working
Dim tempInterest As Integer: tempInterest = 0 'working (just to establish variable)
Do While PaymentLoop <= PaymentNumber 'working
tempInterest = Round(tempUPB * (InterestRate / 12), 2)
tempUPB = Round(tempUPB - (piConstant - tempInterest), 2)
AmortPrincipal = AmortPrincipal + (piConstant - tempInterest)
AmortPrincipal = (piConstant - tempInterest)
AmortInterest = AmortInterest + tempInterest
DoCmd.RunSQL "INSERT INTO tbl_AmortizationTest ([L#],[Payment#],[PaymentDate],[UPB],[Interest],[Principal],[TotalPayment],[InterestRate],[TempUPB],[Ranking]) " & _
"VALUES (" & Account & "," & PaymentLoop & "," & StartDate & "," & UPB & "," & tempInterest & "," & AmortPrincipal & "," & (tempInterest + AmortPrincipal) & "," & InterestRate & "," & tempUPB & "," & Ranking & ")"
UPB = tempUPB
StartDate = DateAdd("m", 1, StartDate) 'NOT WORKING
PaymentLoop = PaymentLoop + 1 'working
Ranking = Ranking + 1 'working
Loop
MsgBox "Done!"
DoCmd.SetWarnings True
End Sub
答案 0 :(得分:1)
首先,如果PaidToDate
的数据类型为Date,请按原样检索日期:
StartDate = DLookup("PaidToDate", "qry_info4amort")
如果是字符串,则CDate或DateValue将执行:
StartDate = DateValue(DLookup("PaidToDate", "qry_info4amort"))
其次,在将其连接到SQL时为日期格式化正确的字符串表达式:
... PaymentLoop & ",#" & Format(StartDate, "yyyy\/mm\/dd") & "#," & UPB ...
答案 1 :(得分:0)
而不是在CDate()
周围使用DLookup("PaidToDate", "qry_info4amort")
,你可能不得不逐件拔出相关部分......
StartDate = DateSerial(<year>, <month>, <day>)
例如,如果DLookup("PaidToDate", "qry_info4amort")
返回05012015
,那么您可以执行以下操作:
StartDate = DateSerial(Mid(DLookup("PaidToDate", "qry_info4amort"),5,4), Left(DLookup("PaidToDate", "qry_info4amort"),2), Mid(DLookup("PaidToDate", "qry_info4amort"),3,2))
如果DLookup("PaidToDate", "qry_info4amort")
返回包含/
的值,那么您必须使用一些Instr()
函数... More on that here。