Results 1 to 9 of 9
  1. #1
    tonycl69 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Nov 2012
    Posts
    102

    prevent record deletion if relasted records exist

    Hi all and thanks for reading.
    I have a database for vehicles and sometimes they go on a job and the location is recorded as to find it for servicing etc, the location is in a another table related to the vehicle table. But if I delete the location, regardless if there are any related records I can still delete the record from the location table. How can I prevent the user from deleting a location if related records exist, I am using a form to edit/delete location.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    set the relationship property, database tools tab menu:
    set CASCADE DELETE
    and
    CASCADE UPDATE

  3. #3
    tonycl69 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Nov 2012
    Posts
    102
    Hi and thanks for that, two things, I have been to check the relationship and the cascade update and delete are greyed out, also it is an access front end with all the tables on a SQL server

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Then it needs to be done on the backend.

  5. #5
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    Can you list the fields in your tables and an example of the data?

    Not sure I understand exactly your issue but you can always do a DLookUp on the VehicleID to see if there is a record before deleteing a Location record that is tied to that vehicle.

  6. #6
    tonycl69 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Nov 2012
    Posts
    102
    Hi Bulzie how would I write that, sorry not too familiar with vba.

  7. #7
    Bulzie is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    Nov 2015
    Posts
    1,474
    Maybe in the OnDelete Event of the Location form or on the Delete button. Assuming VehicleID is a field even if invisible on your Location record that they want to delete (hence the me.VehicleID).

    If Not isnull(DLookup("[VehicleID]", "tblVehicle", "[VehicleID] =" & me.VehicleID)) then
    Msgbox "This location cannot be deleted"
    End
    End If

    DoCmd.RunCommand acCmdDeleteRecord

  8. #8
    tonycl69 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Nov 2012
    Posts
    102
    Hi Bulzie, thanks for that much appreciated, I'll try it out now.

  9. #9
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    It sounds like you should not have "Cascading Delete".

    Since your BE is SQL Server, read these:

    Referential Integrity in Microsoft SQL Server
    http://www.craigsmullins.com/sql_ref.htm Look for a heading "Delete Rules"


    The following two links are for Visual Studio, but if you set Cascading actions for ON DELETE to "NO ACTION", you shouldn't be able to delete a PK record if there are child (FK) records

    Cascading Referential Integrity Constraints
    https://technet.microsoft.com/en-us/...ql.105%29.aspx

    Foreign Key Relationships Dialog Box (Visual Database Tools)
    https://technet.microsoft.com/en-us/...ql.105%29.aspx

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 12-09-2016, 08:58 AM
  2. Prevent data entry or deletion
    By mortonsafari in forum Forms
    Replies: 3
    Last Post: 02-01-2016, 01:10 PM
  3. Replies: 4
    Last Post: 07-10-2015, 07:51 AM
  4. Replies: 2
    Last Post: 06-11-2015, 08:42 AM
  5. Replies: 2
    Last Post: 02-29-2012, 12:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums