传递RecordCount属性时,不会出现MsgBox

时间:2019-02-11 20:39:24

标签: vba ms-access access-vba msgbox

编辑:问题是On Error Resume Next排除了OpenRecordset中的错误,而将“变体”设置为“无”。 OpenRecordset由于列名拼写错误而失败,并且当 Type 由代理默认确定为链接表时指定了 Type

解决方案:注释掉/删除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&“ Records”)。该行之前和之后的MsgBox行显示一个MsgBox,我必须单击“确定”将其关闭。但是,此特定行不显示MsgBox,代码也不会暂停,它只是继续到下一行,就好像根本没有代码一样。

我尝试重命名查询中的表,在调用.RecordCount之前尝试了.MoveLast。即使WITH块中的MsgBox也不会显示。我希望,如果查询未返回任何结果,则在尝试显示在MsgBox中时会出现0,Null或错误,但MsgBox仍会显示并且需要用户采取措施才能继续。

0 个答案:

没有答案