首页 > 解决方案 > 传递 RecordCount 属性时 MsgBox 不出现

问题描述

编辑:问题是On Error Resume Next抑制中的错误OpenRecordset,将 Variant 设置为 Nothing。OpenRecordset由于列名拼写错误而失败,并且在默认情况下由作为链接表的代理确定类型时指定类型。

解决方案:注释掉/删除On Error Resume Next以引发错误。更正列名中的拼写错误。从链接表查询时不要指定类型(将位置留空)(对于来自链接表的查询,类型自动为动态集。

在代码的特定部分中,我试图显示记录集中的记录数(主要用于调试目的)。我偶然发现了我认为是一个独特的错误。代码如下:

specsVariableTempTableName = "specs_variable_" & CStr(Me.OpFormOpRecordID) & "_temp"

specItemsQuery = "SELECT [Item], [Document #], [Item Description], [SubItems], [Attribute], [Spec Characteristics] FROM [tbl_forms] WHERE [Document #] = '" & Me![Form #] & "' AND [Part #] = '" & Me![Part Number] & "';"

Set db_source = CurrentDb

If IsNull(DLookup("Name", "MSysObjects", "Name='" & specsVariableTempTableName & "' And Type In (1,4,6)")) Then
        'Check for existing temporary spec table
        'Create one if not
        MsgBox ("No temporary table " & specsVariableTempTableName)
        Set specItemsRecordSet = db_source.OpenRecordset(specItemsQuery, , dbFailOnError).Clone

        MsgBox (specItemsRecordSet.RecordCount & " Records")
        MsgBox ("What the f***")


        If Not (specItemsRecordSet.EOF And specItemsRecordSet.BOF) Then
            'If the result set is not empty, go to first entry
            specItemsRecordSet.MoveFirst
            With specItemsRecordSet
                MsgBox (.RecordCount)
            End With
        Else
            MsgBox ("No Specs found for given part number and document number")
        End If


        db_source.Execute "SELECT [Document #], [Part #], [ParentItem], [SubItem], [Nominal], [LSL], [USL], [UOM] INTO " & specsVariableTempTableName & " FROM [Specs_Variable] WHERE (([Specs_Variable]![Document #] = '" & Me![Form #] & "') AND ([Specs_Variable]![Part #] = '" & Me![Part Number] & "'));", dbFailOnError
    End If

有问题的行是 MsgBox(specItemsRecordSet.RecordCount & "Record")。此行之前和之后的 MsgBox 行显示一个 MsgBox,我必须单击 OK 关闭它。但是,这一行没有显示 MsgBox,代码没有暂停,它只是继续到下一行,就好像代码根本不存在一样。

我试图重命名查询中的表,我在调用 .RecordCount 之前尝试过 .MoveLast。甚至 WITH 块中的 MsgBox 也不显示。我希望如果查询没有返回任何结果,我会在尝试在 MsgBox 中显示时得到 0、Null 或错误,但 MsgBox 仍会显示并需要用户操作才能继续。

标签: ms-accessvbamsgbox

解决方案


推荐阅读