我该如何删除记录? [英] How can i delete a record?

查看:53
本文介绍了我该如何删除记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我应该用来删除记录的代码

没有消息框说你即将删除记录,是

你确定......弹出。


目前我正在使用代码:


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


任何想法可以帮助我

谢谢

I would like to know the code that i should use to delete a record
without the message box saying "You are about to delete a record, are
you sure..." poping up.

At the moment i am using the code:

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

Any ideas to help me
thanks

推荐答案

尝试这样的事情:


如果Me.Dirty那么

Me.Undo

结束如果

如果不是Me.NewRecord那么

DoCmd.SetWarnings False

RunCommand acCmdDeleteRecord

DoCmd.SetWarnings True

结束如果


该代码:

- 如果编辑是明确撤消记录正在进行中;

- 检查是否有要删除的记录;

- 抑制警告;

- 删除记录;

- 再次打开警告。


从表单'RecordsetClone中删除记录也会删除它而不确认

。如果在删除发生时

表单可能没有焦点,这也是一种更好的方法。


-

Allen Browne - 微软MVP。西澳大利亚州珀斯。

访问用户提示 - http:// allenbrowne.com/tips.html

回复群组,而不是mvps dot org的allenbrowne。


" MartinR" < ma ********* @ esbi.iewrote in message

news:11 ********************* *@75g2000cwc.googlegro ups.com ...
Try something like this:

If Me.Dirty Then
Me.Undo
End If
If Not Me.NewRecord Then
DoCmd.SetWarnings False
RunCommand acCmdDeleteRecord
DoCmd.SetWarnings True
End If

That code:
- explicitly undoes the record if an edit is in progress;
- checks that there is a record to delete;
- suppresses warnings;
- deletes the record;
- turns the warnings back on again.

Deleting the record from the form''s RecordsetClone also deletes it without
confirmation. This is also a much better approach if there is any chance the
form may not have focus at the time the deletion occurs.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"MartinR" <ma*********@esbi.iewrote in message
news:11**********************@75g2000cwc.googlegro ups.com...

>我想知道我应该用来删除记录的代码

没有消息框说你即将删除一条记录,是

你确定......弹出。


目前我正在使用代码:


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


任何想法可以帮助我

谢谢
>I would like to know the code that i should use to delete a record
without the message box saying "You are about to delete a record, are
you sure..." poping up.

At the moment i am using the code:

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

Any ideas to help me
thanks



我使用了那个代码并且它有效,但我有另一个问题。在我想要删除记录之后,我希望表格自动关闭

。但是这种情况没有发生,在记录被删除之后

没有显示任何警告,我收到一条信息消息,说没有当前的

记录。然后我必须按好。当我按下确定时,该消息消失了

,但我仍然打开表格。


这是我正在使用的代码:


Private Sub Back_Click()

错误GoTo Err_Back_Click

Const MB_OK = 0,MB_OKCANCEL = 1''定义按钮。

Const MB_YESNOCANCEL = 3,MB_YESNO = 4

Const MB_ICONSTOP = 16,MB_ICONQUESTION = 32''定义图标。

Const MB_ICONEXCLAMATION = 48,MB_ICONINFORMATION = 64

Const MB_DEFBUTTON2 = 0,IDYES = 6,IDNO = 7''定义其他。


Title ="退出而不添加图像

DgDef = MB_YESNO + MB_ICONSTOP + MB_DEFBUTTON2

响应= MsgBox(你确定要退出而不添加

图像吗?,DgDef,标题)


如果响应= IDYES那么


DoCmd.SetWarnings False

RunCommand acCmdDeleteRecord

DoCmd.Close acForm

DoCmd.SetWarnings True

否则


结束如果


Exit_Back_Click:

退出Sub


Err_Back_Click:

MsgBox Err.Description

恢复退出_Back_Click


结束子

i used that codeand it works but i''m having another problem. After i
want the record to be deleted, i want the form to be closed
automatically. But this is not happening, after the record is deleted
with no warnings showing, i get an info message saying "No current
record" and then i have to press ok. when i press ok, that message goes
away but i am still left with the form open.

Here is the code i am using:

Private Sub Back_Click()
On Error GoTo Err_Back_Click
Const MB_OK = 0, MB_OKCANCEL = 1 '' Define buttons.
Const MB_YESNOCANCEL = 3, MB_YESNO = 4
Const MB_ICONSTOP = 16, MB_ICONQUESTION = 32 '' Define icons.
Const MB_ICONEXCLAMATION = 48, MB_ICONINFORMATION = 64
Const MB_DEFBUTTON2 = 0, IDYES = 6, IDNO = 7 '' Define other.

