"Delete Record" not working after adding "Edit Record" button.

M

Mike Janes

I have a form with the AllowEdits property set to "No", and the
following code attached to a command button's OnClick event. The
button is by default labeled "Edit Record", and when clicked changes to
"Save Record" allowing me to edit the form. Clicking it again locks
the form, and changes the button back to "Edit Record"

If Me.CommandButton.Caption = "Edit Record" Then
Me.AllowEdits = True
Me.CommandButton.Caption = "Save Record"
Else
If Me.Dirty Then Me.Dirty = False
Me.AllowEdits = False
Me.CommandButton.Caption = "Edit Record"
End If

The problem is, the "Delete Record" button (added using the Wizard) no
longer functions! Oddly, the "Add New Record" button DOES work (also
created using the wizard).

The form's Allow Additions and Allow Deletions properties are both set
to "Yes".

any thoughts?
 
K

keven.denen

The problem is, the "Delete Record" button (added using the Wizard) no
longer functions! Oddly, the "Add New Record" button DOES work (also
created using the wizard).

The problem is that when you set the AllowEdits property to be true,
you lose the ability to delete the record. It does not remove the
ability to add records, which is why it is acting this way. What you
will want to do is in design view, right click on the Delete Record
button and choose Build Event. You will see something like:

Private Sub Command12_Click()
On Error GoTo Err_Command12_Click

DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70

Exit_Command12_Click:
Exit Sub

Err_Command12_Click:
MsgBox Err.Description
Resume Exit_Command12_Click

End Sub

To allow your button to delete records, you need to set AllowEdits to
true, delete the record, then set AllowEdits back to false. Modify the
middle part of the code there to look like this:

Me.AllowEdits = True
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
Me.AllowEdits = False

Should do it for you.

Keven
 
M

Mike Janes

Worked like a charm! Thanks a million!

One more question, though. I've always found it annoying that when you
try to delete a record in a form, you get the standard Access "confirm
delete" dialog box, but the record in the form SWITCHES to the next
record while the dialog box is open. Is there a way to keep the
"to-be-deleted" record in the form while this dialog box is open? I'm
used to the way it is, but I'm afraid it might freak out the people who
will actually be using the database...
 
K

keven.denen

Worked like a charm! Thanks a million!
One more question, though. I've always found it annoying that when you
try to delete a record in a form, you get the standard Access "confirm
delete" dialog box, but the record in the form SWITCHES to the next
record while the dialog box is open. Is there a way to keep the
"to-be-deleted" record in the form while this dialog box is open? I'm
used to the way it is, but I'm afraid it might freak out the people who
will actually be using the database...

Sure. Before the code to delete the records, put:

application.echo false

then after the deletion is done put:

application.echo true

Essential that tells access not to update the screen until after the
deletion is done.

So in your case it would look like:

Application.Echo False
Me.AllowEdits = True
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
Me.AllowEdits = False
Application.Echo True
 
M

Mike Janes

hrm, I typed in what you said...however, if I click "No" when the
Delete Record dialog box comes up, Access freezes! any thoughts?
 
C

CompGeek78

Mike said:
hrm, I typed in what you said...however, if I click "No" when the
Delete Record dialog box comes up, Access freezes! any thoughts?

Ahh...edit the code to move the application.echo = true before the exit
sub

Private Sub Command12_Click()
On Error GoTo Err_Command12_Click
Application.Echo False
Me.AllowEdits = True
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
Me.AllowEdits = False

Exit_Command12_Click:
Application.Echo True
Exit Sub

Err_Command12_Click:
MsgBox Err.Description
Resume Exit_Command12_Click

End Sub
 
G

Guest

hello hi hi hi hi hi hi hi hihi hi hi hi hi hi hi hi hi hi hi hi
Mike Janes wrote in message
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top