Search code examples
sqldelphims-accessdelphi-7ado

How to solve a default value error when running an update/insert query in Delphi 7?


I keep getting an error in Delphi 7 with my application that uses the Microsoft Jet engine and a Microsoft Access (*.mdb) database. I am making the connection via a TADOQuery component. The error says 'Parameter iPointsNew has no default value' and happens only when using an integer variable in an update/insert query:

frmHome.adoqryInLoop.SQL.Text := 'UPDATE Users SET Points = iPointsNew WHERE UID = "'+sUID+'"';

The event handler's code is as follows:

    procedure TfrmAdmin.bmbSubmitClick(Sender: TObject);
var
  sScore, sEID, sPrediction, sUID : String;
  iRecordCount, x, iPos, iLength, iActual, iPoints, iPointsNew : Integer;
  rPrediction : Real;
begin
  // Assign values to variables
  sScore := IntToStr(sedSuthies.Value) + '-' + IntToStr(sedOpponent.Value);
  iActual := sedSuthies.Value + sedOpponent.Value;
  sEID := frmHome.arrEID[lstEvents.ItemIndex];
  // Update the score for the event in the database
  frmHome.adoqryMain.Active := False;
  frmHome.adoqryMain.SQL.Clear;
  frmHome.adoqryMain.SQL.Text := 'UPDATE Events SET Score = "'+sScore+'",Complete = True WHERE EID = "'+sEID+'" ';
  frmHome.adoqryMain.ExecSQL;
  frmHome.adoqryMain.SQL.Text := 'SELECT * FROM Predictions WHERE EID = "'+sEID+'" ';
  frmHome.adoqryMain.Open;
  iRecordCount := frmHome.adoqryMain.RecordCount;
  //Assign points to users for all the predictions
  for x := 0 to (iRecordCount - 1) do begin
    sUID := frmHome.adoqryMain.Fields[1].AsString;
    sPrediction := frmHome.adoqryMain.Fields[4].AsString;
    iPos := Pos('-',sPrediction) - 1;
    iLength := Length(sPrediction) - iPos;
    ShowMessage('1');
    if ((sedSuthies.Value >= sedOpponent.Value) AND (StrToFloat(Copy(sPrediction, 1, iPos)) >= StrToFloat(Copy(sPrediction, iPos + 2, iLength + 1)))) OR ((sedSuthies.Value < sedOpponent.Value) AND (StrToFloat(Copy(sPrediction, 1, iPos)) < StrToFloat(Copy(sPrediction, iPos + 2, iLength + 1)))) then begin
      rPrediction := StrToFloat(Copy(sPrediction, iPos + 2, iLength + 1)) + StrToFloat(Copy(sPrediction, 1, iPos));
      if rPrediction >= iActual then
        rPrediction := rPrediction - iActual
      else
        rPrediction := iActual - rPrediction;
      iPoints := Round(10 * (1 - (rPrediction / iActual)));
    end
    else
      iPoints := 0;
    ShowMessage('2');
    frmHome.adoqryInLoop.Close;
    frmHome.adoqryInLoop.SQL.Text := 'SELECT * FROM Users WHERE UID ="'+sUID+'"';
    frmHome.adoqryInLoop.Open;
    iPointsNew := frmHome.adoqryInLoop.Fields[10].AsInteger + iPoints;
    ShowMessage(IntToStr(iPointsNew));
    frmHome.adoqryInLoop.Close;
    frmHome.adoqryInLoop.SQL.Text := 'UPDATE Users SET Points = iPointsNew WHERE UID = "'+sUID+'"';
    frmHome.adoqryInLoop.ExecSQL;
    frmHome.adoqryInLoop.Close;
    frmHome.adoqryInLoop.SQL.Text := 'UPDATE Predictions SET Complete = True WHERE UID = "'+sUID+'" AND EID = "'+sEID+'" ';
    frmHome.adoqryInLoop.ExecSQL;
    ShowMessage('3');
    ShowMessage(IntToStr(iPoints));
    frmHome.adoqryMain.Next;
  end;
  ShowMessage('Score succefully submitted!');
  frmHome.UpdateEventLists;
end;

This error only occurs when trying to use an integer variable. I have tried many solutions such as using quotes, plus' and even testing with a normal integer such as 10 (It works when using a set number). Any help would be greatly appreciated.

P.S. The ShowMessage functions are there for debugging purposes and will be removed.


Solution

  • Thanks to the help of @TLama and @kobik, I have come up with a solution that is as follows:

    The integer variable can either be enclosed in single quotes and plus' and put in an IntToStr function like this:

    frmHome.adoqryInLoop.SQL.Text := 'UPDATE Users SET Points = '+IntToStr(iPointsNew)+' WHERE UID = "'+sUID+'"';
    

    OR


    Parameterised queries can be used (A bit difficult if your whole program is not parameterised for consistency sake, but the benefits outweigh the disadvantages). For example:

    frmHome.adoqryInLoop.SQL.Text := 'UPDATE Users SET Points = :points WHERE UID = :uid';
    frmHome.adoqryInLoop.Params.ParamByName('points').Value := iPointsNew;
    frmHome.adoqryInLoop.Params.ParamByName('uid').Value := sUID;
    frmHome.adoqryInLoop.ExecSQL;
    

    Thanks for all the advice everyone!