?
Solved

Adding a detail record with referential integrity

Posted on 2000-02-26
11
Medium Priority
?
137 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sageryd
ID: 2560658
How?
0
 

Accepted Solution

by:
Freaky earned 200 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
 
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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