Solved

The Command or Action "DeleteRecord" is not  available now

Posted on 2004-09-01
15
581 Views
Last Modified: 2012-05-05
I have a form  ( Access 2003)where if the User makes a mistake or does not want to  save a record  he gets an option to exit without saving the record.

The Code in this part is as follows:
If IsNull(Me.City) Or Me.City = "" Then
        msg = "You Must Enter a Contact City" & Chr(13) & "Do you want to exit without Saving this record"
        style = vbYesNo
        response = MsgBox(msg, style)
        If response = vbYes Then GoTo DeleteRecord
        If response = vbNo Then City.Locked = False: City.SetFocus: Exit Sub
        End If

DeleteRecord:
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.Close acForm, "ContactData"

Clicking on Yes   to action - DoCmd.RunCommand acCmdDeleteRecord produces  a Runtime Error 2046
The Command or Action "DeleteRecord" is not  available now.

This happens if it is a new record or an existing record.

The Allowdeletions Property is set to Yes and there are no fields in the Table that are required except the Record ID

Can any expert give me a pointer to what is going wrong??

With Thanks

Rick

 



Exit Sub
0
Comment
Question by:Aerocom
  • 6
  • 4
  • 3
  • +2
15 Comments
 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 125 total points
Comment Utility
Don't delete it ... just do an Undo:

If IsNull(Me.City) Or Me.City = "" Then
  If MsgBox("You Must Enter a Contact City" & Chr(13) & "Do you want to exit without Saving this record"), vbYesNo)<>    vbYes Then
  Me.Undo
  DoCmd.Close acForm, "YourFormName"
  End If
End If
0
 
LVL 1

Author Comment

by:Aerocom
Comment Utility
When the user goes to Add a new record and puts in any data a new record is created. ( The RecordID field shows a new Autonumber)
 IRickf the data is wrong  or there is some missing data, then the record needs to be deleted.
0
 
LVL 1

Author Comment

by:Aerocom
Comment Utility
What doe Me.Undo Do ??
0
 
LVL 15

Expert Comment

by:will_scarlet7
Comment Utility
If me.undo does not work, change your DeleteRecord section to something like this:

DeleteRecord:
Me.City = "Delete Me!"
CurrentDB.Execute "DELETE YourTable.* FROM YourTable WHERE (((YourTable.City)='Delete Me!'));"
DoCmd.Close acForm, "ContactData"
0
 
LVL 15

Expert Comment

by:will_scarlet7
Comment Utility
Sorry, I think that may have to be rearranged:

DeleteRecord:
Me.City = "Delete Me!"
DoCmd.Close acForm, "ContactData"
CurrentDB.Execute "DELETE YourTable.* FROM YourTable WHERE (((YourTable.City)='Delete Me!'));"
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
Comment Utility
Me.Undo undoes (oddly enough) any changes made to the data by the user.
0
 
LVL 1

Author Comment

by:Aerocom
Comment Utility
I have tried

DeleteRecord:
Me Undo
DoCmd.Close acForm, "ContactData"

instead of

DeleteRecord:
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.Close acForm, "ContactData"

This works for a new record - The record is not added to the table and the form closes without error. So we are half the way there

The problem still lies in the abiliy to delete an existing record when viewing a saved record in the table.

me.undo does not seem to work on this as it only clears any entry/edits done on the form

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 41

Expert Comment

by:shanesuebsahakarn
Comment Utility
> The problem still lies in the abiliy to delete an existing record when viewing a saved record in the table.

Are you sure you want to do this? If a user goes to an existing record without a city, then clicks yes to exit without saving it will delete that existing record!
0
 
LVL 1

Author Comment

by:Aerocom
Comment Utility
The City field is only one of many  fields in an old database table ( started in Access 2 12 years ago) that may be empty

The aiim is to get users either to update the records fields  such as City, phone Number and contact name or to decide to delete the contact. record completly. That way the database eventually get cleaned up - either missing data is provided or the record get deleted.

It used to work OK in Access97 the problems only occur now the DB is in Access 2002/3
0
 
LVL 41

Expert Comment

by:shanesuebsahakarn
Comment Utility
Hmm, ok, in that case I'd use a method similar to will_scarlet7's - what is the name of your primary key field, and what data type is it?
0
 
LVL 1

Author Comment

by:Aerocom
Comment Utility
The Primary key is ContactID - its an automumber
0
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 125 total points
Comment Utility
Something like this:
If IsNull(Me.City) Or Me.City = "" Then
        msg = "You Must Enter a Contact City" & Chr(13) & "Do you want to exit without Saving this record"
        style = vbYesNo
        response = MsgBox(msg, style)
        If response = vbYes Then GoTo DeleteRecord
        If response = vbNo Then City.Locked = False: City.SetFocus: Exit Sub
        End If

DeleteRecord:
Me.Dirty=False
CurrentDb.Execute "DELETE * FROM tblContactData WHERE ContactID=" & Me!txtContactID
DoCmd.Close acForm, "ContactData"

I've assumed that the table is called tblContactData and the ContactID is in a textbox called txtContactID.
0
 
LVL 15

Expert Comment

by:will_scarlet7
Comment Utility
Aerocom,
Any progress?
0
 
LVL 1

Author Comment

by:Aerocom
Comment Utility
Thanks gentlmen for your help.
Sorry to have been so long getting back but I was away on other business

Rick
0
 

Expert Comment

by:Texrat
Comment Utility
I'm still curious as to why the error even occurs.

I have a situation similar to Aerocom's only a bit more complicated.  I have an Admin form that may load any of a number of subforms that the user may manipulate.  I would like to have a single delete key on the Master form (instead of duplicating the object and code on each loaded form) that would delete the current record of the subform regardless of which form it is hosting at the time.  I also get that "Error 2046" with no further explanation available.

???
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

762 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

6 Experts available now in Live!

Get 1:1 Help Now