MS ACCESS VBA记录集搜索条件类型不匹配

时间:2014-03-28 22:10:36

标签: vba loops ms-access access-vba recordset

我需要有关记录集搜索条件的帮助,这会给我一个类型不匹配错误。以下代码部分:

'Set search criteria from SO recordset

sCriteria = "Item = " & rsSO!Item & " And Expire >= " & rsSO!Expire

rsSO!Item is Text 

rsSO!Expire is Number

我尝试使用Variant而不是String。它不起作用

我可以不在搜索条件字符串中使用> =吗?

我正在尝试遍历销售订单记录集并使用料品代码和过期号码我想在库存记录集中找到任何可用库存,并通过将其放入结果表中将其分配给销售订单。

挂断搜索条件类型不匹配。

Public Function UpdateInventoryIntl()
    Dim rsInv As DAO.Recordset, rsSO As DAO.Recordset, rsItems As DAO.Recordset, db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim sCriteria As String
    Dim AllocationQty As Long, SaleOrderRemainder As Long
    Set db = CurrentDb

            'Inventory by LOT FIFO through [Expire] ASC
            '========================================== 
            Set rsInv = CurrentDb.OpenRecordset( _
            "SELECT * FROM [tbl_InventoryAvailForIntl] ORDER BY [Item] DESC,[Expire] ASC", _
            dbOpenDynaset)

            'Need to add expiry date requirement to SO table by item by geo
            '===============================================================
            Set rsSO = CurrentDb.OpenRecordset("SELECT * FROM [tbl_IntlAllocated] ORDER BY [Item] DESC,[Due_Date] ASC ,[Expire] DESC", _
            dbOpenDynaset)


    Do Until rsSO.RecordCount = 0

    'Set search criteria from SO recordset
    '===================================== 
    sCriteria = "Item = " & rsSO!Item & " And Expire >= " & rsSO!Expire

    'Find first SO criteria in the Inventory recordset
    '=================================================
    rsInv.FindFirst (sCriteria)

    If rsInv.NoMatch Then
    'Delete SO because there is no inventory and go to next SO
    rsSO.Delete
    rsSO.MoveNext

    Else
        AllocationQty = IIf(rsSO!Qty_Open >= rsInv!QOH_IntlAllocation, rsInv!QOH_IntlAllocation, rsSO!Qty_Open)

        db.Execute ("INSERT INTO tbl_IntlAllocatedResults (Due_Date, Sale_Order_Num, SO_Line, Item, Qty_OpenStart, Location, Lot, QtyAllocated) " & _
        "VALUES (#" & rsSO!Due_Date & "#,'" & rsSO!Sale_Order_Num & "'," & rsSO!SO_Line & ",'" & rsSO!Item & "'," & rsSO!Qty_OpenStart & ",'" & rsInv!Location & "','" & rsInv!Lot & "'," & AllocationQty & ");")

        rsSO.Edit
        rsSO!Qty_Open = rsSO!Qty_Open - AllocationQty
        rsSO.Update

        If rsSO!Qty_Open = 0 Then
        rsSO.Delete
        rsSO.MoveNext
        End If

        rsInv.Edit
        rsInv!QOH_IntlAllocation = rsInv!QOH_IntlAllocation - AllocationQty
        rsInv.Update

        If rsInv!QOH_IntlAllocation = 0 Then
        rsInv.Delete
        rsInv.MoveNext
            If rsSO.RecordCount = 0 Then
            Exit Do
            End If
                If rsInv!Item <> rsSO!Item Then
                Debug.Print rsSO!Item
                db.Execute ("DELETE tbl_IntlAllocated.* FROM tbl_IntlAllocated WHERE Item = '" & rsSO!Item & "';")
                Set rsSO = CurrentDb.OpenRecordset("SELECT * FROM [tbl_IntlAllocated] ORDER BY [Item] DESC,[Due_Date] DESC", _
                dbOpenDynaset)
                End If
        End If
    End If

    Loop

    rsSO.Close
    Set rsSO = Nothing
    Set qdf = Nothing
    rsInv.Close
    Set rsInv = Nothing

End Function  

1 个答案:

答案 0 :(得分:3)

我认为expire是一个日期而item是一个字符串。唯一不需要任何类型“附件”的数据类型是数字。日期需要放在#中,字符串需要包含在'中。

sCriteria = "Item = '" & rsSO!Item & "' And Expire >= #" & Format(rsSO!Expire, "yyyy-mm-dd") & "#"

由于Remou的评论,我更新了比较日期的方式,因此没有潜在的歧义。