Solved

Problem with collections

Posted on 1998-07-23
5
174 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 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel object stays open 19 76
VBA error replacing data 6 40
bit defender blocks good applications 2 94
Adding to a VBA? 6 70
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

777 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