目前无法进入中断模式错误 [英] Can't Enter Break Mode at this time Error

查看:85
本文介绍了目前无法进入中断模式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个模块中执行以下操作:

I am trying to do the following in one module:

    在运行时
  1. 在另一个模块中替换 Public Const .
  2. 调用使用更新后的公共常量的过程

但是,它会引发错误:

目前无法进入中断模式

尽管,它确实更新了公共常量.

Although, it does update the public constant.

我尝试按 F5 恢复执行,但没有帮助.有什么建议吗?

I tried pressing F5 to resume execution, but it did not help. Any suggestions?

Sub AB()

    Call LoadQuoteDetails2.Automation

    Application.VBE _
               .ActiveVBProject _
               .VBComponents _
               .Item("mod00Admin") _
               .CodeModule _
               .ReplaceLine 2, "Public Const QuoteDB = ""A:\1.0 Projects\P0445 Ireland Commercial Raters\02 Analysis\05 Rate Assessor\ROI Fleet Rater\Quote Database\Quote DB June 18.accdb"""

    Call CalcTariffPrem

End Sub

推荐答案

如注释中所述,您需要使用全局变量或属性而不是常量".之所以收到此时无法进入中断模式"消息,是因为您正在修改当前正在运行的 VBProject .基本上,发生的事情是您在更改 已编译的代码 的同时更改源代码. Const 被硬编码"到正在执行的过程中,因此更改它在重新编译项目之前不会做任何事情.实际上,VBA语言规范没有 Const声明的运行时语义.(为什么会这样?).例如...

As mentioned in the comments, you need to use global variable or property instead of a "constant". The reason you're getting the "Can't Enter Break Mode at this time" message is because you are modifying the VBProject that is currently running. Basically what is happening is that you are changing the source code while the already compiled code is still executing. A Const is "hard coded" into the executing procedure, so changing it won't do anything until the project is recompiled. In fact, the VBA language specification has no runtime semantics for Const Declarations (why would it?). For example...

Public Const EXAMPLE = "Foo"

Public Sub Test()
    Application.VBE.ActiveVBProject.VBComponents.Item("Module1").CodeModule.ReplaceLine 1, _
        "Public Const EXAMPLE = ""Bar"""
    Debug.Print EXAMPLE   '<-- prints Foo
End Sub

如果尝试通过调试器逐步解决此问题,则会收到相同的消息,因为VBE中的代码不再与在调试器上下文中执行的代码匹配.考虑以下代码:

If you attempt to step through this with the debugger, you'll get the same message because the code in the VBE no longer matches what is being executed in the context of the debugger. Consider the following code:

'Module1
Public Const EXAMPLE = "Bar"

Public Sub Test()
    With Application.VBE.ActiveVBProject.VBComponents.Item("Module1").CodeModule
        .DeleteLines 1, .CountOfLines
    End With
    Debug.Print "Where am I?"  '<-- this will still execute.
End Sub

如果使用 F8 逐步执行此操作,那么在删除所有代码后调试器应该突出显示哪一行?

If you step through this using F8, what line is the debugger supposed to highlight after you delete all of the code?

因此, X到您的Y 只是为了避免尝试运行自修改代码.需要更改的字符串不是常量,而是一个变量.我的建议是将其设置为属性,为其提供默认值,然后根据需要在运行时进行设置:

So, the X to your Y is to simply not attempt to run self modifying code. A string that needs to change is not a constant - it is a variable. My recommendation would be to make it a property, give it a default value, and set it at run-time as needed:

'mod00Admin
Private Const DEFAULT_DB As String = "C:\Foo\Bar.accdb"
Private activeQuoteDB As String

Public Property Let QuoteDB(rhs As String)
    activeQuoteDB = rhs
End Property

Public Property Get QuoteDB() As String
    If activeQuoteDB = vbNullString Then
        QuoteDB = DEFAULT_DB
    Else
        QuoteDB = activeQuoteDB
    End If
End Property

'...

Public Sub AB()
    LoadQuoteDetails2.Automation
    mod00Admin.QuoteDB = "A:\1.0 Projects\P0445 Ireland Commercial Raters\02 Analysis\05 Rate Assessor\ROI Fleet Rater\Quote Database\Quote DB June 18.accdb"
    CalcTariffPrem
End Sub

这篇关于目前无法进入中断模式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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