使用指定索引

时间:2017-09-03 08:58:11

标签: ms-access indexing recordset

这是我第一次问一个问题,虽然我多年来在这里发现了许多问题的解决方案。

我有一个令人沮丧的问题,我无法找到答案。我希望在读取之前通过使用表中定义的特定索引名称打开的动态集DAO记录集打开的链接表进行初始读取。

我的代码在.index行上返回错误3251'不支持操作...'。毫无疑问,有一个明显的解决方案(请注意,我正试图避免打开一个明显答案的SQL查询。)

Public Function IOrdCustomerStock(CustomerID As Long, ProductID As Long, KeepRsOpen As Boolean, Optional UseIndexName As String) As Boolean

    Set zcls_CS.CS_rs = CurrentDb.OpenRecordset(Name:=CS_TableName, Type:=RecordsetTypeEnum.dbOpenDynaset)
    With zcls_CS.CS_rs
        If Not IsMissing(UseIndexName) Then
            .Index = UseIndexName
        End If
        .FindFirst "CS_CustomerID = " & CustomerID & " and CS_ProductID = " & ProductID
        If .NoMatch Then
            zcls_CS.CS_EOF = True
        Else
            zcls_CS.CS_EOF = False
            zcls_CS.CS_ID = .Fields("[ID]")
            zcls_CS.CS_CustomerID = .Fields("[CS_CustomerID]")
            zcls_CS.CS_PhysSalesStock = .Fields("[CS_PhysSalesStock]")
            zcls_CS.CS_ProductID = .Fields("[CS_ProductID]")
            zcls_CS.CS_PurQuantityRecvd = .Fields("[CS_PurQuantityRecvd]")
            zcls_CS.CS_PurUnitDesc = .Fields("[CS_PurUnitDesc]")
            zcls_CS.CS_PurUnitFactor = .Fields("[CS_PurUnitFactor]")
            zcls_CS.CS_SaleQuantityAlloc = .Fields("[CS_SaleQuantityAlloc]")
            zcls_CS.CS_SaleQuantityOrdered = .Fields("[CS_SaleQuantityOrdered]")
            zcls_CS.CS_SaleUnitDesc = .Fields("[CS_SaleUnitDesc]")
            zcls_CS.CS_SaleUnitFactor = .Fields("[CS_SaleUnitFactor]")
        End If
    End With
    If Not KeepRsOpen Then
        Call IOclCustomerStock
    End If

    IOrdCustomerStock = Not zcls_CS.CS_EOF

End Function

1 个答案:

答案 0 :(得分:0)

一旦我将问题限制在链接表中,我发现了以下帖子:

https://social.msdn.microsoft.com/Forums/office/en-US/d402a8d2-0771-458c-b57e-09e2d6f0c536/trying-to-open-a-linked-table-whats-going-on?forum=accessdev

我不会假装了解OpenDatabase参数,但它可以正常工作。我只需要将通常的错误处理添加到我的小概念证明中:

Public Function IOksInitRsIX1CustomerStock(UseIndexName As String, CustomerID As Long, ProductID As Long) As Boolean
Set zcls_CS.CS_rs = OpenDatabase(Mid(DBEngine(0)(0).TableDefs(CS_TableName).Connect, 11)).OpenRecordset(CS_TableName)
With zcls_CS.CS_rs
    zcls_CS.CS_rs.Index = UseIndexName
    If (CustomerID > 0 And ProductID > 0) Then
        .Seek "=", CustomerID, ProductID
    Else
        If CustomerID > 0 Then
            .Seek "=", CustomerID
        End If
    End If
    If .NoMatch Then
        zcls_CS.CS_EOF = True
    Else
        zcls_CS.CS_EOF = False
        zcls_CS.CS_ID = .Fields("[ID]")
        zcls_CS.CS_CustomerID = .Fields("[CS_CustomerID]")
        zcls_CS.CS_PhysSalesStock = .Fields("[CS_PhysSalesStock]")
        zcls_CS.CS_ProductID = .Fields("[CS_ProductID]")
        zcls_CS.CS_PurQuantityRecvd = .Fields("[CS_PurQuantityRecvd]")
        zcls_CS.CS_PurUnitDesc = .Fields("[CS_PurUnitDesc]")
        zcls_CS.CS_PurUnitFactor = .Fields("[CS_PurUnitFactor]")
        zcls_CS.CS_SaleQuantityAlloc = .Fields("[CS_SaleQuantityAlloc]")
        zcls_CS.CS_SaleQuantityOrdered = .Fields("[CS_SaleQuantityOrdered]")
        zcls_CS.CS_SaleUnitDesc = .Fields("[CS_SaleUnitDesc]")
        zcls_CS.CS_SaleUnitFactor = .Fields("[CS_SaleUnitFactor]")
    End If
End With

IOksInitRsIX1CustomerStock = Not zcls_CS.CS_EOF

结束功能