Benedict Ferguson
Benedict Ferguson

Reputation: 51

Syntax Error with Query

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?

Upvotes: 4

Views: 157

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123829

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) ');

Upvotes: 3

Related Questions