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

Delete a Record in Access

675 512MB
I have a main form named fAAA. I have a Command Button "cmdDelRecord", with the following code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelRecord_Click
  2. On Error GoTo ErrorMessage
  3.     Forms!fAAA.RecordSource="tAAA"
  4.     Forms!fAAA.FilterOn=True
  5.     Forms!fAAA.Filter="Key=555"
  6.     DoCmd.RunCommand acCmdDelete
  7.     Exit Sub
  8. ErrorMessage:
  9.    MsgBox Err.Number & ": " & Err.Description
  10. End Sub 'cmdDelRecord_Click
  11.  
When I run the code, I get a "chime" but nothing is deleted. Why doesn't this code Delete a Record from the table = tAAA, or at least raise an error?
May 18 '07 #1
6 19192
JConsulting
603 Expert 512MB
I have a main form named fAAA. I have a Command Button "cmdDelRecord", with the following code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelRecord_Click
  2. On Error GoTo ErrorMessage
  3.     Forms!fAAA.RecordSource="tAAA"
  4.     Forms!fAAA.FilterOn=True
  5.     Forms!fAAA.Filter="Key=555"
  6.     DoCmd.RunCommand acCmdDelete
  7.     Exit Sub
  8. ErrorMessage:
  9.    MsgBox Err.Number & ": " & Err.Description
  10. End Sub 'cmdDelRecord_Click
  11.  

When I run the code, I get a "chime" but nothing is deleted. Why doesn't this code Delete a Record from the table = tAAA, or at least raise an error?

{guessing}
When you press the button, you set the recordsource, filter the form, but don't select a record to delete
May 18 '07 #2
OldBirdman
675 512MB
You are probably right, but I do not know how to select the current record (and the filter will assure that there is only one record).

I have tried to search this site, but the only time the question was asked "Code to select a record " on 12 Nov05, it was never answered.

I would like to insert this statement immediately preceeding the line
"DoCmd.RunCommand acCmdDelete"
May 18 '07 #3
JConsulting
603 Expert 512MB
You are probably right, but I do not know how to select the current record (and the filter will assure that there is only one record).

I have tried to search this site, but the only time the question was asked "Code to select a record " on 12 Nov05, it was never answered.

I would like to insert this statement immediately preceeding the line
"DoCmd.RunCommand acCmdDelete"
ok...we'll get you there.

Can I ask why you're setting the recordsource during a delete event?

Is it your intention to isolate the record prior to deleting?

Are you on the form that you're making reference to in your code when you try to delete?

with the code, the "Key" = 555 is hardcoded...which means that this event would realistically delete 1 record, the screen would shoe #Deleted, and you would have to go into the code and change that 555 to something else in ordeer for the delete button to work again.

So I guess I need to understand how this is all used, and what the expected action before and after pressing the delete key is.

is "Key" a field name?
May 19 '07 #4
OldBirdman
675 512MB
I thought it would be easier to present my problem if I could isolate it from a large and fairly complex program. By putting all the code into a single command, the essentials of the problem are presented. In the actual program, the procedure is lines 1,7, and 11.

I have made 2 changes in the code since post #1. I added line #6 and I changed "acCmdDelete" to "acCmdDeleteRecord" in line #7
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelRecord_Click
  2. On Error GoTo ErrorMessage
  3.     Forms!fAAA.RecordSource="tAAA"
  4.     Forms!fAAA.FilterOn=True
  5.     Forms!fAAA.AllowDeletions = True
  6.     Forms!fAAA.Filter="Key=555"
  7.     DoCmd.RunCommand acCmdDeleteRecord
  8.     Exit Sub
  9. ErrorMessage:
  10.    MsgBox Err.Number & ": " & Err.Description
  11. End Sub 'cmdDelRecord_Click
  12.  
Referencing the code lines -
Code line #1 - I have a control on my form named "cmdDelRecord", with VBA code
Code line #2 - I am aware that Access may get an error, and if so, I want to know what it is
Code line #3 - My form, here named "fAAA" is bound to a Table named "tAAA"
Code line #4 - I am aware that assigning a filter is not enough, "FilterOn" must also be True
Code line #5 - Form property Allow Deletions is set to True
Code line #6 - Of course this would only delete the same record over & over. I used this as an example to show that the filter restricts the form to a single record. In practice, the filter is set by any one of several choices by the user. Once the user has displayed the correct record, he may choose to press cmdDelRecord. Here, I am using Key=555 as a simplification of that process. And yes, Key is a field in table tAAA. It is the Primary Key for the table, and is Indexed = Yes (No Duplicates).
Code line #7 - I want to delete the displayed record.

Changing from acCmdDelete to acCmdDeleteRecord, probably the correct constant, I get error #2046 "The command or action 'Delete Record' isn't available now."

