Advertisement
Guest User

Untitled

a guest
Aug 31st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Puts a single field from a recordset in a collection
  2. 'Parameters: rs: the recordset, Valname: The name or position of the field, KeyName (optional): the name or position of the field you want to use as keys in the collection
  3. 'Usage: Set MyCollection = RSToColl(MyRS, "Field1")
  4. Public Function RSToColl(rs As DAO.Recordset, Optional ValName As Variant = 0, Optional KeyName As Variant = Null) As VBA.Collection
  5.     Dim coll As New VBA.Collection
  6.     If Not rs.EOF Or rs.RecordCount > 0 Then rs.MoveFirst
  7.     Do While Not rs.EOF
  8.         If IsNull(KeyName) Then
  9.             coll.Add rs.Fields(ValName).Value
  10.         Else
  11.             coll.Add rs.Fields(ValName).Value, rs.Fields(KeyName).Value & ""
  12.         End If
  13.         rs.MoveNext
  14.     Loop
  15.     Set RSToColl = coll
  16. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement