2

I am unable to set default or null value in MS access. For example, when I tried to delete parent row, the foreign key of the affected child row is not set to NULL. I have tried setting default value to NULL at the design view but it does not seem to work.

Here is my sample data:
In staff table:
ID | Name
1 | Tom
2 | Tim

In customer table:
ID | Name | processed_By
1 | cust1 | 2

If I were to delete Tim from staff table, cust1 still retains 2 in processed_By column. However I wanted it to perform the following after deleting Tim from staff_table.

ID | Name | processed_By
1 | cust1 | NULL

OR

ID | Name | processed_By
1 | cust1 | 1

Hope that you can help me to solve this question. Thank you.

2
  • Please add more detail, for example, sample data and / or the code that did not work for you.
    – Fionnuala
    Jan 20, 2013 at 12:46
  • 1
    Hi Remou, I had updated it with sample data. Hope this helps
    – Dale
    Jan 20, 2013 at 13:23

3 Answers 3

2

You can do this easily enough in MS Access 2010 with the new data macros

Data macros even work when the table is used outside of MS Access. Note the keyword Old, applied to the Staff table where the After Delete macro is created. The line /* delete is a comment and purely for illustration.

delete data macro

0
2

My impression is you were looking for ON DELETE SET NULL. Access has supported that relationship feature since Jet 4 (Access 2000). However, there is no provision in the Access user interface to set ON DELETE SET NULL. You must do it from VBA code or with a DDL statement executed in SQL-92 mode.

I executed a DDL statement in the Immediate window. Note CurrentProject.Connection.Execute is an ADO method, which means the query is executed in SQL-92 mode instead of Access' default SQL-89 mode.

strSql = "ALTER TABLE Customer" & vbCrLf & _
    "ADD CONSTRAINT processed_By_FK" & vbCrLf & _
    "FOREIGN KEY (processed_By) REFERENCES staff(ID)" & vbCrLf & _
    "ON DELETE SET NULL"
CurrentProject.Connection.Execute strSql

Afterward, I deleted the staff record for Tim. That deletion caused the Customer.processed_By value for cust1 to change from 2 to Null.

If this approach sounds interesting, you can find further information from Allen Browne at Cascade to Null Relations and from this Stack Overflow question: MS Access set cascade-to-null constraint to existing table. There are several "gotchas" with ON DELETE SET NULL.

3
  • 1
    Hmm, this seems to be quite a reasonable feature. Do you know why they would keep this hidden from the user interface?
    – KekuSemau
    Jan 20, 2013 at 17:39
  • Not exactly, but I suspect it wasn't a deliberate choice to keep it hidden ... more likely they didn't see enough value to expose it in the UI. I think this is similar to other Jet 4 features (check constraints as one example) which were never exposed in the UI.
    – HansUp
    Jan 20, 2013 at 17:43
  • If you test the example I provided, you may see this message when deleting Tim from the staff table: allenbrowne.com/graphics/CascadeDeleteDialog.gif That doesn't seem right to me. I said ON DELETE SET NULL is supported, but I didn't say it is supported well. :-)
    – HansUp
    Jan 20, 2013 at 17:45
1

I don't think that you can do that. You can have the invalidated child row deleted automatically, but there is no built in function to set it back to null.

If your design really needs this, you can only do this through vba, but this only with a button on a form. When you delete rows directly in a table, I see no way to do this in access.

1
  • Interesting. I didn't know that.
    – KekuSemau
    Jan 20, 2013 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.