Copying record of table in itself via vba

Asked

Viewed 1,241 times

-1

Can you copy a record from a table and insert it in itself? I wanted to copy a record into a new one on the same table.

INSERT INTO Produtos ( Produto, Preco )
SELECT Produtos.Produto, Produtos.Preco
FROM Produtos
WHERE (((Produtos.Produto)="AN001"));
  • 1

    Hello! Out of curiosity, you tried to execute this already, and what was the result? any error message ?

  • 1

    This code returns some error?

  • If you’re having a hard time with this, maybe it’s because you’re doubling the Primary Key. Usually SQL tools allow this type of code, but this would only work in the form of the question if you have a code (ID) with auto increment.

  • @utluiz I was going to comment on keys, but I thought better to wait for a change in the question (more details: worked or not? with which error?) to then give an answer. I’m pretty sure your comment is the answer to the question

  • 1

    Regis, do not edit the title to "Solved". Post your solution and mark it as sure.

  • 5

    Don’t edit the question to apply the answer... I suggest you change the question so that it has the necessary code to notice the error that you discovered and after that, you put an answer to the solution, marking it as the correct answer, thus closing this subject. :)

  • You should add an answer instead of changing the question.

  • 6

    I am trying to understand what is happening with this question at http://meta.answall.com/questions/533/o-que-esta-acontecendo-com-uma-questiona-ja-resolver

  • I cannot post an answer to myself, but as I said. It has already been solved, and the correct answer is in the sequence.

  • 2

    @Regisdasilva We are trying to reopen the question, then you can post an answer. If you prefer, you can also simply delete the question (i.e. if you find that it is not very useful to keep the question on the site; but I am not saying that this is what needs to be done - the decision is yours alone!)

Show 5 more comments

1 answer

5


Answer originally put by the questioner in the question itself

It was just a syntax error, follow the correct code:

sql = "INSERT INTO Produtos ( Produto, Preco )" _
    & " SELECT '" & Me.CodProduto & "', Preco" _
    & " FROM Produtos" _
    & " WHERE Produto='" & Me.ListaProduto & "';"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.