Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with collections

Posted on 1998-07-23
5
Medium Priority
?
182 Views
Last Modified: 2010-05-03
I have a collection that I'm adding items to these items are base off a database recordset.  The problem that I'm having is that each time I add a new item to the collection it adds successfully, but it also overwrites all previous items in the collection with the new information and I have now clue why.
Here is the function that adds the items to the collection:

Public Function GetHistory(ByVal Town As String, ByVal Map As String, ByVal Lot As String, Optional ByVal Unit As String) As Collection
  'returns a collection of all the Sales Transaction for the Property
  Dim colHistory As New Collection  'The Collection
  Dim hisItem As New cHistoryItem   'The item
  Dim rs As New ADODB.Recordset     'The recordset
  Dim cn As New ADODB.Connection    'The connection to the database
 
  'open the database
  cn.Open "FILEDSN=Assessor"
  If Len(Unit) Then

    rs.Open "SELECT [ID],[Town],[Owner],[SaleDate],[SalePrice],[DeedNo],[Book],[Page] FROM [History] WHERE [Map] = '" & Map & "' AND [Lot] = '" & Lot & "' AND [Unit] = '" & Unit & "' AND [Town] = " & Town & " ORDER BY [SaleDate]", cn, adOpenForwardOnly, adLockReadOnly

  Else

    rs.Open "SELECT [ID],[Town],[Owner],[SaleDate],[SalePrice],[DeedNo],[Book],[Page] FROM [History] WHERE [Map] = '" & Map & "' AND [Lot] = '" & Lot & "' AND [Town] = " & Town & " ORDER BY [SaleDate]", cn, adOpenForwardOnly, adLockReadOnly

  End If

  'loop throught the returned recordset
  Do While Not rs.EOF

    'clear old values
    hisItem.Clear

    'assign new values
    hisItem.Owner = rs.Fields("Owner")
    hisItem.SaleDate = rs.Fields("SaleDate")
    hisItem.SalePrice = rs.Fields("SalePrice")
    hisItem.Book = rs.Fields("Book")
    If Not IsNull(rs.Fields("Page")) Then hisItem.Page = rs.Fields("Page")
    If Not IsNull(rs.Fields("DeedNo")) Then hisItem.DeedNo = rs.Fields("DeedNo")
    hisItem.Town = rs.Fields("Town")

    'add item to the collection
    colHistory.Add hisItem, CStr(rs.Fields("ID"))

    'move to the next item in the recordset
    rs.MoveNext
  Loop
  Set GetHistory = colHistory

  'cleanup resources
  rs.Close
  Set rs = Nothing
End Function
0
Comment
Question by:clsmaster
  • 3
5 Comments
 
LVL 1

Expert Comment

by:wizard2072098
ID: 1466553
Try it without putting the key value in (CStr(rs.Fields("ID"))) during the add. I know it sounds strange, but your code looks fine. Nothing glaringly wrong pops up. See if the values will go into the collection without a key, or if you end up with the same problem.
0
 
LVL 1

Accepted Solution

by:
Anita030598 earned 200 total points
ID: 1466554
You are using one instance of hisItem object. So, whenever you change information in hisItem object, all items in the collection will have the latest information.

Try this.

Change the declaration,

Dim hisItem As New cHistoryItem      to

Dim hisItem As cHistoryItem

Change hisItem.Clear statement within Do While Loop to

Set hisItem = New cHistoryItem

At the end of the DO Loop, ie., just before the Loop Statement add the line

Set hisItem = Nothing.

0
 
LVL 1

Expert Comment

by:Anita030598
ID: 1466555
I mean the line,

Set hisItem = Nothing

Not Set hisItem = Nothing. ( NO . )

0
 
LVL 1

Expert Comment

by:Anita030598
ID: 1466556
I mean the line,

Set hisItem = Nothing

Not Set hisItem = Nothing. ( NO . )

0
 

Author Comment

by:clsmaster
ID: 1466557
That did it thanks

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question