Solved

Adding a detail record with referential integrity

Posted on 2000-02-26
11
136 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
Industry Leaders: 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 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
 
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

Industry Leaders: 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!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

689 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