Solved

Adding a detail record with referential integrity

Posted on 2000-02-26
11
130 Views
Last Modified: 2010-04-04
I have a master-table and a detail-table. The detail-table is linked to the master-table like so:

Master.ID(Autoincrement) <--> Detail.Disc(Longint)


How can I add a detail record to a newly added master-record? I can't get the Detail.Disc field right!

johan
0
Comment
Question by:sageryd
  • 5
  • 4
  • 2
11 Comments
 

Expert Comment

by:Freaky
ID: 2560615

Hi!

It should work with the following code:

If not Master.Active then Abort;
With Detail do
 Begin
  Append;
  FieldByName('Disc').AsInteger:=
Master.FieldByName('ID').AsInteger;
  Post;
 End;

Greets

 Sascha
0
 
LVL 1

Author Comment

by:sageryd
ID: 2560624
Well, no. That's what I've been trying. The "ID" field is an autoincrement, and it seems like it isn't updated until the master table is closed and then re-opened, but if I close it and re-open it, the cursor position will change, and I will not be able to check the ID of the last record. There must be some other way.
0
 

Expert Comment

by:Freaky
ID: 2560628

Well, you could insert it via SQL directly ?
0
 
LVL 1

Author Comment

by:sageryd
ID: 2560658
How?
0
 

Accepted Solution

by:
Freaky earned 50 total points
ID: 2560665
Well, I wrote a simple function which creates a query-object, executes and returns it.

like that

function DoQuery(Alias, SQLStr : String; JustExec, Live : Boolean) : TQuery;
begin
 Try
  Result:=TQuery.Create(Nil);
  With Result do
   Begin
    DatabaseName:=Alias;
    RequestLive:=Live;
    SQL.Add(SQLStr);
    Prepare;
    If JustExec then ExecSQL
                else Open;
   End;
 Except
  On E : Exception do Begin
                       Result:=Nil;
                       ShowMessage(E.Message);
                      End;
 End;
end;


Now you can call it to insert your record.

Ex

procedure InsertDetailRecord;
var Q : TQuery;
Begin
 Q:=Nil;
 Q:=DoQuery(AppDatabase.DatabaseName,
           'INSERT INTO TARGETDB (fieldname1,fieldname2,.....) VALUES (fieldvalue1, fieldvalue2, .....)',True,False);

Now you can check the Q if errors occured or just free it.

But be careful, working with SQL directly can be a tricky thing.


Greets

 Sascha
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 27

Expert Comment

by:kretzschmar
ID: 2560911
what database, sagaryd?

to freaky,
i guess your answer doesn't help,
because the value of the masterid (the autoi-field) isn't available,
when you do this.

meikl
0
 

Expert Comment

by:Freaky
ID: 2560931


to kretzschmar,

yes, but you can read the mastervalue out before you add the detailrecord .. don't you think it's possible ?

Sascha
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2560989
to freaky,

depends on the database,
if access via odbc, then not

meikl
0
 
LVL 1

Author Comment

by:sageryd
ID: 2561256
Freaky, I think I will insert the records without using SQL. Thanks anyway. I found a workaround by myself to my problem, so I really don't need any help no more. Freaky, I'll give you the point anyway, I'm feeling great today!

Meikl, if you still want to know what type of database I'm using then it is a Local Paradox 7. (havn't really got so far using databases as you may know)

Thanks to ya all! ;)

//johan
0
 

Expert Comment

by:Freaky
ID: 2561431

To Sageryd

I thank you sincerely and hope that the response will perhaps sometime be useful (again).


Greets

 Sascha

0
 
LVL 1

Author Comment

by:sageryd
ID: 2561465
Yes, maybe....


Cheers!

Johan
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now