Search code examples
mysqldelphidelphi-7

Syntax error in SQL SELECT INTO FROM from Delphi


I am trying to insert a new record into my database using SQL, but it keeps telling me that I have a syntax error in the from clause.

I don't see the error.

Here is the code:

procedure TForm1.BitBtn7Click(Sender: TObject);
var
 sCategoryName :string;
begin
 sCategoryName := InputBox('Category Name', 'Please enter your category name that you would like to add','');
 with dmRecords do
  begin
   qryRecords.Active := False;
   qryRecords.SQL.Add('INSERT INTO [Category of Income]([Category Name])');
   qryRecords.SQL.Add('VALUES ' + '(' + QuotedStr(sCategoryName) + ')');
   qryRecords.ExecSQL;
   qryRecords.SQL.Add('SELECT * FROM [Category of Income] ORDER BY [Category ID]');
   qryRecords.Active := True;
  end; 
end;

Solution

  • You haven't cleared the SQL from the previous statement. When you open the query, SQL has three lines of text.

    Add qryRecords.SQL.Clear before adding the SELECT statement.