Results 1 to 7 of 7
  1. #1
    Bob M is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Mar 2013
    Posts
    43

    writing contents of a collection to a new record

    Hi

    I have a table which holds several records.
    30 fields in each record are calculated every time I create a new record



    In my code I hold these values in a collection

    When I come to write a new record to the table is there a simply way of writing these 30 fields in one movement rather than specifying each of the 30 fields one at a time ?

    Bob M

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    Please tell us about the database--the table(s), relationships, forms, queries.....etc.

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,916
    A table that holds only 'several' records? Saving calculated data? 30 calculated fields? This sounds very odd.

    What do you mean by 'collection'? Not sure I've ever used. I have used arrays.

    Exactly how are you saving data? Are you opening a recordset object and setting value for each field of record? Or are you executing an INSERT action SQL?

    Post your code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    In your collection, add the corresponding table field name to each element.
    Then, using a loop, you can build an INSERT query from the collection to add the record.

  5. #5
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    Below demonstrates my suggestion in post #4 above.
    Builds 2 element collection, then parses the two fields into the table fieldname and value portions of an Insert query.
    The demo form shows each record as it is added to the table.

    davegri-InsertFromCollection.zip

    You could just as easily build 2 collections, one for the field names and one for the values, and build your query from those. That might be easier, as the field names won't change.
    In that case, you wouldn't need fcnParse.

    Second thought:
    I believe that if you are very careful, you don't need to specify the table field names in an insert query. Just list the values in the exact right order to match the order of the field names in the table.
    That way you would not need to explicitly name the fields in the query.
    That said, all you need is your existing collection to build the query. However, this will only work if your table does not have an autonumber primary key.
    Below is a example using a single element collection (just the values).

    davegri-InsertFromSingleCollection.zip
    Last edited by davegri; 01-22-2018 at 11:55 PM. Reason: additional thoughts

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,916
    Now I know what a collection looks like.

    Unless you want to reference fields by index or give them names like: Data1, Data2, Data3, etc., will have to type all 30 field names at least once somewhere. Whether to build collection, array, or a string.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    Now I know what a collection looks like.
    Arrays are so Yesterday, and Microsoft kindly gave us Collections.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Writing to a record twice
    By Thompyt in forum Programming
    Replies: 6
    Last Post: 10-19-2016, 12:00 PM
  2. Forms collection vs. AllForms collection
    By Access_Novice in forum Programming
    Replies: 1
    Last Post: 01-03-2016, 12:30 AM
  3. Writing macro to go to specific record
    By johnjmcnaughton in forum Programming
    Replies: 18
    Last Post: 04-15-2013, 10:23 AM
  4. Replies: 3
    Last Post: 11-16-2012, 10:15 AM
  5. Deleting Record Contents, not complete record...
    By Charles Waters in forum Access
    Replies: 2
    Last Post: 06-24-2008, 12:00 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums