Hi I have the following insert into statement in my code and have created the RS table fields from a SELECT statement in my code.
I keep getting the following error at the insert into statement. The column that the data is failing on is the first field in the query and is a varchar field; when I create the table I create the column names, sizes and data types from that what is found in the query columns.Code:"INSERT INTO " & tbl.Name & "(" & RS.Fields(0).Name & ", " & RS.Fields(1).Name & ", " & RS.Fields(2).Name & ", " & RS.Fields(3).Name & ") VALUES (" & RS.Fields(0).Value & ", " & RS.Fields(1).Value & ", " & RS.Fields(2).Value & ", " & RS.Fields(3).Value & ")"
I have done a debug print for the INSERT INTO statement and it is as below:3075 syntax error (missing operator) in query expression 'FirstVarcharValue'
But from here I cannot see the problem. My full code is below for illustration purposes:INSERT INTO tablename(ColumnAliasName1, ColumnAliasName2, ColumnAliasName3, ColumnAliasName4) VALUES (FirstVarcharValue, Second Varchar Value, 3, 771804.16)
From here I do not really understand what is missing unless there is something I do nto have that should be in the code to make the DML statement correct for code?Code:Public Function resultset() As String On Error GoTo Err_SomeName Dim db As Database Const NewTableName = "OpptHeaderProducts" Dim RS As DAO.Recordset Dim LSQL As String Dim sqlInsert As String Dim ConcatVars As String Dim recordCnt As Integer Dim i As Integer Set db = CurrentDb() 'CreateTable LSQL = " SELECT o.Column2 AS ColumnAliasName1, " & _ " p.Column1 AS ColumnAliasName2," & _ " COUNT(*) AS ColumnAliasName3, " & _ " SUM(p.Column3) As ColumnAliasName4" & _ " FROM table1 o " & _ " INNER JOIN table2 p ON o.column1 = p.column4 " & _ " GROUP BY o.column2, p.column1 " & _ " HAVING COUNT(*) > 2 " & _ " AND SUM(p.Column3) > 10000 " & _ " ORDER BY o.p.Column2, p.Column1 " Set RS = db.OpenRecordset(LSQL) Dim tbl As TableDef If Not (TableExists(NewTableName)) Then Set db = CurrentDb() Set tbl = db.CreateTableDef(NewTableName) 'creates each column in new table relating to the name, type and size of the fields in the results set of the query For fldNo = 0 To RS.Fields.Count - 1 tbl.Fields.Append tbl.CreateField(RS.Fields(fldNo).Name, RS.Fields(fldNo).Type, RS.Fields(fldNo).Size) Next fldNo 'Move to first row of data in result set 'Check if result set not at end of file 'create a do loop until end of the result set is reached 'Execute insert statement for new table using result set values from first to last row 'once a row has been inserted then move to the next row 'if end of file is reached then call exit procedure RS.MoveFirst If Not (RS.EOF) Then Do Until RS.EOF sqlInsert = "INSERT INTO " & tbl.Name & "(" & RS.Fields(0).Name & ", " & RS.Fields(1).Name & ", " & RS.Fields(2).Name & ", " & RS.Fields(3).Name & ") VALUES (" & RS.Fields(0).Value & ", " & RS.Fields(1).Value & ", " & RS.Fields(2).Value & ", " & RS.Fields(3).Value & ")" ' this is just to allow debug printing Debug.Print sqlInsert db.Execute ("INSERT INTO " & tbl.Name & "(" & RS.Fields(0).Name & ", " & RS.Fields(1).Name & ", " & RS.Fields(2).Name & ", " & RS.Fields(3).Name & ") VALUES (" & RS.Fields(0).Value & ", " & RS.Fields(1).Value & ", " & RS.Fields(2).Value & ", " & RS.Fields(3).Value & ")") <<< Error raised here on first row of data RS.MoveNext Loop Else GoTo Exit_SomeName End If 'tbl.Fields.Append fld db.TableDefs.Append tbl db.TableDefs.Refresh Else MsgBox "This table already exists." End If RS.Close db.Close Exit_SomeName: Exit Function Err_SomeName: MsgBox Err.Number & " " & Err.Description Resume Exit_SomeName End Function
G




Reply With Quote