0

When running my VBA code in access I get the runtime error 424, "Object Required". This Is the code causing the error (specifically line 2)

 DoCmd.RunSQL "DELETE * From PF_PC_TOTAL; "
 DmCmd.RunSQL "INSERT INTO Table![PF_PC_TOTAL] VALUES ('Andre', 5, 6 ) ;"
shruti1810
  • 3,920
  • 2
  • 16
  • 28
Chris Chevalier
  • 620
  • 1
  • 7
  • 20
  • what is the structure of your `PF_PC_TOTAL` table, maybe it has some fields that have to be set. Anyway: I would recommend buildung all queries inside the query design tool, that way they are kept up to date in regards to renaming... – luk2302 May 13 '15 at 14:44
  • RunSQL is nearly always a bad idea http://stackoverflow.com/questions/11213892/whats-the-difference-between-docmd-setwarnings-and-currentdb-execute/11213943#11213943 – Fionnuala May 13 '15 at 14:47

2 Answers2

2

Try this

DmCmd.RunSQL "INSERT INTO PF_PC_TOTAL (COL1, COL2, COL3) VALUES ('Andre', 5, 6 ) ;"

and replace COL1, COL2 and COL3 with actual column names in the table

Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28
0

Omit the "Table!" prefix from your table name. The "bang" notation is only recognized by VBA code, not the SQL engine that your VBA code is using.

DmCmd.RunSQL "INSERT INTO [PF_PC_TOTAL] VALUES ('Andre', 5, 6 ) ;"

There is another question which explains that part in more detail and has some good links: Bang Notation and Dot Notation in VBA and MS-Access

You may also want to check the code samples in the documentation: https://msdn.microsoft.com/en-us/library/office/ff834799.aspx

They are using the .Execute method of the Database object, but the SQL syntax is the same for DoCmd.RunSQL.

Community
  • 1
  • 1
GuitarPicker
  • 316
  • 2
  • 11
  • As @Saagar Elias Jacky alluded to, it is a much better practice to specify the column names as well, although it is technically optional as long as the number and type of columns match. Not specifying the names will come back to bit you if someone ever adds a column or rearranges the column order. – GuitarPicker May 13 '15 at 15:02