Because my filter restricts the records to 1, I have fAAA.RecordSelectors=No. If I change this to =Yes, the currently displayed record in the form is always selected.

Why doesn't this code Delete a Record from the table = tAAA?
May 19 '07 #5
JConsulting
603 Expert 512MB
I thought it would be easier to present my problem if I could isolate it from a large and fairly complex program. By putting all the code into a single command, the essentials of the problem are presented. In the actual program, the procedure is lines 1,7, and 11.

I have made 2 changes in the code since post #1. I added line #6 and I changed "acCmdDelete" to "acCmdDeleteRecord" in line #7
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdDelRecord_Click
  2. On Error GoTo ErrorMessage
  3.     Forms!fAAA.RecordSource="tAAA"
  4.     Forms!fAAA.FilterOn=True
  5.     Forms!fAAA.AllowDeletions = True
  6.     Forms!fAAA.Filter="Key=555"
  7.     DoCmd.RunCommand acCmdDeleteRecord
  8.     Exit Sub
  9. ErrorMessage:
  10.    MsgBox Err.Number & ": " & Err.Description
  11. End Sub 'cmdDelRecord_Click
  12.  
Referencing the code lines -
Code line #1 - I have a control on my form named "cmdDelRecord", with VBA code
Code line #2 - I am aware that Access may get an error, and if so, I want to know what it is
Code line #3 - My form, here named "fAAA" is bound to a Table named "tAAA"
Code line #4 - I am aware that assigning a filter is not enough, "FilterOn" must also be True
Code line #5 - Form property Allow Deletions is set to True
Code line #6 - Of course this would only delete the same record over & over. I used this as an example to show that the filter restricts the form to a single record. In practice, the filter is set by any one of several choices by the user. Once the user has displayed the correct record, he may choose to press cmdDelRecord. Here, I am using Key=555 as a simplification of that process. And yes, Key is a field in table tAAA. It is the Primary Key for the table, and is Indexed = Yes (No Duplicates).
Code line #7 - I want to delete the displayed record.

Changing from acCmdDelete to acCmdDeleteRecord, probably the correct constant, I get error #2046 "The command or action 'Delete Record' isn't available now."

Because my filter restricts the records to 1, I have fAAA.RecordSelectors=No. If I change this to =Yes, the currently displayed record in the form is always selected.

Why doesn't this code Delete a Record from the table = tAAA?
if you're building a filter...then you can simply use that same criteria to build a delete SQL statement.

currentdb.execute "delete * from tAAA where Key = 555"
me.requery

You can do this even if you're filtered on that record because it deletes from the table.
My opinion is that you could bypass the filtering alltogether...unless there's a specific reason you're using it.

J
May 19 '07 #6
OldBirdman
675 512MB
I'm not certain some of the questions asked belong on this thread. My question is that when I use the statement "DoCmd.RunCommand acCmdDeleteRecord" I get error #2046 - The command or action 'DeleteRecord' isn't available now. Either than to the statement executes, but nothing is deleted.

I added the statement "DoCmd.SetWarnings False" between lines 6 & 7 (see post #6). Although this does not work as described in Access Help, and I wasn't getting a warning anyway, it solved the problem in my test program.

Now this test program works as it should, whether or not the DoCmd.SetWarnings is present or not. Great, the "DoCmd.RunCommand acCmdDeleteRecord" works as described.

But I cannot make the same statement work outside my test environment.
May 22 '07 #7

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

Similar topics

8
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: ...
4
by: Stephen | last post by:
Hello People, Using MS Access 2003 VBA I get the error 3020 Update or CancelUpdate without AddNew or Edit when I run through the following code. Can anyone help suggest anything to try? Thanks....
3
by: Maria | last post by:
Is there another way to delete the current record in a subform from the main form, another subform or a sub-subform other than setting focus on a field in the subform and using run command...
4
by: Susan Bricker | last post by:
I have a command button on a form that is supposed to Delete the record being displayed. The record is displayed one to a form. The form is not a Pop-Up nor is it Modal. Tracing the btnDelete...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
4
by: Craggy | last post by:
Hi, I am trying to pop up a yes/no message box so that a user can delete a record in a continuous form. The default delete message is a bit sloppy because it seems to move the continuous form to...
5
WyvsEyeView
by: WyvsEyeView | last post by:
Upon clicking Delete to delete the current record on frmTopics, I want several things to happen: 1) Display a custom message rather than Access's standard "You are about to delete n records" one....
10
beacon
by: beacon | last post by:
Hi everybody, This is probably going to sound unorthodox, but I have to log records that are deleted...I know, go figure. Anyway, I have a form with a (continuous) subform, and on the subform I...
31
by: matthewslyman | last post by:
I have an unusual design and some very unusual issues with my code... I have forced Access to cooperate on everything except one issue - record deletion. My form design involves a recursively...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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
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
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...

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.