我想使用alter table命令將字段添加到現有的表,代碼重述如下。但是,我在第一個db.execute命令中出現錯誤,提示「定義的字段太多」。此代碼應創建大約(12 + 4)* 3 = 48個新列。是從形式的值如下:(以防萬一):改變表中的循環添加多個字段vba access
- me.yearsback = 1
- me.valdate = 2016年5月31日
- me.period = 「每月」
在這裏需要一些幫助。是否有我應該使用的不同命令或完全不同的方法?
Option Compare Database
Private Sub EP_Click()
Dim db As Database
Dim rs As Recordset
Dim x As Integer
Dim y As Integer
Dim Months As Integer
Dim WPmonthly As String ' field name for monthly written premium
Dim UPRmonthly As String ' field name for monthly unearned premium
Dim EPmonthly As String ' field name for monthly earned premium
Dim runningDate As Date
Dim runningDate2 As Date
Dim useDateLower As Date
Dim useDateUpper As Date
Months = Me.YearsBack * 12 + Month(Me.ValDate)
If Me.Period = "monthly" Then
Set db = CurrentDb
For x = 1 To Months
runningDate = Format(DateAdd("m", -x + 1, Me.ValDate), "mm yyyy")
WPmonthly = "WP M" & Month(runningDate) & " " & Year(runningDate)
EPmonthly = "EP M" & Month(runningDate) & " " & Year(runningDate)
UPRmonthly = "UPR M" & Month(runningDate) & " " & Year(runningDate)
db.Execute "ALTER TABLE tblEPdata ADD COLUMN [" & WPmonthly & "] STRING;"
db.Execute "ALTER TABLE tblEPdata ADD COLUMN [" & EPmonthly & "] STRING;"
db.Execute "ALTER TABLE tblEPdata ADD COLUMN [" & UPRmonthly & "] STRING;"
If x = Months Then
runningDate = Format(DateAdd("m", -x + 1, Me.ValDate), "mm yyyy")
UPRmonthly = "UPR M" & Month(runningDate) & " " & Year(runningDate)
db.Execute "ALTER TABLE tblEPdata ADD COLUMN [" & UPRmonthly & "] STRING;"
End If
Next
End If
db.Close
End Sub