473,785 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to delete a record in a one to many situation when using a list in form

I am building a database and I am using a list where I can make
multiple choices. The data is like this

Master table
item1id
item2
index(item1id)

detail table
item1id
item2id
index (item1id, item2id)

item2 table
item2id
item2
index(item2id)

My form would have

Item1

list of all the choices for item2 table where I can make multiple
choices.

This piece is easy enough to do inserts with. Loop on the listbox
array and insert.
Now the question is what do I do to:
1. Make changes
2. Delete an item.

Do I instead of update, build a function where I delete all the items
in the detail table belonging to item1 and then insert the new list?
Or is there a more elegant way to handle this?

May 24 '06 #1
2 1414
rich wrote:
I am building a database and I am using a list where I can make
multiple choices. The data is like this

Master table
item1id
item2
index(item1id)

detail table
item1id
item2id
index (item1id, item2id)

item2 table
item2id
item2
index(item2id)

My form would have

Item1

list of all the choices for item2 table where I can make multiple
choices.

This piece is easy enough to do inserts with. Loop on the listbox
array and insert.
Now the question is what do I do to:
1. Make changes
2. Delete an item.

Do I instead of update, build a function where I delete all the items
in the detail table belonging to item1 and then insert the new list?
Or is there a more elegant way to handle this?


Nope, you should just define the foreign key with "ON DELETE CASCADE"
option. You should do something like:

ALTER TABLE DETAIL ADD CONSTRAINT FK_MASTER_DETAI L
FOREIGN KEY(ITEM1) REFERENCES MASTER(ITEM1) ON DELETE CASCADE;

This is how it works on the known EMP and DEPT tables:

SQL> alter table emp add constraint fk_dept_emp
2 foreign key(deptno) references dept(deptno) on delete cascade;

Table altered.

SQL>

--
Mladen Gogala
http://www.mgogala.com
May 24 '06 #2
Easiest way is to delete and then insert. But you should do it one
record at a time. You dont want to delete all at once and then insert.
Otherwise if you have a system crash right after the delete you could
lose massive amounts of data instead of just one record.

To do this you need to add a new character type 'delete' flag column in
your items table which would be used for this purpose.

1) Update all the records you want to delete by putting in X in the
deleteFlag.
..
2) Delete the first one whose key matches the one you are inserting
from the list.

3) Insert the new one from the list (leave the deleteFlag column null
or put a blank)

Repeat the 1-3 process until you've inserted all your records from the
list.

4) Now delete all the records that still have an X in the delete flag
column. These would be the left over records.

You can also use Transactions but its only safe as long as your server
will not crash during commit.

May 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3196
by: Tony Williams | last post by:
I have two tables one lists the names of committees and the other is a list of documents they generate. I have a form based on the documents table which gives details of the document including which committee generated it. On the form I also want to indicate which other committees are interested in that document, there could be 1 or say 8, it differs for each document. I had thought of creating say 10 combo boxes based on the committee...
8
4066
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: DoCmd.SetWarnings False DoCmd.RunCommand acCmdDeleteRecord DoCmd.SetWarnings True End If ExitHere: Me!SubName.SetFocus
3
3974
by: Uwe Range | last post by:
Hi to all, I am displaying a list of records in a subform which is embedded in a popup main form (in order to ensure that users close the form when leaving it). It seems to be impossible to delete a record in this subform. When I switched modal off and tried to delete a record from the list, I deleted a record on another form (below the popup form).
6
4027
by: Bernd Smits | last post by:
Hi, I would like to delete a record (with commandbutton) of a table associated to a combobox, when I select a certain value in the combobox (the value I select is associated with the record that I want to delete. For example: I have a form associated to a table, which has only one field (PatientN°), and a combobox (within the form) that contains the list of values (PatientN°) contained in the table. Now I would like to select a value in...
4
7850
by: Susan Bricker | last post by:
I have a command button on a form that is supposed to Delete the record being displayed. The record is displayed one to a form. The form is not a Pop-Up nor is it Modal. Tracing the btnDelete event routine shows that AllowDeletions is TRUE. When the Delete button is clicked (without TRACE ON), I get a 'beep', the recordselector (vertical bar on left of form) gets dark in color, but the record is not deleted. Also, there is no error...
7
3056
by: Stephen Poley | last post by:
I have the following situation: - a table of employees, keyed on employee-id; - a table of training sessions, keyed on session-id; - a requirement to log who was present at which session, plus optional extra textual information (e.g. "left early due to illness"). The solution I had in mind was: 1) create a presence table, keyed on employee-id and session-id, containing a 'present' yes/no field and a comment text field; 2) cross-join...
6
3858
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called "Generations"), and it allows the user to add, edit and delete the various records of the table. "Generations" table has the following fields: "IDPerson", NamePerson", "AgePerson" and "IDParent". A record contains the information about a person (his name, his...
5
4147
by: Bob Bridges | last post by:
Start with two tables, parent records in one and child records in the other, a one-to-many relationship. Create a select statement joining the two. Display the query in datasheet mode. When I delete a row, only the child record is deleted from the source tables; the parent record is still there...which is what I wanted. Now display fields from that query in a continuous form. When I delete a record from that form, one of the child...
3
2523
by: blakerrr | last post by:
Hi All, I have a strange situation that I can't figure out. The task is quite simple, delete a record from a table. Here is my situation: I have a form called Order Create which cycles through the parts in my database, run off of a table called Master BOM. In this form there is a subform called Order Data (not linked to the main form) which has a field on it called OrderID. This subform is run off of a table called Order Data, which has...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.