Results 1 to 7 of 7
  1. #1
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182

    VBA - Insert Same Values from Multiple Fields in the Previous Record

    In Access, [Ctrl]+['] inserts the value from the same field in the previous record. I find this shortcut useful and I use it often. However, I want to be able to press a button on my form and have Access automatically “insert the value from the multiple specified fields in the previous record.” How can I accomplish this using VBA?

  2. #2
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    If this happens frequently, then the easiest route is to set the default value to the current value on that group of controls when you press your button to got to the new record.

    Add a Tag value to the controls you want to duplicate. Then loop through those tagged controls setting the default value.
    When you then got to a new record those fields will be completed with the previous records values.

    If you need help with that shout back here.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Quote Originally Posted by Minty View Post
    If this happens frequently, then the easiest route is to set the default value to the current value on that group of controls when you press your button to got to the new record.

    Add a Tag value to the controls you want to duplicate. Then loop through those tagged controls setting the default value.
    When you then got to a new record those fields will be completed with the previous records values.

    If you need help with that shout back here.
    This is not something that happens frequently. May once every three weeks do I need something like this. That is why I specifically need a button.

  4. #4
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Ok, setting them as default values is probably a bit overkill if it's once a month.
    Doing it that way would only be for as long as you have the form open though, it's not a permanent change, so may still be the most efficient way to achieve the end goal.

    How many fields are you talking about?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Quote Originally Posted by Minty View Post
    Ok, setting them as default values is probably a bit overkill if it's once a month.
    Doing it that way would only be for as long as you have the form open though, it's not a permanent change, so may still be the most efficient way to achieve the end goal.

    How many fields are you talking about?
    About 15 fields

  6. #6
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You'll need to test this as it's air code(pretty untested).

    On each control that you want to duplicate set the Tag property to Duplicate
    On your button to copy the previous record add the following code;
    Code:
    
    Dim ctrl As Control
    
    'Loop through controls and get current values into defaults values 
        For Each ctrl In Me.Controls
            If ctrl.Tag = "Duplicate" Then
                ctrl.DefaultValue = "'" & ctrl.Value & "'"
            End If
        Next
    
     DoCmd.GoToRecord , , acNewRec  ' This should now populate the new record with the values from the existing record
    
    
    
    I'm pretty sure this will fail on checkboxes, so don't include them.
    Closing the form should reset the default values back to their original settings provided you don't save form changes.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  7. #7
    zashaikh is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Jan 2017
    Posts
    182
    Thank you!!! I think that did it. I really apprecaite it

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

Similar Threads

  1. Replies: 3
    Last Post: 12-02-2017, 02:04 PM
  2. Replies: 6
    Last Post: 07-01-2015, 10:56 AM
  3. Replies: 7
    Last Post: 01-12-2015, 03:47 PM
  4. Replies: 5
    Last Post: 01-03-2014, 02:07 AM
  5. Replies: 8
    Last Post: 08-02-2012, 08:48 AM

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