简体   繁体   中英

Using the Insert into SQL in MS Access database

I'm trying to copy a number of columns from one table to another table (column names are the same in both tables) when a (tick box) column is checked in my MS Access database. I know that I can use the Insert Into SQL query but it doesn't seem to be working.

When I try to run it I just get

Syntax error in INSERT INTO statement

This is what I have written:

INSERT INTO Fauna_ATT (Burn ID, Burn Name, Value Effected, Pre Burn Action Details, During Burn Action Details, Post Burn Action Details) 
    SELECT Burn ID, Burn Name, Value Effected, Pre Burn Action Details, During Burn Action Details, Post Burn Action Details 
    FROM EPFP 
    WHERE ATT="TRUE";

Fauna_ATT is my table I want the columns to be placed

Burn ID, Burn Name, Value Effected, Pre Burn Actions, During Burn Actions and Post Burn Actions are my columns I want to copy

EPFP is my table I want the columns to come from and I only want the columns to be copied over when my ATT (check box column) is ticked.

enclosed your column names to "[]"

INSERT INTO Fauna_ATT ([Burn ID], [Burn Name], [Value Effected], [Pre Burn Action Details], [During Burn Action Details], [Post Burn Action Details]) 
SELECT [Burn ID], [Burn Name], [Value Effected], [Pre Burn Action Details], [During Burn Action Details], [Post Burn Action Details]
FROM EPFP 
WHERE ATT=1;

details on when to enclose column names to brackets can be found here

    OleDbConnection conn;
    OleDbCommand cmd;
    OleDbDataReader dr;


   private void connectDb()
   {
            conn = new OleDbConnection(@"Your Connection String");
            conn.Open();//initialization of your connection
   }

   private void performCommandOne()
   {

         String command1 = "INSERT INTO Fauna_ATT values(@Burn_ID, @Burn_Name, 
         @Value_Effected, 
         @Pre_Burn_Action_Details, @During_Burn_Action_Details, 
         @Post_Burn_Action_Details)";

          /*insert statement and the name of your coloumns in your 
          database must be no space */

        /*this code dislays like this.
        if you perform the insert processs..*/
        /*connection of your database must be initialize first*/
        this.connectDb();
        cmd = new OleDbCommand(command1,conn);
        cmd.Parameters.AddWithValue("@Burn_ID", "Fields for Burn_ID");
        cmd.Parameters.AddWithValue("@Burn_Name", "Fields for Burn_Name");
        cmd.Parameters.AddWithValue("@Value_Effected", "Fields for Value_Effected");
        cmd.Parameters.AddWithValue("@Pre_Burn_Action_Details", "Fields for Pre_Burn_Action_Details");
        cmd.Parameters.AddWithValue("@During_Burn_Action_Details", "Fields for During_Burn_Action_Details");
        cmd.Parameters.AddWithValue("@Post_Burn_Action_Details", "Post_Burn_Action_Details");
        cmd.ExecutedNonQuery();
        conn.Close();

   }


   private void performCommandTwo()
   {

            String commandTwo = "SELECT Burn_ID, Burn_Name, Value_Effected, 
            Pre_Burn_Action_Details, 
            During_Burn_Action_Details, Post_Burn_Action_Details 
            FROM EPFP 
            WHERE ATT='TRUE'";

            this.connectDb();
            cmd = new OleDbCommand(commandTwo,conn);
            cmd.Parameters.AddWithValue("@ATT", "fOR aTT FIELDS");
            dr = cmd.ExcecuteReader();

            if(dr.Read())
            {
                 //Found the value
            }else
            {
                 //values is not exist..
            }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM