4

I would like to use an Insert Into query in Delphi XE2 to insert a user's information into a MS Access Database. The problem is I keep getting the same error:

Syntax error in INSERT INTO statement

I have done some research but there is no definitive answer. my source code is:

  opendb('QuizDB.mdb');

  DB.Close;
  DB.SQL.Add('INSERT INTO tblUsers');
  DB.SQL.Add('(FirstName,Surname,Username,Password,Grade)');
  DB.SQL.Add('Values (:Firstname, :Surname, :Username, :Password, :Grade)');

  Db.Parameters.ParamByName('Firstname').Value := pFirstname;
  Db.Parameters.ParamByName('Surname').Value := pSurname;
  Db.Parameters.ParamByName('Username').Value := pUsername;
  Db.Parameters.ParamByName('Password').Value := pPassword;
  Db.Parameters.ParamByName('Grade').Value := pGrade;
  DB.ExecSQL;

QuizDB being the database name, DB being a ADOQuery component and then p(var) being variables received as parameters.

How do I make it work?

0

1 Answer 1

3

PASSWORD is a reserved word in Access so if you use it as a column name you must enclose it in square brackets.

Try this instead:

DB.SQL.Add('INSERT INTO tblUsers ');
DB.SQL.Add('(FirstName,Surname,Username,[Password],Grade) ');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much. IT worked all I needed to do was add those brackets.
There must be close to a hundred questions regarding this trap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.