Solved

Problem with collections

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft Access combo box help 2 57
VBA Shell can't Find Word document 11 155
Send outlook email from VBS Script 2 67
Outlook Macro to delete emails not meeting keywords 56 119
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

710 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