Solved

Problem with collections

Posted on 1998-07-23
5
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 50 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

615 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