''显示错误信息 转到exit_proc 结束选择 - Tim http:/ /www.ucs.mun.ca/~tmars hal / ^ o< /#)Burp-beep,burp-beep,burp-beep? - Quaker Jake / ^^什么是UP,Dittoooooo? - 同上 From http://www.mvps.org/access/tencommandments.htm 9th item: Thou shalt not use "SendKeys", "Smart Codes" or "GoTo" (unless the GoTobe part of an OnError process) for these will lead you from the path ofrighteousness. What about also using it as a means of exiting a procedure? I''m a firm believer that exit sub and exit function should not besprinkled about tany procedure. There should one, and only one exit point. For example: Function fSilly() as Boolean ''returns true if the remarks are silly dim int1 as integer ''just to flesh out the proc On Error Goto Err_Proc int1 = forms!frmSIlly.txtSIllyIndicator if int1 = 0 then ''perform all kinds of stuff elseif int1 = 1 then GoTo Exit_Proc end if Exit_Proc: ''set database variables to nothing, etc Exit_Sub Err_Proc: Select case err.number case else ''display error message goto exit_proc end select --Tim http://www.ucs.mun.ca/~tmarshal/^o</#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake/^^ "What''s UP, Dittoooooo?" - Ditto推荐答案 " Tim Marshall" < TI **** @ PurplePandaChasers.Moertherium>在消息中写道 news:cv ********** @ coranto.ucs.mun.ca ... "Tim Marshall" <TI****@PurplePandaChasers.Moertherium> wrote in messagenews:cv**********@coranto.ucs.mun.ca...来自 http://www.mvps.org/access/tencommandments.htm 您不得使用SendKeys,智能代码和智能代码。或GoTo或(除非GoTo是OnError过程的一部分)因为这些将导致你走上正义的道路。 如何使用它作为退出程序的方法? 我坚信,退出子和退出功能不应该散布在任何程序上。应该有一个,只有一个出口点。 例如: 函数fSilly()as Boolean ''返回如果备注是愚蠢的话,那就是真的将int1作为整数''只是为了充实整个过程 On Error Goto Err_Proc int1 =表格! frmSIlly.txtSIllyIndicator 如果int1 = 0那么 ''执行各种各样的东西 elseif int1 = 1则 GoTo Exit_Proc 结束如果 Exit_Proc: ''将数据库变量设置为空,等等 Exit_Sub Err_Proc: 选择案例err.number 案例其他 ''显示错误消息 转到exit_proc 结束选择 - Tim http://www.ucs.mun.ca/~tmarshal/ ^ o< /#)" Burp-哔哔,打嗝,打嗝?&q UOT; - Quaker Jake / ^^什么是UP,Dittoooooo? - 同上 From http://www.mvps.org/access/tencommandments.htm 9th item: Thou shalt not use "SendKeys", "Smart Codes" or "GoTo" (unless the GoTo be part of an OnError process) for these will lead you from the path of righteousness. What about also using it as a means of exiting a procedure? I''m a firm believer that exit sub and exit function should not be sprinkled about tany procedure. There should one, and only one exit point. For example: Function fSilly() as Boolean ''returns true if the remarks are silly dim int1 as integer ''just to flesh out the proc On Error Goto Err_Proc int1 = forms!frmSIlly.txtSIllyIndicator if int1 = 0 then ''perform all kinds of stuff elseif int1 = 1 then GoTo Exit_Proc end if Exit_Proc: ''set database variables to nothing, etc Exit_Sub Err_Proc: Select case err.number case else ''display error message goto exit_proc end select -- Tim http://www.ucs.mun.ca/~tmarshal/ ^o< /#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake /^^ "What''s UP, Dittoooooo?" - Ditto 你可能更好地拥有自己坚持的编码规则而不是 根本没有。在您给出的示例中,您已经暗示 可能是要关闭/设置为空的对象。但我可以想象为我写的那个函数编写 代码: 如果不是IsFormLoaded(frmSilly)那么 退出功能 结束如果 我做最基本的检查并快速退出,如果这个标准不符合。我知道没有别的东西需要做,我知道函数会返回 false,所以我很乐意写它。您可能会坚持使用Goto Exit_Proc,但 在这种情况下代码的运行方式没有区别,但是在这里我会发现两种情况都可以接受。 我见过有人可能会使用blnReturn = False来启动这个功能,他们声称这可以帮助他们提供可读性,因为他们明确表示除非 更改某些内容,返回值将为false。当然,无论有没有这条线都会是 false,但有些(虽然不是我)觉得它有用。 当我们在编码约定的主题,我倾向于编写如下所示的代码:,虽然我没有看到很多其他人这样做: Exit_Proc: 错误继续下一步 如果不是第一次那么 rst.Close 设置rst = Nothing 结束如果 退出函数 也就是说,对于退出部分,代码应该做到最好清理和 退出 - 因此接下来是简历 - 否则错误会在 永久循环中向我们发送。所以我很费心去检查记录集是否是b ,但是我忽略了检查它是否可以关闭 (也许它从未打开过) ) - 我只是通过蒸汽滚动到出口处 换句话说,如果可以的话关闭它,否则继续前进。 我不知道别人怎么想这个。 Probably better to have your own rules of coding which you stick to than tohave none at all. In the example you have given, you have hinted that theremay be objects to be closed / set to nothing. But I can imagine writingcode for that function where I wrote: If Not IsFormLoaded("frmSilly") ThenExit FunctionEnd If where I do the most basic check and exit quickly if this criterion is notmet. I know that nothing else needs doing, I know the function will returnfalse and so I''m happy to write it. You might insist on Goto Exit_Proc, butit makes no difference to how the code will run in this case, but here Iwould find either case acceptable. I''ve seen people who might start the function with blnReturn=False, whichthey claim helps readability in that they make it clear that unlesssomething changes, the return value will be false. Of course it would befalse with or without that line, but some (although not me) find it helpful. While we''re on the subject of coding conventions, I tend to write code asshown below, although I have not seen many others do it: Exit_Proc:On Error Resume NextIf Not rst Is Nothing Thenrst.CloseSet rst=NothingEnd IfExit Function That is, with the exit part, the code should do it''s best to clear up andexit - hence the resume next - otherwise an error would send us round in aneternal loop. So I do bother to check whether or not the recordset isnothing or not, but I neglect to check whether it can be closed or not(perhaps it was never opened) - I just steam-roller through to the exit Inother words, close it if you can, otherwise move on. I wonder what others think of this. 2005年2月18日星期五23:10:41 +0000(UTC),Stefan Kowalski < [email protected]> 写道: 我使用比你更大的压路机。在我看来,没有 需要关于一个问题(是一个对象)是微妙的,而不是另一个(对象是开放的)微妙的b / b : Exit_Proc: On Error Resume Next rst.Close Set rst = Nothing 退出功能 -Tom。 < clip> On Fri, 18 Feb 2005 23:10:41 +0000 (UTC), "Stefan Kowalski" <[email protected]>wrote: I use an even bigger steamroller than you do. In my mind there is noneed to be subtle about one issue (is an object) while not beingsubtle about another (object is open): Exit_Proc:On Error Resume Nextrst.CloseSet rst=NothingExit Function -Tom. <clip> 虽然我们关于编码约定的问题,我倾向于编写如下所示的代码,虽然我还没有看到很多其他人这样做: Exit_Proc: On Error Resume下一步如果不是第一次就没有了 rst.Close 设置rst =什么都没有结束如果退出功能 那就是,用退出部分,代码应该做它最好清理并退出 - 因此接下来的简历 - 否则一个错误将发送我们在一个永恒的循环。所以我很费心去检查记录集是否没有,但是我忽略了检查它是否可以关闭(也许它从未打开过) - 我只是通过蒸汽滚动到了出口在换句话说,如果可以的话,请关闭它,否则继续前进。 我想知道其他人对此的看法。While we''re on the subject of coding conventions, I tend to write code asshown below, although I have not seen many others do it:Exit_Proc: On Error Resume Next If Not rst Is Nothing Then rst.Close Set rst=Nothing End If Exit FunctionThat is, with the exit part, the code should do it''s best to clear up andexit - hence the resume next - otherwise an error would send us round in aneternal loop. So I do bother to check whether or not the recordset isnothing or not, but I neglect to check whether it can be closed or not(perhaps it was never opened) - I just steam-roller through to the exit Inother words, close it if you can, otherwise move on.I wonder what others think of this. 过去使用Goto导致了如此糟糕的意大利面,以至于常见的智慧一直是为了避免使用它 - 期间。在具有足够强大的代码流控制机制的语言中,这种逻辑可能是有效的。 但是在VB / VBA中,有时代码更容易阅读/遵循 a明智地使用Goto,在这些情况下,这正是你应该使用的。 这是'我遵循的心理过程: 1.到一个我很想使用Goto的地方。 2.红旗!为什么我认为我需要goto?! 3.是否有更好的代码流控制结构? 3a。退出吗?退出? 3b。存储一个布尔状态,并在以后评估它? 3c。将代码提取到新程序,并从If块调用? 3d。将代码提取到新程序,然后使用Exit Sub / Function? 4.不 - 这些都不比我现在的要好。 5.好的 - 使用转到,并为此感到自豪。 获得5并不是特别罕见。 周五,2005年2月18日16:58:21 -0330, Tim Marshall < TI **** @ PurplePandaChasers.Moertherium>写道: The use of Goto has lead to such awful spaghetti in the past that commonwisdom has been to avoid using it - period. In a language with sufficientlypowerful code flow control mechanisms, this logic is probably valid. In VB/VBA, though, there are times when the code is easier to read/follow witha judicious use of a Goto, and in those cases, that''s exactly what you shoulduse. Here''s the mental process I follow: 1. Get to a place where I''m tempted to use a Goto.2. Red flag! Why do I think I need a goto?!3. Is there some better code flow control construct for this?3a. Exit Do? Exit For?3b. Store a boolean state, and evaluate it later?3c. Extract code to new procedure, and call from If block?3d. Extract code to new procedure, and use Exit Sub/Function?4. Nope - none of those is better than what I have now.5. OK - use the Goto, and be proud of it. Getting to 5 is not particularly rare.On Fri, 18 Feb 2005 16:58:21 -0330, Tim Marshall<TI****@PurplePandaChasers.Moertherium> wrote: 来自 http:/ /www.mvps.org/access/tencommandments.htm 第9项: 您不得使用SendKeys,智能代码和智能代码。或GoTo或(除非GoTo 成为OnError过程的一部分),这些将导致你走上正义的道路。 如何使用它作为退出程序的方法? 我坚信,退出子和退出功能不应该散布在任何程序上。应该只有一个退出点。 例如: 函数fSilly()as Boolean ''如果备注则返回true愚蠢 dim int1 as integer''只是为了充实proc On Error Goto Err_Proc int1 = forms!frmSIlly.txtSIllyIndicator 如果int1 = 0那么 ''执行各种各样的东西 elseif int1 = 1则 GoTo Exit_Proc 结束如果 Exit_Proc: ''将数据库变量设置为空,等等 Exit_Sub Err_Proc: 选择案例err.number 案例 ''显示错误消息 转到exit_proc 结束选择 From http://www.mvps.org/access/tencommandments.htm9th item:Thou shalt not use "SendKeys", "Smart Codes" or "GoTo" (unless the GoTobe part of an OnError process) for these will lead you from the path ofrighteousness.What about also using it as a means of exiting a procedure?I''m a firm believer that exit sub and exit function should not besprinkled about tany procedure. There should one, and only one exit point.For example:Function fSilly() as Boolean''returns true if the remarks are silly dim int1 as integer ''just to flesh out the proc On Error Goto Err_Proc int1 = forms!frmSIlly.txtSIllyIndicator if int1 = 0 then ''perform all kinds of stuff elseif int1 = 1 then GoTo Exit_Proc end ifExit_Proc: ''set database variables to nothing, etc Exit_SubErr_Proc: Select case err.number case else ''display error message goto exit_proc end select 这篇关于使用转到Exit_Proc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-26 17:26