Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jul 2022
    Posts
    16

    DMAX previous record

    I am have one large database and a few pop up forms. I am wanting to go to one of my pop of forms on a new record and lookup 5 fields from the previous record and have them copied over with a click of a button. I have a table with the name Order Tracker and I have an ID field that auto updates on each record. I tried this but its not working. Can someone guide me?




    Private Sub Copy_Last_Record_Click()

    Dim ID As Long

    ID = Dmax ("ID", "Order Tracking")
    DoCmd.GoToRecord , , acPrevious
    Contract_Signer_Title = DLookup("Contract_Signer_Title", "Order Tracking", "ID=")
    Contract_Signer_First_Name = DLookup("Contract_Signer_First_Name", "Order Tracking", "ID=" & ID)

    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    Dmax does not locate a record, merely returns a value?

    You would need to locate that record with that Dmax ID, and only then move previous.

    However if table is not sorted asc by ID, that is never going to work?

    You also have not included the ID value for the first Dlookup() ?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Join Date
    Jul 2022
    Posts
    16
    The table is sorted by ID. So is it possible to copy certain fields from the most recent record and fill out vs having to retype? I am placing orders and sometimes I get three separate orders from the same customer however their local contact is all the same, so I just want to hit copy and it fills it out for me.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    The way I did it years ago, was to set the default value for the control from the current value, as I had lots of entries for the same date, ship and rank
    Code:
    Private Sub Date_ID_AfterUpdate()
        If Me.chkCopy Then
            Me![Date_ID].DefaultValue = """" & Me![Date_ID].Value & """"
        Else
            Me![Date_ID].DefaultValue = 0
        End If
    End Sub
    Because of that, I had a checkbox to indicate whether I should copy previous value or not.

    http://allenbrowne.com/ser-24.html
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Another way to do it would be to set the default value of those 5 fields as the value of the previous record, many examples in the forums:
    https://www.access-programmers.co.uk...fields.307738/
    https://www.access-programmers.co.uk...-value.120910/
    https://www.access-programmers.co.uk...record.287145/ <------this one has code similar to yours

    To do it how you started (with a button) please review this updated code:

    EDITED CODE BELOW - added square brackets to table name in the two dLookups:

    Code:
    Dim lngID As Long
    
    
    lngID = Dmax ("ID", "[Order Tracking]","[ID]<" & Me.ID) 'assumes ID is an autonumber and already created for the current new record
    'DoCmd.GoToRecord , , acPrevious 'not needed
    Me.Contract_Signer_Title = DLookup("Contract_Signer_Title", "[Order Tracking]", "ID=" & lngID) 'Contract_Signer_Title is the name of the textbox control bound to the Contract_Signer_Title field in [Order Tracking] table
    Me.Contract_Signer_First_Name = DLookup("Contract_Signer_First_Name", "[Order Tracking]", "ID=" & lng)
    'add your other fields using a similar syntax)
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    Join Date
    Jul 2022
    Posts
    16
    This helps. let me try these out. Thank you.

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Please see the revised code, initially I forgot to add the brackets to the dLookups (you shouldn't have spaces in any objects names).

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  8. #8
    Join Date
    Jul 2022
    Posts
    16
    Quote Originally Posted by oldmanbumper View Post
    This helps. let me try these out. Thank you.
    I tried this but its not working: Is what I did right?

    Private Sub Copy_Last_Record_Click()

    Dim lngID As Long

    lngID = DMax("ID", "[Order Tracking]", "[ID]<" & Me.ID)
    Me.Contract_Signer_Title = DLookup("Contract_Signer_Title", "[Order Tracking]", "ID=" & lngID)
    Me.Contract_Signer_First_Name = DLookup("Contract_Signer_First_Name", "[Order Tracking]", "ID=" & lng)

    End Sub

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,920
    "not working" means what - error message, wrong result, nothing happens?

    You show variable lng in the last DLookup instead of lngID.

    Do you have Option Explicit in module header? Review https://chandoo.org/forum/threads/ho...default.32072/
    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.

  10. #10
    Join Date
    Jul 2022
    Posts
    16
    Nothing happens. The fields just stay blank. I checked record 72 and its filled out. When I go to record 73 and hit "copy last record" button, with the code provided, it does nothing.


    Private Sub Copy_Last_Record_Click()

    Dim lngID As Long

    lngID = DMax("ID", "[Order Tracking]", "[ID]<" & Me.ID)
    Me.Contract_Signer_First_Name = DLookup("Contract_Signer_First_Name", "[Order Tracking]", "ID=" & lng)
    Me.Contract_Signer_Last_Name = DLookup("Contract_Signer_Last_Name", "[Order Tracking]", "ID=" & lng)

    End Sub

  11. #11
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,920
    Again, you declare variable lngID but use lng.

    Did you read the thread I linked?
    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.

  12. #12
    Join Date
    Jul 2022
    Posts
    16
    I updated it to the below code and it still does not work. I did read the link to send my database, I have to delete alot of info but I can do that then upload it.

    Private Sub Copy_Last_Record_Click()

    Dim lngID As Long

    lngID = DMax("ID", "[Order Tracking]", "[ID]<" & Me.ID)
    Me.Contract_Signer_First_Name = DLookup("Contract_Signer_First_Name", "[Order Tracking]", "ID=" & lngID)
    Me.Contract_Signer_Last_Name = DLookup("Contract_Signer_Last_Name", "[Order Tracking]", "ID=" & lngID)

    End Sub

  13. #13
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,920
    I meant the link in post 9 about using Option Explicit in modules - not the link in my post signature. But providing db would be my next suggestion.
    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.

  14. #14
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    Start walking through your code with F8.
    See the debugging link in my signature.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  15. #15
    Join Date
    Jul 2022
    Posts
    16

    Database attached

    here is the database. If you go to the Contact Info form you will see the copy button. Thank you for your help.
    Attached Files Attached Files

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 12-15-2018, 09:11 AM
  2. Replies: 8
    Last Post: 01-10-2016, 11:51 AM
  3. Replies: 1
    Last Post: 04-11-2015, 10:31 AM
  4. DMAx Question for Report - DMax <= Dtae
    By crimedog in forum Access
    Replies: 8
    Last Post: 12-29-2014, 09:31 PM
  5. Replies: 6
    Last Post: 07-24-2012, 03:02 PM

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