2015-07-13 163 views
0

我有字段,其中有作爲交貨日期的名稱,您可以在這裏看到: 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 
+3

我不會那樣設計表格 - 看起來完全反對關係數據庫理論和規範化。取而代之的是3列 - PricingDate,OldFieldHeadingDate,Value。然後,您的表可以從表中SELECT Value FROM WHERE PricingDate =#somedate#AND OldFieldHeadingDate =#somedate#。您將在PricingDate中出現多個相同日期 - 也許可以使用PricingDate和OldFieldHeadingDate作爲主鍵。數據庫規則1 - 它不是電子表格。 :) –

+0

領域是兩週前(周圍)的結算日期和交易止損。然後這個想法是在交易停止之前(月份)爲所有不同時期獲得一個新的時間表。對於每個日期,新價格是在最近的交易日期+交易止損前的期間(我們保持固定)的加權平均價格。此外,我不只有3列,而是75列。然而,如果我明白了你的想法,我可以爲結算日期創建一個索引,然後我可以計算新的價格,不是嗎? –

+1

是的,索引將是第一個和第二個字段的組合 - 不需要75個字段,只需要三個字段。如果計算價格,您可以輕鬆編寫查詢以處理同一個表中較早的PricingDate/SettlementDate(?)記錄。 –

回答

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. [email protected], 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
在我的測試中,它將所有日期正確地轉換爲每月的第一個日期。

+0

所有日期均採用歐洲格式,因此該列在8月1日和9月1日開始。所有的定居點都是每個月的第一個。 –

+0

對於日期格式,我用下面的代碼解決了我的pb:''SELECT PricingDate,#「&DateSerial(Year(fld.Name),Day(fld.Name),Month(fld.Name))& #,[「&fld.Name&」] FROM fc_historical WHERE [「&fld.Name&」] BETWEEN 0和10000000000000 ORDER BY PricingDate「 –

-1

使用正確格式化的日期表達式:

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

,做聽達倫約正常化。

+0

我按照[這裏]的說明嘗試過(http://pauls-learningcurve.blogspot.co.uk/2013/12/ms-access-functions-domain-functions.html),但該錯誤依然存在。 –

+0

對不起,我想你是創建帶有「日期」作爲標題的列。正如其他人已經提到的那樣,您需要徹底重新設計您的概念。訪問不是電子表格。 – Gustav

相關問題