Solved

VBA Run-time error 2046 'DeleteRecord'

Posted on 2011-02-15
7
1,765 Views
Last Modified: 2012-05-11
I just converted a 3-yr old Access 2003 database to 2010 for testing.  I have a piece of VBA code I use often that checks temporary tables to make sure they were properly emptied after use and, if not, deletes all the records so they are ready for the next use.

In the 2003 version, the code looked like this and worked without any problems:

Dim db As Database
Dim table_RST As Recordset
Set db = CurrentDb
Set table_RST = db.OpenRecordset ("tbl_TempTable", dbOpenDynaset)

If (table_RST.RecordCount > 0) Then
  table_RST.MoveFirst
  Do While (Not table_RST.EOF)
    DoCmd.Delete
    table_RST.MoveNext
  Loop
End If

In Access 2010, I now get a error that the "DoCmd.Delete" command is not vaild.  So, I did a little looking around and tried replacing that line with the following two lines:

DoCmd.RunCommand acCmdSelectRecord
DoCmd.RunCommand acCmdDeleteRecord

When I try to run this, I get a Run-time error 2046 saying that the "command or action 'DeleteRecord' isn't available now."

All my forms are unbound and I use VB Scripting almost exclusively to move data from tables to forms, so I don't think it is a record lock problem.  But, I don't know how to correct it.  Any suggestions?  Thanks!
0
Comment
Question by:jmcclosk
  • 3
  • 2
  • 2
7 Comments
 
LVL 75
ID: 34898854
That code does not even compile in A2003, so I do see how it ever worked.

Delete is not a Method of DoCmd.

I think you need

table_RST.Delete

mx
0
 
LVL 28

Expert Comment

by:omgang
ID: 34898911
I agree with 'MX but why are you enumerating the recordset if you simply want to delete all the records.  For that matter, why even check if the table contains records first, simply execute a delete query (DELETE * FROM tbl_TempTable).
OM Gang
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 250 total points
ID: 34899014
Yes ... if your goal is to delete all records, all you need is one line of code:


    CurrentDb.Execute "DELETE tbl_TempTable.* FROM tbl_TempTable", dbFailOnError


mx
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

Author Comment

by:jmcclosk
ID: 34899035
What a bonehead I am!  I copied some code to another form and didn't select everything, so I typed the rest from memory.  Obviously, my memory is not so good.  DatabaseMX got me back on track.  

However, omgang raises a good point, which may be a new question to write up.  I taught myself VBA.  I have no doubt "roughed out" some things that can be done better.  I tried entering that command you gave me, although it looks more like SQL than VBA and I got a compile error.  How could i wipe out all records in the table with a single command instead of going one by one?
0
 
LVL 28

Expert Comment

by:omgang
ID: 34899061
See 'MX's second post.  That's the command you need.
OM Gang
0
 

Author Closing Comment

by:jmcclosk
ID: 34899069
Thank you for updating my self-learned skills to be more efficient!
0
 
LVL 28

Expert Comment

by:omgang
ID: 34899072
You're only a bonehead if you don't continue to learn and improve.
OM Gang
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now