Article ID: 182945
Article Last Modified on 1/9/2003
MyCollection.Add MyRecordset![LastName]This same line of code in Visual Basic 5.0 or later adds a reference to the field as a whole to the collection rather than the contents of the field. The Visual Basic 5.0 or later equivalent to the above line of code would be:
MyCollection.Add MyRecordset![LastName].ValueTherefore the programmer should be explicit in determining what is added to a collection.
Private Sub Form_Load()
Dim MyDatabase As Database
Dim MyRecordset As Recordset
Dim CollectionItem As Variant
Dim MyCollection As New Collection
'Open the database and recordset
Set MyDatabase = OpenDatabase("C:\VB\NWIND.MDB")
Set MyRecordset = _
MyDatabase.OpenRecordset("SELECT * FROM EMPLOYEES")
'Add last name of each employee to the collection
While Not MyRecordset.EOF
MyCollection.Add MyRecordset![LastName]
MyRecordset.MoveNext
Wend
'Display each item in collection
For Each CollectionItem In MyCollection
Debug.Print CollectionItem '<- Generates an Error
Next
'Close the recordset and database
MyRecordset.Close
MyDatabase.Close
End Sub
Additional query words: kbDAO kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport
Keywords: kbprb KB182945