|
-
Feb 2nd, 2000, 08:31 AM
#1
Thread Starter
Junior Member
Hi All,
I have a problem looping thru a collection. Can anyone please help me out with this......
I have this following code.....
Public mcolContacts As New Collection
Public Sub Add(CustomerID As Long, SiteName As String, ContactName As String)
Dim udtContact As ContactType
Dim key As String
With udtContact
.CustomerID = CustomerID
.SiteName = SiteName
.ContactName = ContactName
.Selected = False
End With
key = "CID:" & CustomerID
End Sub
Now I have 2 conditions
1)I want to remove the item if it already exists and add the same item with the updated value OR
2)Add a new item if it is not present in the collection.
The code for this is code is as follows
if key exists then
mcolContacts.Remove key mcolContacts.Add udtContact, key
Else
mcolContacts.Add udtContact, CStr(key) End If
-
Feb 2nd, 2000, 08:51 AM
#2
I think you can update my answer to a previous post to do what you want.
------------------
Marty
HASTE CUISINE
Fast French food.
[This message has been edited by MartinLiss (edited 02-02-2000).]
-
Feb 2nd, 2000, 09:25 AM
#3
Thread Starter
Junior Member
Hi Martin,
I did go thru your code before I posted this topic, but I was not able to relate that with my problem becos here I'm using a user defined datatype for the items in the collection. Can U please brief me about that has to be done with respect to the code I have given in here.
Thanks in advance.
-
Feb 3rd, 2000, 01:46 AM
#4
Frenzied Member
Have you tried a regular loop through the collection? Here is a function that might work for you. You pass it the user defined type, and it returns TRUE if it is in the collection, or FALSE if not:
Code:
Private Function ExistsInColl(udtContact As Contact) As Boolean
Dim nCounter As Integer
ExistsInColl = False
For nCounter = 0 To mcolContacts.Count - 1
If udtContact.CustomerID = mcolContacts(nCounter).CustomerID Then
ExistsInColl = True
Exit Function
End If
Next nCounter
End Function
-
Feb 3rd, 2000, 08:47 AM
#5
Well you learn something all the time. I worked on it a while and it turns out you can't add a udt to a collection. However, if you are willing to parse a string later on when to want to read the data in the collection then you can do this. 
Code:
Public MyCollection As New Collection
Private Sub Command1_Click()
Dim intIndex As Integer
' This is just test data, and obviously you wouldn't want to
' hard-code the data values. I assume instead that the data would
' come from TextBoxes, etc. and you would only try to add/update
' one record at a time.
If RecordExits(111, "site 1", "Marty") Then
MsgBox "Record replaced"
Else
MsgBox "record added"
End If
MsgBox MyCollection.Count
If RecordExits(111, "site 1", "New Name") Then
MsgBox "Record replaced"
Else
MsgBox "record added"
End If
MsgBox MyCollection.Count
If RecordExits(222, "site 2", "Liss") Then
MsgBox "Record replaced"
Else
MsgBox "record added"
End If
MsgBox MyCollection.Count
For intIndex = 1 To MyCollection.Count
Debug.Print MyCollection.Item(intIndex)
Next
End Sub
Public Function RecordExits(CustomerID As Long, SiteName As String, ContactName As String) As Boolean
Const SEPERATOR = ";" ' Use whatever seperator that would NEVER be in your
' data. You may need to validate the input beforehand
' to make sure it's not present
On Error Resume Next
' Try to add the record
MyCollection.Add CustomerID & SEPERATOR & SiteName & SEPERATOR & ContactName, CStr(CustomerID)
If Err = 0 Then
' OK, it's added
RecordExits = False
Else
RecordExits = True
' Remove the old record
MyCollection.Remove CStr(CustomerID)
' Add the updated record
MyCollection.Add CustomerID & SEPERATOR & SiteName & SEPERATOR & ContactName, CStr(CustomerID)
End If
End Function
------------------
Marty
HASTE CUISINE
Fast French food.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|