Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

The Command or Action "DeleteRecord" is not  available now

Posted on 2004-09-01
15
Medium Priority
?
629 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +2
15 Comments
 
LVL 85

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 375 total points
ID: 11955505
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
ID: 11955753
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
ID: 11955756
What doe Me.Undo Do ??
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 15

Expert Comment

by:will_scarlet7
ID: 11955922
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
ID: 11955938
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
ID: 11955962
Me.Undo undoes (oddly enough) any changes made to the data by the user.
0
 
LVL 1

Author Comment

by:Aerocom
ID: 11956412
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
 
LVL 41

Expert Comment

by:shanesuebsahakarn
ID: 11956511
> 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
ID: 11956927
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
ID: 11957000
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
ID: 11959959
The Primary key is ContactID - its an automumber
0
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 375 total points
ID: 11959994
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
ID: 12008322
Aerocom,
Any progress?
0
 
LVL 1

Author Comment

by:Aerocom
ID: 12102433
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
ID: 13331267
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

721 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