My code wont work Why?

T

Tony Williams

I have this code behind a command button but it doesn't delete the record.
If vbNo = MsgBox("Are you sure you want to save this new record?", 4) Then
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
If vbYes = MsgBox("Do you want to input another record?", 4) Then
[DocNametxt].SetFocus
End If
DoCmd.Close

I've also tried
DoCmd.RunCommand acCmdDeleteRecord
But this didn't work either
Anyone help me with why?
TIA
Tony Williams
 
R

Rick Brandt

Tony Williams said:
I have this code behind a command button but it doesn't delete the record.
If vbNo = MsgBox("Are you sure you want to save this new record?", 4) Then
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
If vbYes = MsgBox("Do you want to input another record?", 4) Then
[DocNametxt].SetFocus
End If
DoCmd.Close

I've also tried
DoCmd.RunCommand acCmdDeleteRecord
But this didn't work either
Anyone help me with why?
TIA

If it's a new record that has never been saved then it cannot be deleted
(it doesn't exist in the table yet). Instead you should issue a Me.Undo to
cancel the creation of the record in the first place.
 
T

Tony Williams

Thanks Rick. Although it is a new record I am working on a form with two
subforms and as soon as the focus goes from the main form to the subform I
understand from the newsgroups (I am a newbie at VBA) that the record is
automatically saved so by the time the user clicks the close button it isn't
a new record so Me.Undo wont work. Does that make sense?
Tony

Rick Brandt said:
Tony Williams said:
I have this code behind a command button but it doesn't delete the record.
If vbNo = MsgBox("Are you sure you want to save this new record?", 4) Then
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
If vbYes = MsgBox("Do you want to input another record?", 4) Then
[DocNametxt].SetFocus
End If
DoCmd.Close

I've also tried
DoCmd.RunCommand acCmdDeleteRecord
But this didn't work either
Anyone help me with why?
TIA

If it's a new record that has never been saved then it cannot be deleted
(it doesn't exist in the table yet). Instead you should issue a Me.Undo to
cancel the creation of the record in the first place.
 
R

Rick Brandt

Tony Williams said:
Thanks Rick. Although it is a new record I am working on a form with two
subforms and as soon as the focus goes from the main form to the subform I
understand from the newsgroups (I am a newbie at VBA) that the record is
automatically saved so by the time the user clicks the close button it isn't
a new record so Me.Undo wont work. Does that make sense?
Tony

I'm not sure then. You might try issuing a save first.

Me.Dirty = False

It's possible delete will fail any time the record is dirty, not just
before initial creation.
 
T

Tony Williams

Thanks Rick I tried another way. I used the wizard to create a delete button
and then used that code to build what I wanted step by step. so far so good!
Thanks for your input
Tony
 

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

Similar Threads


Top