Problems writing to MS Access database (Delphi)

I am trying to write code bits to a Microsoft access database from Delphi. I get data from TStringGrid. The first column has ItemID, and the second column shows the quantity. I would like it to go through TStringGrid and save each row as a repeating row in my database, and also keep the order identifier with it in each column (the order identifier remains the same for each order, so it does not need to be changed).

I get an error on startup saying

"Project Heatmat.exe raised the EVarientInvalidArgError exception class with the message" Invalid Argument. "The process stopped."

I can’t understand why this gives me this error, and as you probably see, I am still not very good at coding. Any help would be appreciated!

Thanks.

procedure TCreateNewOrder.btnSaveClick(Sender: TObject); var intNumber, count : integer; begin Count:= 0; if messagedlg ('Are you sure?', mtWarning, [mbyes, mbno], 0) = mryes then begin with HeatmatConnection.HeatmatDatabase do begin intNumber:= TBLOrder.RecordCount; TBLOrder.Append; TBLOrder['CustomerID']:= CompanyName.ItemIndex+1; TBLOrder['OrderID']:= intNumber +1; for count:= 1 to StringGrid1.RowCount-1 do begin TBLOrderedItem.Append; TBLOrderedItem['OrderID']:= intNumber+1; TBLOrderedItem['ItemID']:= StringGrid1.Cells[1, count]; TBLOrderedItem['Quantity']:= StringGrid1.Cells[2, count]; TBLOrderedItem.Post; end; end; end; end; 
+4
source share
1 answer

TStringGrid Cells are rows. an attempt to assign a string directly to a digital field will throw an exception.

Thus, it is good practice to assign values ​​to database fields via AsString , AsInteger , AsBoolean , etc ... this will result in the correct conversion.

In your code use:

 TBLOrderedItem.FieldByName('ItemID').AsString := StringGrid1.Cells[1, count]; 

The same is true for Quantity .

To assign an Integer value, use:

 TBLOrderedItem.FieldByName('OrderID').AsInteger := intNumber + 1; 

By the way, you forgot TBLOrder.Post ie:

 .... TBLOrder.Append; TBLOrder.FieldByName('CustomerID').AsInteger := CompanyName.ItemIndex + 1; TBLOrder.FieldByName('OrderID').AsInteger := intNumber + 1; TBLOrder.Post; ... 

Finally, I would also suggest renaming TBLOrder to TBLOrder so that this name does not mean that it is Type .

+6
source

Source: https://habr.com/ru/post/1401372/


All Articles