date作为ms访问中无法识别的字段名称

时间:2015-07-13 13:37:25

标签: sql date ms-access access-vba field

我有字段,其名称是交货日期,您可以在此处看到: extract of the table 我有大约75列这样的。当我想计算值P1时,我有一个错误,因为它找不到该字段。顺便说一句,我的系数T1,T2(日期)以及A和B都是日期值非常奇怪。有代码:

 Sub vcm_fc()
    Dim db As Database, T As Date, longest As Integer, nearest_date As Date, rsb As DAO.Recordset, strsqlb As String
    Dim strsqla As String, rsa As Recordset, maturity As Date, T1 As Date, T2 As Date, P1 As Double, P2 As Double
    Dim a As Integer, B As Integer, j As Integer, rsc As DAO.Recordset, strqlc As String, settlementbis As String
    Dim settlement As String, maturitybis As Date, ym As Integer, ymbis As Integer
        Set db = CurrentDb()
        T = DateSerial(2020, 8, 15)
        nearest_date = DFirst("PricingDate", "fc_historical")
        longest = DateDiff("m", nearest_date, T)
         db.Execute "CREATE TABLE time_series " _
                    & "(PricingDate CHAR);"
        db.Execute " INSERT INTO time_series " _
            & "SELECT PricingDate " _
            & "FROM fc_historical " _
            & "ORDER BY PricingDate;"
               For i = 1 To longest
                db.Execute " ALTER TABLE time_series " _
                    & "ADD COLUMN F_" & i & " Number;"
                 strsqla = "SELECT PricingDate, F_" & i & " FROM time_series ORDER BY PricingDate"
                 Set rsa = db.OpenRecordset(strsqla, dbOpenDynaset)
                 rsa.MoveFirst
                 rsa.Delete 'delete the first row which is blank when the time series table is created'
                 rsa.MoveFirst
                 While (Not rsa.EOF())
                 rsa.Edit
                 maturity = DateAdd("m", i, rsa.Fields("PricingDate").Value)
                 ym = Year(maturity) - 2000
                 settlement = "1/" & Month(maturity) & "/" & ym
                 strsqlb = "SELECT Pricingdate, " & settlement & " FROM fc_historical ORDER BY PricingDate;"
                 Set rsb = db.OpenRecordset(strsqlb, dbOpenDynaset)
                 rsb.MoveLast
                 T1 = rsb.Fields("PricingDate").Value
                 maturitybis = DateAdd("m", i, maturity)
                 ymbis = Year(maturitybis) - 2000
                 settlementbis = "1/" & Month(maturitybis) & "/" & ymbis
                 strsqlc = "SELECT Pricingdate, " & settlementbis & " FROM fc_historical ORDER BY PricingDate;"
                 Set rsc = db.OpenRecordset(strsqlc, dbOpenDynaset)
                 rsc.MoveLast
                 T2 = rsc.Fields("PricingDate").Value
                 a = DateDiff("d", T1, rsa.Fields("PricingDate").Value)
                 B = DateDiff("d", rsa.Fields("PricingDate").Value, T2)
                 P1 = rsb.Fields(settlement).Value
                 P2 = rsc.Fields(settlementbis).Value
                 rsa.Fields("F_" & i) = (P1 * B + P2 * a) / (a + B)
                 rsa.Update
                 rsa.MoveNext
                 Wend
        Next i
End Sub

2 个答案:

答案 0 :(得分:1)

添加了答案,以便我可以获取代码的格式。此代码将获取您的日期字段名称并将它们作为记录值放置 - 您可能需要使用fld.Name来确保它不是美国日期格式(1月8日,1月9日等,用于您的表示例) )以及该日期的价格。

Sub Put_Field_Names_As_Record_Values()

    Dim DB As DAO.Database
    Dim OldTable As DAO.TableDef
    Dim fld As DAO.Field

    Set DB = CurrentDb()
    Set OldTable = DB.TableDefs("time_series")

    DoCmd.SetWarnings False
    For Each fld In OldTable.Fields
        If IsDate(fld.Name) Then
            Debug.Print fld.Name & " : " & SQLDate(fld.Name)
            DoCmd.RunSQL "INSERT INTO MyNewTable (PricingDate, SettlementDate, Price) " & _
                "SELECT PricingDate," & SQLDate(fld.Name) & ", [" & fld.Name & "] FROM time_series"
        End If
    Next fld
    DoCmd.SetWarnings True

End Sub

Function SQLDate(varDate As Variant) As String
    'Purpose:    Return a delimited string in the date format used natively by JET SQL.
    'Argument:   A date/time value.
    'Note:       Returns just the date format if the argument has no time component,
    '                or a date/time format if it does.
    'Author:     Allen Browne. allen@allenbrowne.com, June 2006.
    If IsDate(varDate) Then
        If DateValue(varDate) = varDate Then
            SQLDate = Format$(varDate, "\#mm\/dd\/yyyy\#")
        Else
            SQLDate = Format$(varDate, "\#mm\/dd\/yyyy hh\:nn\:ss\#")
        End If
    End If
End Function

然后您可以在桌面上运行查询:

SELECT  SUM(Price) 
FROM    MyNewTable 
WHERE   PricingDate<=#07/16/2014# AND SettlementDate=#01/08/2014#

编辑:测试您的数据库副本!!
编辑2:手动创建MyNewTable并将PricingDate和SettlementDate设置为关键字段。

我已更新,将字段名称转换为正确的日期 - http://allenbrowne.com/ser-36.html
在我的测试中,它正确地将所有日期转换为每个月的第一天。

答案 1 :(得分:-1)

使用格式正确的日期表达式:

settlementbis = "#" & CStr(ymbis) & "/" & CStr(Month(maturitybis)) & "/1#"

请听听Darren关于正常化的事情。

相关问题