473,387 Members | 1,904 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,387 software developers and data experts.

How to prevent accidental deletion of record in subform

Hello, I'm using Access 2013

I have an unbound sub form that I'm populating with a SQL query in VBA by creating a querydef and passing it to the sub form source control. The sub form is shown in datasheet view. I had a user accidentally delete a record through the sub form, and I want to make sure that doesn't happen again. I tried setting the allow deletions property to "no" but that property doesn't show up in the sub form properties. It does show up in the main form but if I set that to "no" they can still delete a record through the sub form even though the main form is locked. I don't want to lock the sub form because people still need to edit values in the sub form, but I want to eliminate the chance of deleting any records with it.

Is there something I'm overlooking? I've tried searching the internet and have not had any luck finding anything.

Any help would be appreciated.
Thanks,
Rodney
Nov 19 '15 #1

✓ answered by jforbes

You could try this for Line 30:
Expand|Select|Wrap|Line Numbers
  1.     Me.Item_Draw_subform.Form.AllowDeletions = False
It sets the AllowDeletions on the SubForm instead of the Main Form.

10 7225
NeoPa
32,556 Expert Mod 16PB
Each form has an AllowDeletions property. If you set that to True for the form that you're using as a Sub-form then it should disallow deletions while allowing edits.

Certain properties get reset by other actions, like setting the RecordSource property and by opening the form with particular parameters, so check the value of the property once the form is running. If it gets itself reset then add some code to set it to True when it's run.
Nov 19 '15 #2
NeoPa thank you for the quick response.

Shouldn't I set AllowDeletions to False if I don't want them to delete fields? Also, I have a combo box selector that changes the RecordSource from table to table, I added a chunk of code to set the Allow Deletions property to false every time the RecordSourse changes, but to no avail if I select a record in the subform and press delete it get's deleted.

I've tried setting the AllowDeletions in the form to true, and false, but it didn't make a difference.

Not sure if this makes any difference but as for properties this is what I've got on the main form, and none of these properties show up on the sub form.

Data Entry - set to yes (I only want them do add new data in the main form.)
Allow Additions - set to yes
Allow Deletions - set to no
Allow Edits - set to yes
Allow Filters - set to no

Thanks,
Rodney
Nov 19 '15 #3
zmbd
5,501 Expert Mod 4TB
This is part of my code used to setup forms.
In this case, a supervisor for a related table to allow editing of a record; however, I don't want additions nor deletions directly to the child records as these need to be recorded/validated for auditing reasons:
Expand|Select|Wrap|Line Numbers
  1. Case "zusermodesupervisor"
  2.         With Me
  3.             .AllowAdditions = False
  4.             .AllowDeletions = False
  5.             .AllowEdits = True
  6.             .Tag = 1
  7.             .z_ctrl_lbl_usermessage.Caption = "To add records use the controls below. You may Edit current records."
  8.         End With
Nov 19 '15 #4
ok, maybe I just need to show what I've got code wise

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboGetTables_AfterUpdate()
  2.  
  3.     Dim strQuery As String
  4.     Dim qry As QueryDef
  5.  
  6.     'check if MasterQ exist if it does delete it
  7.     For Each qry In CurrentDb.QueryDefs
  8.         If qry.Name = "MasterQ" Then CurrentDb.QueryDefs.Delete "MasterQ"
  9.     Next
  10.  
  11.     'choose query depending on user perfered sort order
  12.     If Me.chbSortDesc Then
  13.         strQuery = "SELECT T.PartNum, T.PrevPartNum, T.Description, T.Initials, T.DateIssued, T.DescLen, T.ECN, T.EngPrefVend, T.EngPrefVendNum, T.EngPrefMfg, T.EngPrefMfgNum FROM [" & Me.cboGetTables.Column(0) & "] as T WHERE (((t.PartNum) Like IIf([Forms]![Item Draw]![txtfPartNum] <> """", [Forms]![Item Draw]![txtfPartNum], ""*"")) And ((t.Description) Like IIf([Forms]![Item Draw]![fDesc] <> """", [Forms]![Item Draw]![fDesc], ""*"")) And ((t.Initials) Like IIf([Forms]![Item Draw]![txtInitials] <> """", [Forms]![Item Draw]![txtInitials], ""*"")) And ((t.DateIssued) >= IIf([Forms]![Item Draw]![DateIssued1] <> """", [Forms]![Item Draw]![DateIssued1], #1/1/1980#))) ORDER BY T.[" & Left(Me.cboGetTables.Column(0), 2) & "PartID]DESC;"
  14.     Else
  15.         strQuery = "SELECT T.PartNum, T.PrevPartNum, T.Description, T.Initials, T.DateIssued, T.DescLen, T.ECN, T.EngPrefVend, T.EngPrefVendNum, T.EngPrefMfg, T.EngPrefMfgNum FROM [" & Me.cboGetTables.Column(0) & "] as T WHERE (((t.PartNum) Like IIf([Forms]![Item Draw]![txtfPartNum] <> """", [Forms]![Item Draw]![txtfPartNum], ""*"")) And ((t.Description) Like IIf([Forms]![Item Draw]![fDesc] <> """", [Forms]![Item Draw]![fDesc], ""*"")) And ((t.Initials) Like IIf([Forms]![Item Draw]![txtInitials] <> """", [Forms]![Item Draw]![txtInitials], ""*"")) And ((t.DateIssued) >= IIf([Forms]![Item Draw]![DateIssued1] <> """", [Forms]![Item Draw]![DateIssued1], #1/1/1980#))) ORDER BY T.[" & Left(Me.cboGetTables.Column(0), 2) & "PartID]Asc;"
  16.     End If
  17.  
  18.     CurrentDb.CreateQueryDef "MasterQ", strQuery
  19.  
  20.     'chage form record source to match what was selected in the combo box
  21.     On Error Resume Next
  22.     Forms![Item Draw].RecordSource = Me.cboGetTables.Column(0)
  23.     If Err Then
  24.         MsgBox "Sorry, that table doesn't exist in the database yet.", vbOKOnly, "Error, Table not found"
  25.         Exit Sub
  26.     End If
  27.  
  28.     'set new query as source object in Subform, and prevend deletion of records
  29.     Me.Item_Draw_subform.SourceObject = "Query.MasterQ"
  30.     Me.AllowDeletions = False
  31.  
  32. End Sub
  33.  
