I looked at your code. There are a couple of problems, as June said.
The Debug.Print statement will print the sSQL string you created so it can be checked.
I created a form and added controls, and entered some data.
This is the string that was generated with your code:
Code:
INSERT INTO tblAccounts (ClientID, T, MonthlyDate, ProductID, Y, Z, V, W, ) VALUES (1, 0, 4/1/2013, 8, 0, 0, 0, 0, )
Note the comma after the "W" and the comma after the last zero. They shouldn't be there.
I modified your code and now have this:
Code:
INSERT INTO tblAccounts (ClientID, T, MonthlyDate, ProductID, Y, Z, V, W ) VALUES (1, 0, #4/1/2013#, 8, 0, 0, 0, 0)
Code:
Option Compare Database '<<- these two lines should be at the top of EVERY code module
Option Explicit '<<- these two lines should be at the top of EVERY code module
Private Sub button_Click()
Dim db As DAO.Database
Dim iMaxTcount As Double
Dim iMaxYCount As Double
Dim iMaxZcount As Double
Dim iMaxWCount As Double
Dim iMaxVcount As Double
Dim iYCount As Integer
Dim iTCount As Integer
Dim iZCount As Integer
Dim iVCount As Integer
Dim iWCount As Integer
Dim ctl As Control
Dim TValue As Double
Dim YValue As Double
Dim ZValue As Double
Dim VValue As Double
Dim WValue As Double
Dim ctl_T As Control
Dim ctl_Y As Control
Dim ctl_Z As Control
Dim ctl_V As Control
Dim ctl_W As Control
Dim sSQL As String '<<-- you didn't have sSQL declared
Set db = CurrentDb
iMaxTcount = 0
iMaxYCount = 0
iMaxZcount = 0
iMaxVcount = 0
iMaxWCount = 0
iTCount = 1
iYCount = 1
iZCount = 1
iVCount = 1
iWCount = 1
For Each ctl In Me.Controls
If InStr(ctl.Tag, "TV") > 0 Then
If Len(ctl.Value) > 0 Then
iMaxTcount = iMaxTcount + 1
End If
End If
Next ctl
For Each ctl In Me.Controls
If InStr(ctl.Tag, "YV") > 0 Then
If Len(ctl.Value) > 0 Then
iMaxYCount = iMaxYCount + 1
End If
End If
Next ctl
For Each ctl In Me.Controls
If InStr(ctl.Tag, "ZV") > 0 Then
If Len(ctl.Value) > 0 Then
iMaxZcount = iMaxZcount + 1
End If
End If
Next ctl
For Each ctl In Me.Controls
If InStr(ctl.Tag, "VV") > 0 Then
If Len(ctl.Value) > 0 Then
iMaxVcount = iMaxVcount + 1
End If
End If
Next ctl
For Each ctl In Me.Controls
If InStr(ctl.Tag, "WV") > 0 Then
If Len(ctl.Value) > 0 Then
iMaxWCount = iMaxWCount + 1
End If
End If
Next ctl
iTCount = 0
Do While iTCount <= iMaxTcount
For Each ctl In Me.Controls
If ctl.Name = "T_Head" & iTCount Then
TValue = ctl.Value
End If
Next ctl
iYCount = 0
Do While iYCount <= iMaxYCount
For Each ctl In Me.Controls
If ctl.Name = "Y_Head" & iYCount Then
YValue = ctl.Value
End If
Next ctl
iZCount = 0
Do While iZCount <= iMaxZcount
For Each ctl In Me.Controls
If ctl.Name = "Z_Head" & iZCount Then
ZValue = ctl.Value
End If
Next ctl
'Brokerage Fee
iWCount = 0
Do While iWCount <= iMaxWCount
For Each ctl In Me.Controls
If ctl.Name = "W_Head" & iWCount Then
WValue = ctl.Value
End If
Next ctl
iVCount = 0
Do While iVCount <= iMaxVcount
For Each ctl In Me.Controls
If ctl.Name = "V_Head" & iVCount Then
VValue = ctl.Value
End If
Next ctl
For Each ctl In Me.Controls
If ctl.Name = "T" & iTCount & "Y" & iYCount & "Z" & iZCount & "V" & iVCount & "W" & iWCount Then
sSQL = "INSERT INTO tblAccounts ("
sSQL = sSQL & "ClientID, "
sSQL = sSQL & "T, "
sSQL = sSQL & "MonthlyDate, "
sSQL = sSQL & "ProductID, "
sSQL = sSQL & "Y, "
sSQL = sSQL & "Z, "
sSQL = sSQL & "V, "
sSQL = sSQL & "W "
sSQL = sSQL & ") VALUES ("
sSQL = sSQL & MySelector & ", "
sSQL = sSQL & TValue & ", "
sSQL = sSQL & "#" & cboMonthlyDate & "#, "
sSQL = sSQL & cboProduct & ", "
sSQL = sSQL & YValue & ", "
sSQL = sSQL & ZValue & ", "
sSQL = sSQL & VValue & ", "
sSQL = sSQL & WValue & ") "
Debug.Print sSQL 'Your Suggestion
db.Execute sSQL
End If
Next ctl
iTCount = iTCount + 1
Loop
iYCount = iYCount + 1
Loop
iZCount = iZCount + 1
Loop
iWCount = iWCount + 1
Loop
iVCount = iVCount + 1
Loop
Cmd_Save.Enabled = False
End Sub
I have no idea what you are doing with the code, so I don't know if there are other errors.