我有字段,其中有作爲交貨日期的名稱,您可以在這裏看到: 我有大約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列 - PricingDate,OldFieldHeadingDate,Value。然後,您的表可以從表中SELECT Value FROM WHERE PricingDate =#somedate#AND OldFieldHeadingDate =#somedate#。您將在PricingDate中出現多個相同日期 - 也許可以使用PricingDate和OldFieldHeadingDate作爲主鍵。數據庫規則1 - 它不是電子表格。 :) –
領域是兩週前(周圍)的結算日期和交易止損。然後這個想法是在交易停止之前(月份)爲所有不同時期獲得一個新的時間表。對於每個日期,新價格是在最近的交易日期+交易止損前的期間(我們保持固定)的加權平均價格。此外,我不只有3列,而是75列。然而,如果我明白了你的想法,我可以爲結算日期創建一個索引,然後我可以計算新的價格,不是嗎? –
是的,索引將是第一個和第二個字段的組合 - 不需要75個字段,只需要三個字段。如果計算價格,您可以輕鬆編寫查詢以處理同一個表中較早的PricingDate/SettlementDate(?)記錄。 –