I'm trying to use the alter table command to add fields to an existing table, with code restated below. However I get an error on the first db.execute command saying "too many fields defined". This code should create around (12+4)*3=48 new columns. The values from the form are as follows (just in case):
- me.yearsback= 1
- me.valdate = 5/31/2016
- me.period = "monthly"
Need some help here. Is there a different command I should be using or a totally different approach?
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