jack madge
jack madge

Reputation: 23

Delphi Database Connection Using ACCESS and ADO connections

Okay so basically I've been working on my computing project for a while now and I've got 90% of it working however I'm having a problem with Delphi where is says that my database is not connected/ there is a problem connecting however I've already tried writing the information to the screen and this showed me that the items I was looking to pick up where in fact being picked up so the failure is when the items are being input in to the database. This however shouldn't be happening as the System already has database information displayed from that table and the user can physically select things from the database tables within the program however when trying to store the information back into the database it just breaks. Me and my computing teacher can not work it out, any help would be appreciated.

The problem appears on the new orders page. If you'd rather look at the system then you can download it from here https://drive.google.com/folderview?id=0B_iRfwwM9QpHVXJnSkx4U1FjMlk&usp=sharing

procedure Tform1.btnSaveClick(Sender: TObject);
var orderID:integer;
count:integer;
begin
  try
    //save into the order table first
    tblOrder.Open;
    tblOrder.Insert;
    tblOrder.FieldByName('CustomerID').value:= strtoint(cboCustomer.Text);
tblOrder.Close;
tblOrder.Open;
tblOrder.Last;
orderID:=tblOrder.FieldByName('OrderID').Value;
showmessage(inttostr(orderID));


for count := 1 to nextFree-1 do
 begin
    if itemOrdered[count,1]<>0 then
    begin
      tblOrderLine.Open;
      tblOrderLine.AppendRecord([orderID, itemOrdered[count,1],itemOrdered[count,2]]);
    end;
  end;
showmessage('The order has been saved');
  except
       showmessage('There was a problem connecting to the database');
  end;
end;

Upvotes: 1

Views: 495

Answers (1)

Ken White
Ken White

Reputation: 125757

You're doing far too much open, do something, close, open. Don't do that, because it's almost certain that is the cause of your problem. If the data is already being displayed, the database is open already. If you want it to keep being displayed, the database has to remain open.

I also removed your try..except. You can put it back in if you'd like; I personally like to allow the exception to occur so that I can find out why the database operation failed from the exception message, rather than hide it and have no clue what caused it not to work.

procedure Tform1.btnSaveClick(Sender: TObject);
var 
  orderID: integer;
  count: integer;
begin
  //save into the order table first
  tblOrder.Insert;
  tblOrder.FieldByName('CustomerID').value:= strtoint(cboCustomer.Text);
  tblOrder.Post;
  orderID:=tblOrder.FieldByName('OrderID').Value;
  showmessage(inttostr(orderID));

  for count := 1 to nextFree-1 do
  begin
    if itemOrdered[count, 1] <> 0 then
    begin
      tblOrderLine.AppendRecord([orderID, itemOrdered[count,1],itemOrdered[count,2]]);
      tblOrderLine.Post;
    end;
  end;
  showmessage('The order has been saved');
end;

Upvotes: 2

Related Questions