Title = "Exit without adding image"
DgDef = MB_YESNO + MB_ICONSTOP + MB_DEFBUTTON2
Response = MsgBox("Are you sure you want to exit without adding
an image?", DgDef, Title)

If Response = IDYES Then

DoCmd.SetWarnings False
RunCommand acCmdDeleteRecord
DoCmd.Close acForm
DoCmd.SetWarnings True
Else

End If

Exit_Back_Click:
Exit Sub

Err_Back_Click:
MsgBox Err.Description
Resume Exit_Back_Click

End Sub


要关闭表单,请将此行添加到过程的结尾:

DoCmd.Close acForm,Me.Name


无当前记录除非有一些其他

代码尝试对不存在的记录执行某些操作,否则不应发生错误。它会很好地跟踪它并避免错误。


如果你不能这样做,暂时禁用错误处理程序,通过添加a / $
第二行的单引号,即:

''On Error GoTo Err_Back_Click

当它以No Current Record失败时;,从对话框中写下错误号码




然后删除该单引号(以恢复错误处理程序)并更改

代码:

Err_Back_Click:

MsgBox Err.Description

恢复Exit_Back_Click

to:

Err_Back_Click:

如果Err.Number< xxxx那么

MsgBox Err.Description

结束如果

恢复退出_Back_Click


替换你写下的数字而不是xxxx。


BTW,看起来很旧的代码。如果您使用的是Access 95或更高版本,

您可以使用内置常量,例如:

vbOk

vbOkCancel


示例:

如果MsgBox(你确定吗?vbYesNo + vbDefaultButton2 + vbCritical)=

vbYes那么


-

Allen Browne - 微软MVP。西澳大利亚州珀斯。

访问用户提示 - http:// allenbrowne.com/tips.html

回复群组,而不是mvps dot org的allenbrowne。


" MartinR" < ma ********* @ esbi.iewrote in message

news:11 ********************* *@p79g2000cwp.googlegr oups.com ...
To close the form, add this line to the end of the procedure:
DoCmd.Close acForm, Me.Name

The "No Current Record" error should not occur unless there is some other
code that is trying to do something with the non-existent record. It would
be good to track that down and avoid the error.

If you can''t do that, temporarily disable the error handler, by adding a
single quote to the 2nd line, i.e.:
''On Error GoTo Err_Back_Click
When it fails with the "No Current Record", write down the error number from
the dialog.

Then remove that single quote (to reinstate the error handler) and change
the code:
Err_Back_Click:
MsgBox Err.Description
Resume Exit_Back_Click
to:
Err_Back_Click:
If Err.Number <xxxx Then
MsgBox Err.Description
End If
Resume Exit_Back_Click

Substitue the number you wrote down instead of xxxx.

BTW, that looks like fairly old code. If you are using Access 95 or later,
you could use the built in constants such as:
vbOk
vbOkCancel

Example:
If MsgBox("Are you sure?" vbYesNo + vbDefaultButton2 + vbCritical) =
vbYes Then

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"MartinR" <ma*********@esbi.iewrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...

>我使用了该代码并且它有效,但我有另一个问题。在我想要删除记录之后,我希望表格自动关闭

。但是这种情况没有发生,在记录被删除之后

没有显示任何警告,我收到一条信息消息,说没有当前的

记录。然后我必须按好。当我按下确定时,该消息消失了

,但我仍然打开表格。


这是我正在使用的代码:


Private Sub Back_Click()

错误GoTo Err_Back_Click

Const MB_OK = 0,MB_OKCANCEL = 1''定义按钮。

Const MB_YESNOCANCEL = 3,MB_YESNO = 4

Const MB_ICONSTOP = 16,MB_ICONQUESTION = 32''定义图标。

Const MB_ICONEXCLAMATION = 48,MB_ICONINFORMATION = 64

Const MB_DEFBUTTON2 = 0,IDYES = 6,IDNO = 7''定义其他。


Title ="退出而不添加图像

DgDef = MB_YESNO + MB_ICONSTOP + MB_DEFBUTTON2

响应= MsgBox(你确定要退出而不添加

图像吗?,DgDef,标题)


如果响应= IDYES那么


DoCmd.SetWarnings False

RunCommand acCmdDeleteRecord

DoCmd.Close acForm

DoCmd.SetWarnings True

否则


结束如果


Exit_Back_Click:

退出Sub


Err_Back_Click:

MsgBox Err.Description

恢复Exit_Back_Click

结束子
>i used that codeand it works but i''m having another problem. After i
want the record to be deleted, i want the form to be closed
automatically. But this is not happening, after the record is deleted
with no warnings showing, i get an info message saying "No current
record" and then i have to press ok. when i press ok, that message goes
away but i am still left with the form open.

Here is the code i am using:

Private Sub Back_Click()
On Error GoTo Err_Back_Click
Const MB_OK = 0, MB_OKCANCEL = 1 '' Define buttons.
Const MB_YESNOCANCEL = 3, MB_YESNO = 4
Const MB_ICONSTOP = 16, MB_ICONQUESTION = 32 '' Define icons.
Const MB_ICONEXCLAMATION = 48, MB_ICONINFORMATION = 64
Const MB_DEFBUTTON2 = 0, IDYES = 6, IDNO = 7 '' Define other.

Title = "Exit without adding image"
DgDef = MB_YESNO + MB_ICONSTOP + MB_DEFBUTTON2
Response = MsgBox("Are you sure you want to exit without adding
an image?", DgDef, Title)

If Response = IDYES Then

DoCmd.SetWarnings False
RunCommand acCmdDeleteRecord
DoCmd.Close acForm
DoCmd.SetWarnings True
Else

End If

Exit_Back_Click:
Exit Sub

Err_Back_Click:
MsgBox Err.Description
Resume Exit_Back_Click
End Sub



这篇关于我该如何删除记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