472,789 Members | 1,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 software developers and data experts.

deletion problems


I have 2 tables, Orders and Order Details, the same as with the database
Nortwindtraders shipped with Access. The table order is linked with the
table Order
Details through the filed OrderID. It is autonumber in the table orders
and number in the table Order Details.I have a very good function
deleting a chosen order from a list box called ListOrders. This function
is the following
Public Function CancelOrders()

Dim StrSQL As String
strSQL = "DELETE * FROM [order details] WHERE orderID = " &
frm!ListOrders & ";"
DoCmd.RunSQL strSQL

strSQL = "DELETE * FROM Orders WHERE orderID = " & frm!ListOrders & ";"
DoCmd.RunSQL strSQL
End Function

I want to copy this function and accomodate it to other needs.The table
order contains also the field PaymentID.I have made a new list boc
called ListInvoices.
I have built the following function, based on the field PaymentID :

Public Function CancelInvoices()
Dim StrSQL As String
strSQL = "DELETE * FROM Orders WHERE PaymentID = " & frm!ListInvoices &
";"
DoCmd.RunSQL strSQL
strSQL = "DELETE * FROM [order details] WHERE orderID = " &
frm!ListInvoices & ";"
DoCmd.RunSQL strSQL
End Function

This function does not work, it does not delete. I can see that the
reason is that do not refer to the field OrderID, because this is the
linking field.Also,the field
PaymentID does not exist in the table Order Details and therefore my
second line makes no sense to me.If i can find a way to refer to this
field also, may be my function might work.
I will be very grateful if somebody will help me.





*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #1
2 2511
Are you aware that you can use cascading deletes for this purpose?

1. Open the Relationships window (Tools menu).

2. If there is no relationship between these 2 tables, create one by
dragging Orders.OrderID onto [Order Details].OrderID. If there is a
relationship, double-click the line joining the 2 tables.

3. Check the boxes for Referential Integrity, and Cascading Deletes.

Now when you delete an order, the details are automatically deleted also.

In general, use cascading deletes with caution. For example, you do not want
cascading deletes on the relation between Customers and Orders. That would
be too dangerous: some user thinks they are helping by deleting an old
customer, but you would then also lose the history of sales.

If you don't like the idea of cascading deletes, you could genericize your
function so it deletes the desired records:

Public Function KillRecords(strTable As String, _
strKeyField As String, lngKeyValue As Long) As Long
Dim strSql As String

strSQL = "DELETE FROM " & strTable & _
" WHERE " & strKeyField & " = " & lngKeyValue & ";"
With dbEngine(0)(0)
.Execute strSQL, dbFailOnError
KillRecords = .RecordsAffected
End With
End Function

Then call it like this:
Call KillRecords("[order details]", "OrderID", frm!ListOrders)
Call KillRecords("Orders]", "OrderID", frm!ListOrders)

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html

"Peter yeshew" <fr*******@abv.bg> wrote in message
news:3f***********************@news.frii.net...

I have 2 tables, Orders and Order Details, the same as with the database
Nortwindtraders shipped with Access. The table order is linked with the
table Order
Details through the filed OrderID. It is autonumber in the table orders
and number in the table Order Details.I have a very good function
deleting a chosen order from a list box called ListOrders. This function
is the following
Public Function CancelOrders()

Dim StrSQL As String
strSQL = "DELETE * FROM [order details] WHERE orderID = " &
frm!ListOrders & ";"
DoCmd.RunSQL strSQL

strSQL = "DELETE * FROM Orders WHERE orderID = " & frm!ListOrders & ";"
DoCmd.RunSQL strSQL
End Function

I want to copy this function and accomodate it to other needs.The table
order contains also the field PaymentID.I have made a new list boc
called ListInvoices.
I have built the following function, based on the field PaymentID :

Public Function CancelInvoices()
Dim StrSQL As String
strSQL = "DELETE * FROM Orders WHERE PaymentID = " & frm!ListInvoices &
";"
DoCmd.RunSQL strSQL
strSQL = "DELETE * FROM [order details] WHERE orderID = " &
frm!ListInvoices & ";"
DoCmd.RunSQL strSQL
End Function

This function does not work, it does not delete. I can see that the
reason is that do not refer to the field OrderID, because this is the
linking field.Also,the field
PaymentID does not exist in the table Order Details and therefore my
second line makes no sense to me.If i can find a way to refer to this
field also, may be my function might work.
I will be very grateful if somebody will help me.

Nov 12 '05 #2
"Peter yeshew" <fr*******@abv.bg> wrote in message
news:3f***********************@news.frii.net...

In addition to what Alan said you might consider wrapping all your deletes
in a transaction - so that of one fails you don't leave the database in an
incistent state.
Nov 12 '05 #3

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

Similar topics

5
by: Tuhin Kumar | last post by:
Hi, I have a requirement on improving the deletion rate on on records of a table. The table contains 5 million records, but since deleting everything matching the condition at one go was giving...
0
by: Vic | last post by:
Hi all, When I test the Delete multi table function in MySQL, DELETE table_name ...] FROM table-references I accidentally delete all data in one table. All data in that table are gone...
6
by: sumit | last post by:
i am trying to restrict deletion on local hard drives i have expertise in c, c++, and delphi if the solution can be found on any of the above languages plz inform me.
2
by: Christopher Pisz | last post by:
currently my node is like so with everything public: class Quadtree_Node { public: Quadtree_Node(Quadtree_Node * parent, Bounding_Box & bounds); ~Quadtree_Node(); void Subdivide(float...
3
by: jaws | last post by:
My problems is as follows. Hope you guys can provide some insight. 1. I have a class which opens a file and simply spits out the text. Methods in place to open, print, close, and delete the...
0
by: Marco | last post by:
Hi to all I have a question about deletion of amount of data: My production environment is this one: - one publisher with a database (historycal events) - 50 subscribers with the prev...
7
by: TJS | last post by:
javascript "confirm" fires after deletion instead of before deletion. how do I get this to stop the processing ? code ================== Sub ShowAlert(ByVal s As string)...
3
by: A_Republican | last post by:
I am interested in writing my own secure file deletion program. I want to be able to read and write to my hard drive directly. My application will seach my hard drive for all locations marked for...
0
by: shivaranjaniadimulam21 | last post by:
Hello, As per the new restrictions on vista I am now creating my application files in Application.CommonAppDataPath folder but now the next problem is I am not able to delete few files created...
0
by: Rob P | last post by:
Is there a reliable way of doing this? It seems that I am having problems with the data files being used by 'other' processes, despite them being custom to the application I'm writing. I'm sure...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.