Checks if an item was found in list of items and return 1 if yes, 0 if not found.
If not found, it can add that missing item to list if caller sets AddItemIfNotFound to 1.
Uses ANString style, where ANStrList is list of strings with defined separator which will be returned as well updated with new item.
Edit 2024-05-31: Adding fix to issue of false positive match when we have only 1 item.
Function ANStrFind_AndOrAdd(ThisItem, ByRef ANStrList, Optional AddItemIfNotFound = 1, Optional Sepa = "|")
' Checks if an item was found in list of items and return 1 if yes, 0 if not
' With option to add the new item to list if not found
'
' Set AdditIfNotFound to 1 if yo uwant tfunction to add this item to list if it was not found
' Needs CutString3
Rett = 0
ItemID = 1
If UCase(Trim(ThisItem)) = UCase(ANStrList) Then
Rett = 1
GoTo SingleItemList
End If
Do
Cut3 = CutString3(ANStrList, ItemID, Sepa)
If Cut3 = "" Or Cut3 = ANStrList Then Exit Do
If IsNumeric(ThisItem) Then
If Val(ThisItem) = Val(Cut3) Then
Rett = 1
Exit Do
End If
Else
If UCase(Trim(ThisItem)) = UCase(Trim(Cut3)) Then
Rett = 1
Exit Do
End If
End If
ItemID = ItemID + 1
Loop
If Rett = 0 And AddItemIfNotFound = 1 Then
If ANStrList > "" Then ANStrList = ANStrList & Sepa
ANStrList = ANStrList & ThisItem
'Rett = ANStrList
End If
SingleItemList:
ANStrFind_AndOrAdd = Rett
End Function
' Checks if an item was found in list of items and return 1 if yes, 0 if not
' With option to add the new item to list if not found
'
' Set AdditIfNotFound to 1 if yo uwant tfunction to add this item to list if it was not found
' Needs CutString3
Rett = 0
ItemID = 1
If UCase(Trim(ThisItem)) = UCase(ANStrList) Then
Rett = 1
GoTo SingleItemList
End If
Do
Cut3 = CutString3(ANStrList, ItemID, Sepa)
If Cut3 = "" Or Cut3 = ANStrList Then Exit Do
If IsNumeric(ThisItem) Then
If Val(ThisItem) = Val(Cut3) Then
Rett = 1
Exit Do
End If
Else
If UCase(Trim(ThisItem)) = UCase(Trim(Cut3)) Then
Rett = 1
Exit Do
End If
End If
ItemID = ItemID + 1
Loop
If Rett = 0 And AddItemIfNotFound = 1 Then
If ANStrList > "" Then ANStrList = ANStrList & Sepa
ANStrList = ANStrList & ThisItem
'Rett = ANStrList
End If
SingleItemList:
ANStrFind_AndOrAdd = Rett
End Function
ThisItem, ByRef ANStrList, Optional AddItemIfNotFound = 1, Optional Sepa = "|"
Views 281
Downloads 115
CodeID
DB ID