Question

I need help with a recordset search criteria that is giving me a type mismatch error. The section of code below:

'Set search criteria from SO recordset

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

rsSO!Item is Text 

rsSO!Expire is Number

I tried using Variant instead of String. It did not work

Can I not use the >= in a search criteria string?

I am trying to loop through a sales order recordset and by using the item code and Expire number I want to find any available inventory in the inventory recordset and assign it to the sales order by placing it in a results table.

Getting hung up on the search criteria type mismatch.

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  
Was it helpful?

Solution

I assume expire is a date and item is a string. The only data types which do not need any type of "enclosures" are numbers. Dates need to be put in # and strings need to be wrapped in '.

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

I updated the way the dates are compared, thanks to a comment by Remou, so there is no potential ambiguity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top