Results 1 to 4 of 4
  1. #1
    kmrickert is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2021
    Posts
    10

    Question Inserting Record Using Last ID of another Table

    I'm trying to do a vba insert - where the newest "LOAN_ID" of a table called "LOANS" Is inserted into a table called "PAYMENTS" along with the current date being inserted into a "DATE_OF_PAYMENT" field also in the PAYMENTS table. Here's what I have:

    '------------------------------------------------------------
    ' NEW_LOAN
    '
    '------------------------------------------------------------
    Function NEW_LOAN()
    On Error GoTo NEW_LOAN_Err


    With CodeContextObject
    If (.ORIGINAL_AMOUNT > 0) Then
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.RefreshRecord
    Beep
    MsgBox "Saved Loan", vbInformation, "LOAN SAVED"
    DoCmd.RunCommand acCmdRefresh
    Else
    DoCmd.RefreshRecord
    Beep
    MsgBox "Loan amount is missing.", vbExclamation, "ENTER AMOUNT:"
    End If
    DoCmd.Close acForm, "New Loan"


    Dim lastID As Integer
    strSQL = "INSERT INTO PAYMENTS (LOAN_ID, DATE_OF_PAYMENT) VALUES (lastID, DATE())"


    DoCmd.RunSQL (strSQL)
    lastID = DMax("[LOAN_ID]", "LOANS")

    End With




    NEW_LOAN_Exit:
    Exit Function


    NEW_LOAN_Err:
    MsgBox Error$
    Resume NEW_LOAN_Exit


    End Function

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,158
    That looks very convoluted.
    And all a bit back to front. You are trying to include LastID in your SQL insert but aren't setting it's value until after the insert command ? ?

    If the form you are closing has the New Loan ID on it simply get it from the form rather than relying on a DMax.
    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
    kmrickert is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2021
    Posts
    10
    Okay, I'm trying that now. Could you please tell me how I can specify the form in the INSERT statement?

  4. #4
    kmrickert is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2021
    Posts
    10
    Figured it out. Thanks!

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

Similar Threads

  1. Replies: 8
    Last Post: 04-30-2020, 07:59 AM
  2. Why inserting new record is not working?
    By jaryszek in forum Access
    Replies: 4
    Last Post: 04-20-2018, 04:01 AM
  3. Replies: 4
    Last Post: 07-21-2015, 02:50 PM
  4. Syntax error when inserting new record
    By The in forum Queries
    Replies: 10
    Last Post: 11-20-2014, 07:53 PM
  5. Replies: 14
    Last Post: 03-19-2013, 12:16 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