vba 如何防止旧访问代码中的“OpenForm 操作被取消”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14129796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:40:15  来源:igfitidea点击:

How to prevent "OpenForm action was cancelled" in legacy Access Code

vbams-accessaccess-vba

提问by WildBill

I recently took over a VERY old Access database. This database has a TON of macros written that the users utilize to help them navigate and enter data. Thus, ditching the code is not step #1 despite the fact that these macros were written in Access 97....

我最近接管了一个非常旧的 Access 数据库。该数据库编写了大量宏,用户可以利用这些宏来帮助他们导航和输入数据。因此,尽管这些宏是在 Access 97 中编写的,但放弃代码并不是第 1 步。

I've managed to get most of the code to work by removing deprecated function calls with new ones, but there are a couple of forms that still do not work correctly. If I can figure out how to resolve one I'm sure I can resolve the others.

我已经通过用新的函数调用删除不推荐使用的函数调用来使大部分代码正常工作,但是有几种形式仍然无法正常工作。如果我能弄清楚如何解决一个问题,我相信我可以解决其他问题。

The main macro has a menu with several buttons. Whenever I click one button I get a "The OpenForm action was cancelled" error. The code for this button is as follows:

主宏有一个带有多个按钮的菜单。每当我单击一个按钮时,都会出现“OpenForm 操作已取消”错误。这个按钮的代码如下:

Option Compare Database   'Use database order for string comparisons

Private Sub Form_Open(Cancel As Integer)
    If IsLoaded("ServiceCircuit") Then
        Me![PropBtn].Visible = True
    Else
        Me![PropBtn].Visible = False
    End If

End Sub

Private Sub Form_Unload(Cancel As Integer)
    If IsLoaded("ServiceCircuit") Then
        Forms![ServiceCircuit].Visible = True
        Forms![ServiceCircuit]![CircuitPrefix].Requery
        Forms![ServiceCircuit]![CircuitPrefix] = Forms![Circuit]![CircuitPrefix]
        Forms![ServiceCircuit]![CircuitBase].Requery
        Forms![ServiceCircuit]![CircuitBase] = Forms![Circuit]![CircuitBase]
        'Forms!ServiceCircuit.Refresh
        Exit Sub
    Else
        If IsLoaded("DedicatedService") Then
            Forms![DedicatedService].Visible = True
            Forms!DedicatedService.Refresh
            Exit Sub
        End If

        If IsLoaded("Property") Then
            Forms![Property].Visible = True
            Forms!Property.Refresh
            Exit Sub
        End If
    End If
End Sub

Private Sub GRC_GotFocus()
    Me![GRC].Requery

End Sub

Private Sub PropBtn_Click()
    DoCmd.Close

    If IsLoaded("ServiceCircuit") Then
        Forms![ServiceCircuit].Visible = True
        DoCmd.Close
        'Forms![ServiceCircuit]![CircuitPrefix].Requery
        'Forms![ServiceCircuit]![CircuitPrefix] = Forms![Circuit]![CircuitPrefix]
        'Forms![ServiceCircuit]![CircuitBase].Requery
        'Forms![ServiceCircuit]![CircuitBase] = Forms![Circuit]![CircuitBase]
        'Forms!ServiceCircuit.Refresh
        Exit Sub
    Else
        If IsLoaded("DedicatedService") Then
            Forms![DedicatedService].Visible = True
            DoCmd.Close
            'Forms!DedicatedService.Refresh
            Exit Sub
        End If

        If IsLoaded("Property") Then
            Forms![Property].Visible = True
            Forms!Property.Refresh
            Exit Sub
        End If
    End If

End Sub

Private Sub Tariff_NotInList(NewData As String, Response As Integer)
    dumbvar = AddRecFromCombo(NewData, "Tariff")

    '  Continue without displaying default error message.
    Response = DATA_ERRCONTINUE

End Sub

I'm VERY new to VB and have been fumbling my way around it with success but I'm stumped as to how I can tackle this or what the issue could be.

