Hi
I am trying to create SQL within a sub that uses the sum of three elements in a table and the current date but I then want the name of the table that these entries relate to to be inserted into the table also so the Totals for a table called C5_1 would show in the table as:
BWS Date BASIC BONUS COMMISSION
C5_1 18/12/2014 2000 500 700
But there could be multiple tables and I am using a recordset to give me the lsit of tables. I just cant seem to get the SQL of the SELECT for the tablename to work.
Below is what I am currently using:
There is currently only one TableName in the Table and that is C5_1
What am I getting wrong in the code
Thanks
Rich
I am trying to create SQL within a sub that uses the sum of three elements in a table and the current date but I then want the name of the table that these entries relate to to be inserted into the table also so the Totals for a table called C5_1 would show in the table as:
BWS Date BASIC BONUS COMMISSION
C5_1 18/12/2014 2000 500 700
But there could be multiple tables and I am using a recordset to give me the lsit of tables. I just cant seem to get the SQL of the SELECT for the tablename to work.
Below is what I am currently using:
Code:
Dim db As Database
Dim rs As Recordset
Dim strSQLText As String
Set db = CurrentDb()
Set rs = db.OpenRecordset("tblofC5_1Tables", dbOpenDynaset)
DoCmd.SetWarnings False
Do Until rs.EOF
strSQLTbl = "(SELECT MSysObjects.Name FROM MSysObjects WHERE MSysObjects.Name = " & rs!TableName & ")"
strInsertText = "INSERT INTO tblC5_1TotalCurrent ([BWS], [DateImported], [BASIC CASH], [BONUS CASH], _
[COMMISSION]) "
strSelectText = "SELECT Now()as DateImported, sum([" & rs!TableName & "].[BASIC CASH]) as [BASIC CASH], _
sum([" & rs!TableName & "].[BONUS CASH]) as [BONUS CASH], sum([" & rs!TableName & "].[COMMISSION]) as _
[COMMISSION]," & strSQLTbl & " as BWS "
strFromText = "FROM " & rs!TableName & ";"
strSQLText = strInsertText & strSelectText & strFromText
Debug.Print strSQLText
DoCmd.RunSQL strSQLText
rs.MoveNext
Loop
There is currently only one TableName in the Table and that is C5_1
What am I getting wrong in the code
Thanks
Rich