I'm writing a function that checks for items received at zero cost. I have a SQL recordset to get the list of receipts to check. I want to loop through it and check the cost of each item received. If the item being checked has a nonzero cost, then move on and check the next item. If any item is found to have a zero cost, I want to immediately exit the loop and the function and return "false" to the calling program. If all items are found to have a nonzero cost, I want to return "true" to the calling program.
This is my function. I have one record in my recordset. It has a nonzero cost. I would have expected the loop to execute once, then exit and return true just before the 2nd time around because we should now be at the end of the recordset. Instead, it keeps looping and I can't figure out why.
Instead of the Do While Not... Loop construct, I have also tried Do... Loop While Not and Do .... Loop Until with no luck. What am i missing here?
dim cn, rstItems, strConnectionString, strSQLInstanceName, strDatabaseName, strSQL
set cn = createObject("ADODB.Connection")
strConnectionString = "Provider=SQLOLEDB;Data Source=" & strSQLInstanceName & ";Persist Security Info=False;Initial Catalog=" & strDatabaseName & ";Integrated Security=SSPI"
' get list of items to check
strSQL = "SELECT Job, StockCode " & _
"FROM dbo.ItemReceipts " & _
"WHERE Job = '" & Form.Job & "' And RecType = 'I' " & _
"GROUP BY Job, StockCode"
Set rstItems = CreateObject("ADODB.Recordset")
If rstItems.EOF Then
msgbox "No records in the recordset."
dim ItemOK, strWH, strStockCode
strWH = "XXX"
strStockCode = rstItems.Fields("StockCode").Value
Do While NOT rstItems.EOF
' exit loop if Item is found to have 0 cost
If GetUnitCost(strWH, strStockCode) = 0 Then
ItemOK = false
' Close & release the recordset
set rstItems = nothing
IsItemCostNonZero = ItemOK