1

我选择DoCmd.RunSQL了一个特定的功能,而不是CurrentDb.Execute因为我想利用 Access 的内置警告来让用户选择回滚查询。但是,当我今天运行它时,我注意到它不再显示警告。当我编写程序时,我几乎可以肯定它确实如此。

我已经尝试过明确设置DoCmd.SetWarnings True,即使没有理由一开始就SetWarnings应该关闭,但这并不能解决问题。有谁知道问题可能是什么?

我的代码在下面(为了节省空间,稍微编辑了一下)。作为参考,这是上下文菜单上的一项功能,它记录当前记录的字段值,并将记录集中的所有记录更新为相同的值。它生成的查询是正确的,并且可以正确执行——但它不会首先显示警告消息。

Public Function FillFoundRecords()
    On Error GoTo ErrHandler

    Dim controlField As String, idName As String, _
        commandText As String, whereClause As String

    'First save the current record
    DoCmd.RunCommand acCmdSaveRecord

    controlField = Screen.ActiveControl.ControlSource

    With Screen.ActiveForm
        'Check for any where clauses or filters on the active form
        whereClause = GetWhereClause(.RecordSource)

        'Build the UPDATE command. This uses a few custom functions and properties, 
        'but it's not the problem -- it always produces the correct query statement.
        commandText = _
          "UPDATE " & .BaseTable & _
          " SET [" & controlField & "] = " & ActiveControlFilter & _
          " WHERE " & whereClause

        DoCmd.SetWarnings True
        DoCmd.RunSQL commandText
        .Refresh
    End With

ExitHandler:
    On Error Resume Next 'Ignore further errors
    Exit Function

ErrHandler:
    If Err.Number = 2501 Then 'RunSQL command was cancelled; ignore this
        Resume ExitHandler
    Else
        ErrorHandle
        Resume ExitHandler
    End If
End Function
4

3 回答 3

3

也许您不久前对警告感到恼火并禁用了它们?在选项菜单中(我认为是在高级选项卡下),有 3 个切换:

确认记录更改
确认文档更改
确认操作查询

这些是否未经检查?

于 2013-04-11T18:02:59.763 回答
2

在您报告的评论中,您现在收到了您想要的确认消息DoCmd.RunSQL。不幸的是,我们仍然不了解他们的暂时失踪。

但是,如果您依赖这些确认消息,请注意每个用户都可以根据自己的偏好 设置高级选项“确认操作查询” ,您的应用程序将尊重她的偏好。

因此,如果您想要这些确认消息,请修改您的应用程序以确保设置是您需要的。

Application.SetOption "Confirm Action Queries", True

然后,该更改将在 Access 会话中持续存在。为避免惹恼您的用户,您可以在应用程序启动时检查设置...

? Application.GetOption("Confirm Action Queries")

...保存它,并在关机时再次使用Application.SetOption.

于 2013-04-11T18:51:19.037 回答
1

我已经很长时间DoCmd.SetWarnings没有使用了,但是值得以下代码确实会引发警告,就像我双击操作查询时一样:

Sub queryTest()
Dim cdb As DAO.Database, qdf As QueryDef
Set cdb = CurrentDb
On Error Resume Next
DoCmd.DeleteObject acQuery, "zzzTempQuery"
On Error GoTo queryTest_Error
Set qdf = cdb.CreateQueryDef("zzzTempQuery", "UPDATE Clients SET LastName=""Thompson"" WHERE ID=25")
qdf.Close
Set qdf = Nothing
Set cdb = Nothing
DoCmd.SetWarnings True
DoCmd.OpenQuery "zzzTempQuery", acViewNormal
DoCmd.Close acQuery, "zzzTempQuery"
DoCmd.DeleteObject acQuery, "zzzTempQuery"
Exit Sub

queryTest_Error:
If Err.Number = 2501 Then
    '' user cancelled - just exit
Else
    Err.Raise Err.Number
End If
End Sub

编辑

嗯,我认为它可能与DoCmd.OpenQuery,但这个变体也引发了一个警告:

DoCmd.SetWarnings True
DoCmd.RunSQL "UPDATE Clients SET LastName=""Thompson"" WHERE ID=25"
于 2013-04-11T18:17:10.933 回答