Retrieve data from a database - Delphi

Hey all,

I'm still a noob with delphi (nothing's changed since my last question).
This is for the same program as my last question, so if you're already
familiar with it great.  If not, here goes.

I have a box where the mileage for vehicles is supposed to be entered.
Instead of the user manually entering this data each time there is an
update, I would like for the field to pull the data from the database
used grdServices grid.  This is a grid on the same form where users
can add and remove vehicle service information.  I.e.  If a vehicle
has maintenance done on it and a user add this information along
with the current mileage of the vehicle, I want the above "mileage"
field to pull that data.

Here's what I've tried.  I tried changing the tbl data for the edtMileage
and pointed it from tblVehicles to tblServices.  This works and pulls
the data from the service table (the item that is listed first, which
is why I needed the descending data).  The problem however, is
now when I try to click on add to add a new service record, it goes
to the last record, and opens that data for me to modify, it will not
allow me to add new reports.

I know you'll prolly need some code, so let me know what you need.


thx!!

Steve
LVL 1
c230kochiAsked:
Who is Participating?
 
pcsentinelCommented:
Ok
forget the previous stuff

procedure TfrmService.btnOKClick(Sender: TObject);
begin
  dm.tblServices.FieldByName('vin').AsString:=dm.tblVehicles.fieldbyname('vin').AsString;
  dm.tblServices.Post;
  dm.tblVehicles.Edit;
  dm.tblVehicles.FieldByName('MILEAGE').AsInteger:=dm.tblServices.FieldByName('MILEAGE').AsInteger;
  dm.tblVehicles.Post;
  Close;
end;
0
 
pcsentinelCommented:
Steve I looked at the code for that button, and it looks like it should be doing the right thing i.e. adding a record, are you sure you dont mean that you want to edit the record?

regards
0
 
c230kochiAuthor Commented:
No the button is doing the right thing.  But I have a field that was setup to pull data from tblVehicles in the Mileage column.  I changed tis to pull from the tblServices in the mileage column and when I do this the add button stops working correctly.  I don't know much about dbs but I was thinking that maybe the other field pulling the data is closing the dataset so that I can't append new information.  Otherwise I simply have that field setup wrong.  I'll send you the before and after code for that field.


The actual code is the same before and after.  The difference is in object insepctor under events the DataSource before is
DM.srcVehicles and after DM.tblVehicles.


procedure TfrmVehicle.edtMileageChange(Sender: TObject);
begin
  dm.tblVehicles.Edit;
  dm.tblVehiclesLAST_UPDATE.Value:=now;
  dm.tblVehicles.Post;
end;
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
pcsentinelCommented:
Ok the above code is on the edit box.

So what happens is everytime some types a character into it then it edits the current record in tblvehicles and changes the field LAST_IPDATE to the current time.

This is very inefficient code as it means the record is being updated every keypress.

I am still confused about the add button though

If I read you right

TblServices does an add
if the tblvehicles is on the right record then after the add you need something like

tblservices.edit;
tblservices.FieldByName('MILEAGE').AsInteger:=tblVehicls.FieldByName('MILEAGE').AsInteger;
tblservices.post;

0
 
c230kochiAuthor Commented:
The edit box was originally designed so that a user could edit the current mileage of the vehicle.  This is not efficient because
the user must remember to manually update this field every time a service is logged.  Instead I want this field to automatically
update every time a service is added.  The record that is highlighted at the start of the program is now the most recent service
update.  (This is why I need the descending, so the most current record was first).  When I change the edit box to tblServices
it automatically updates the mileage with the first record (unless of course you highlight another record).  This isn't an issue
because I added a report feature that goes through and grabs all the data as it first looks.  

I either need to have a way that this edit box will work the way it currently does but without causing an issue with adding
new data.  Or I need a way to have this edit box (or another box) automatically update Only when a new record is added.


thx
0
 
pcsentinelCommented:
Ok I think I've got it now but just to check

When you press the add button then you want to create a new record in the services table and because the vehicles table is already on the right vehicle  you want to copy the mileage record from the vehicles table to the services table new record? is that right?

If so then on the

procedure TfrmService.btnOKClick(Sender: TObject);
begin
  dm.tblServices.FieldByName('vin').AsString:=dm.tblVehicles.fieldbyname('vin').AsString;
  dm.tblServices.Post;
  Close;
end;

change it to


procedure TfrmService.btnOKClick(Sender: TObject);
begin
  dm.tblservices.FieldByName('MILEAGE').AsInteger:=tblVehicles.FieldByName('MILEAGE').AsInteger;
  dm.tblServices.FieldByName('vin').AsString:=dm.tblVehicles.fieldbyname('vin').AsString;
  dm.tblServices.Post;
  Close;
end;
0
 
c230kochiAuthor Commented:
Yes that is correct.  I will implement this code and tell you how it goes.
0
 
c230kochiAuthor Commented:
Here's what I have.

procedure TfrmService.btnOKClick(Sender: TObject);
begin
  dm.tblServices.FieldByName('MILEAGE').AsInteger:=dm.tblVehicles.FieldByName('MILEAGE').AsInteger;
  dm.tblServices.FieldByName('vin').AsString:=dm.tblVehicles.fieldbyname('vin').AsString;
  dm.tblServices.Post;
  Close;
end;


This makes the add button work just fine.  But when I add a new field it doesn't update the tblVehicles field
'mileage'.


thx
0
 
pcsentinelCommented:
Wo hold on did we get things the wrong way round?

The code above will copy the mileage from the vehicles table to the services table

I think in your last sentence you mean add a new record not field.

Are you trying to put the mileage from the
services record into the vehicle record
or
vehicel record into the serives record?
0
 
c230kochiAuthor Commented:
:)

I'm trying to take the mileage data from the last service mileage data entry, and place it
in the vehicle mileage data edit box.

I thought it might go something like this, but the program didn't like it.

dm.tblVehicles.FieldByName('MILEAGE').AsInteger:=dm.tblServices.FieldByName('MILEAGE').AsInteger;

Maybe cuz its and edt field, I don't know enuf to say.
0
 
c230kochiAuthor Commented:
I get an error that says dataset not in edit mode

raise EADSDatabaseError.create( self, AE_TADSDATASET_GENERAL, SNotEditing );


0
 
pcsentinelCommented:
When you click the ok button?
0
 
c230kochiAuthor Commented:
no when i go into the vehicle record to make modifications.
0
 
pcsentinelCommented:
I think that may be to do with

procedure TfrmVehicle.edtMileageChange(Sender: TObject);
begin
  dm.tblVehicles.Edit;
  dm.tblVehiclesLAST_UPDATE.Value:=now;
  dm.tblVehicles.Post;
end;

try commenting this code out for now and see if it sorts the problem out
0
 
c230kochiAuthor Commented:
Okay, cool that worked.


Thanks for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.