user4170051
user4170051

Reputation: 11

Copy paste field in Access vba

I am trying to write an access macro in VBA and to start I want it to copy and paste all the information from two different fields in one table into a new table. Is there a vba command that would allow me to do this in access? Any help would be highly appreciated.

Upvotes: 0

Views: 3470

Answers (1)

Paul Cooper
Paul Cooper

Reputation: 11

Charleh is right, but you can run "query statements" from VBA. Here are a couple of examples;

If the new table does not exist, or you want to overwrite it then;

Docmd.RunSQL "SELECT tbl_Table1.str_Field_1, tbl_Table1.str_Field_2 INTO tbl_Table_2 FROM tbl_Table1;"

If the target table (#2) exists and you want to append the data from tbl_Table1 try;

Docmd.RunSQL "INSERT INTO tbl_Table_2 ( str_Field_1, str_Field_2 ) SELECT tbl_Table1.str_Field_1, tbl_Table1.str_Field_2 FROM tbl_Table1;"

You do not have to use the same field names (but they have to be the same type) in the second example i.e.

Docmd.RunSQL "INSERT INTO tbl_Table_2 ( str_Field_A, str_Field_B ) SELECT tbl_Table1.str_Field_1, tbl_Table1.str_Field_2 FROM tbl_Table1;"

Upvotes: 1

Related Questions