473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 2541
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.