Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

The Command or Action "DeleteRecord" is not  available now

Posted on 2004-09-01
15
Medium Priority
?
632 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

885 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