我对 VB 非常陌生,并且一直在摸索着解决它并取得了成功,但我对如何解决这个问题或可能出现的问题感到困惑。

UPDATE:

更新:

I set a breakpoint in the main menu form. The code for the actual button is:

我在主菜单表单中设置了一个断点。实际按钮的代码是:

Private Sub CircuitBtn_Click()
On Error GoTo Err_CircuitBtn_Click

    Dim DocName As String
    Dim LinkCriteria As String

    DocName = "Circuit"
    DoCmd.OpenForm DocName, , , LinkCriteria

Exit_CircuitBtn_Click:
    Exit Sub

Err_CircuitBtn_Click:
    MsgBox Err.description
    Resume Exit_CircuitBtn_Click

Stepping through the DoCmd.OpenForm DocName, , , LinkCriteriacode runs and then it jumps down to Err_CircuitBtn_Click. I do not believe I can set a breakpoint elsewhere. Unsure why this is jumping to the error, the other buttons on the main form are coded the same way (on the main form that is). I tried setting some breakpoints in the first chunk of code (code for the form once it is suppose to pop up) but the breakpoint is never caught. I'm guessing that breakpoint should never be reached as that is the script that is executed if I hit a button ON that form, correct?

单步执行DoCmd.OpenForm DocName, , , LinkCriteria代码运行,然后跳转到Err_CircuitBtn_Click. 我不相信我可以在其他地方设置断点。不确定为什么会跳转到错误,主窗体上的其他按钮以相同的方式编码(在主窗体上)。我尝试在第一个代码块中设置一些断点(一旦假设弹出表单的代码),但断点从未被捕获。我猜永远不应该到达断点,因为如果我在该表单上点击按钮,就会执行该脚本,对吗?

回答by david

“OpenForm action was cancelled” is the error you get when the form ("Circuit") doesn't open correctly.

“OpenForm 操作被取消”是当表单(“Circuit”)无法正确打开时出现的错误。

That code was generated by a Wizard, and both "Docname" and "LinkCriteria" are redundant. Try it in the immediate window by entering

该代码是由向导生成的,“Docname”和“LinkCriteria”都是多余的。通过输入在立即窗口中尝试

`DoCmd.OpenForm "Circuit"`

回答by Chris Okonkwo

Just experienced the same issue and managed to fixed it.

刚刚遇到同样的问题并设法修复它。

The problem was that I originally created the form [B]by copying another form [A]. For that reason [B]had the same recordsource(even though it didn't need a recordsource) as [A].

问题是我最初是[B]通过复制另一个表单来创建表单的[A]。出于这个原因,[B]有相同的recordsource(尽管它并不需要recordsource)的[A]

I've renamed the query that was the forms' [A and B]recordsource. I made the update to [A](the one that needs a recordsource), but didn't realize that [B]also needed an update.

我重命名了表单的查询[A and B]recordsource。我对[A](需要记录源的那个)进行了更新,但没有意识到这[B]也需要更新。

The fix (for me) was to remove the recordsourcefrom [B]since the form did not need to be bound. That said, a fix for others might be to correct the recordsourceor other property values that they might have changed in the course of development.

修复(对我来说)是删除recordsourcefrom[B]因为不需要绑定表单。也就是说,对其他人的修复可能是纠正recordsource他们在开发过程中可能已更改的属性值或其他属性值。

回答by MorZa

I guess you don't need the answer anymore (It's been a while since you asked it), but I write a solution for others:

我想你不再需要答案了(你问它已经有一段时间了),但我为其他人写了一个解决方案:

On Error GoTo ErrorHandler

出错时转到 ErrorHandler

' Insert code that might generate an error here

' 在此处插入可能产生错误的代码

DoCmd.OpenQuery "query_name"

DoCmd.OpenQuery "query_name"

Exit Sub

退出子

ErrorHandler:

错误处理程序:

' Insert code to handle the error here

' 在此处插入处理错误的代码

MsgBox "The query did not run"

MsgBox "查询没有运行"

Resume Next

继续下一步