This is what I've got coded when the query changes in the sub form. Even though I've got the allow deletions set to false I can still delete records in the sub form if I select the left side and press delete.

Thanks, guys for you help.
Nov 19 '15 #5
zmbd
5,501 Expert Mod 4TB
it appears that this code is on the parent form?

As NeoPa said in his post, this appears to be an issue with the subform itself.
+ Open your subform directly in design view
+ What is the property in the subform set to when you do this?
Nov 19 '15 #6
jforbes
1,107 Expert 1GB
You could try this for Line 30:
Expand|Select|Wrap|Line Numbers
  1.     Me.Item_Draw_subform.Form.AllowDeletions = False
It sets the AllowDeletions on the SubForm instead of the Main Form.
Nov 19 '15 #7
Sweet, jforbes that worked!!!! :-)

Thanks NeoPa and zmbd I did open the subform in design view and made sure that the allow deletion property was set to "no", but I was still able to delete a record.
Nov 19 '15 #8
NeoPa
32,556 Expert Mod 16PB
Hi Rodney.

You're quite right. I should have said to set it to False rather than True. That said, it is the form you use as a subform that you want to set it for. Not the main form nor the SubForm object itself.

I suspect that the fix to your code would be to replace lines #28 through #30 with :
Expand|Select|Wrap|Line Numbers
  1.     'set new query as source object in Subform, and prevent deletion of records
  2.     With Me.Item_Draw_subform
  3.         .SourceObject = "Query.MasterQ"
  4.         .AllowDeletions = False
  5.     End With
Nov 19 '15 #9
That helps, I was just confused because the subform object didn't have the property. so I didn't think to physically change the subform property because I didn't think it existed.

Thanks again all,
Nov 19 '15 #10
zmbd
5,501 Expert Mod 4TB
Instead of setting that in the Parent form I set the values directly in the form I am using as the subform. Typically in the on_open event; thus, the "me" holds true whenever the form is opened - ether directly or as subform.
Nov 19 '15 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: bher2 | last post by:
please help i was trying to delete a specific record on a table but it still shows the same record that should be deleted. i dont know how.
5
by: Alex | last post by:
Hi, I have tables that have default records that must not be deleted or modified. Is there an easy way to do this. Like setting a trigger on the Primary key value ? Alex
15
by: Hi5 | last post by:
Hi, I am designing a database for a client in which It has a client table including the followings: 1-Table Client 2-Table lookupcategory 3-Table Ctegory
13
by: ricky.agrawal | last post by:
I'm really not sure how to go about this in Access. What I've created is a table for each location. Those tables are identical in format but different in information. The tables are named after...
6
by: dk4300 | last post by:
Hi!! I have a subform with a comments (memo) field, linked to a form. On the Memo field: Properties-Other-Enter Key Behavior = New Line in Field On the subform: Properties- Other- Cycle =...
3
by: Kalaine | last post by:
MSAccess 2003: Users tab through the data entry fields on a form, and accidently tab to the next record thereby erroneously inserting a record. What can I code in the Form_BeforeInsert to prevent...
1
by: pamela fluente | last post by:
Is there an option to avoid the deletion of the entire site when doing the deployment (Publish feature) ? Why on the heart it has to erase *everything* by default? This seems absurd! If am I...
3
Jerry Maiapu
by: Jerry Maiapu | last post by:
Can please someone help me out. I am a beginner (of VB) I have a main form with a sub form created based on two tables having one to many relationships. i want to prompt the users a message...
2
by: BobS | last post by:
I have written a form whose function is to delete the record I have entered under the search criteria. The user enters the asset tag in the text box and presses the search cmd box . The record is...
0
zmbd
by: zmbd | last post by:
The question is based on post #3 in the following" http://bytes.com/topic/access/answers/940234-discussion-advantages-subform-vs-filtering In the filtred form users simply enter either the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.