2014-03-28 73 views
1

我需要記錄集搜索條件的幫助,該條件會給我一個類型不匹配錯誤。下面的代碼段:MS ACCESS VBA記錄集搜索條件類型不匹配

'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 

回答

3

我認爲expire是一個日期和item是一個字符串。唯一不需要任何類型的「機箱」的數據類型是數字。需要將日期放入#,並且需要將字符串包裝在'中。

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

由於Remou的評論,我更新了日期的比較方式,所以沒有潛在的歧義。

+1

將日期格式設置爲yyyy-mm-dd以避免出現語言環境問題可能是個想法。 – Fionnuala

+0

@Remou你會格式化? SQL字段的記錄集字段?或兩者? 「CDate」能解決這種問題嗎?是不是locale問題僅僅是因爲'rsSO!Expire'會在連接之前被轉換爲字符串,但不是如何將這個轉換形成爲區域設置的函數?它不會選擇正確嗎? – Brad

+1

是的,rsSO!Expire會被轉換爲一個字符串,所以如果你的語言環境在美國之外,你可能會得到'dd/mm/yyyy',這是行不通的,Access需要明確的日期和年份,月份,一天是相當明確的。如果您使用'&format(rsSO!Expire,「yyyy/mm/dd」)&'或'&format(rsSO!Expire,「yyyy-mm-dd」)&',您將會安全。 – Fionnuala