Results 1 to 4 of 4
  1. #1
    runner231 is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jun 2018
    Posts
    6

    How to auto create a row in a table based on a field in another table

    Hi



    I’ve 2 tables Table A and Table B.

    Table A has field SalesOrderID, TotalAmt, CustomerName

    Table B has field SalesOrderID,PaymentAmt, PaymentDate.

    Qn:
    Is there a way to auto create a row in Table B with the SalesOrderID being populated whenever a new row is being created in Table A?

    Table A
    SalesOrderID TotalAmt CustomerName
    123782 $1000 Alan
    285123 $2000 Steven

    Table B
    SalesOrderID PaymentAmt PaymentDate
    123782 $500 23/May/2018
    285123 $100 26/May/2018

    Suppose a new record is being created in Table A (user input these values in a Form)
    SalesOrderID = 674235; TotalAmt = $2200; CustomerName = David

    Table A
    SalesOrderID TotalAmt CustomerName
    123782 $1000 Alan
    285123 $2000 Steven
    674235 $2200 David

    Table B
    SalesOrderID PaymentAmt PaymentDate
    123782 $500 23/May/2018
    285123 $100 26/May/2018
    674235 $0 23/Jun/2018

    Then I want a new row in Table B with SalesOrderID 674235 being insert automatically (without user manually input 674235 in a form or directly in Table B). PaymentAmt and PaymentDate will take the default value when the new row is first being created.

    Can this be done? Thanks.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Can be done with code. But why would you want a payment record if there is no payment?
    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.

  3. #3
    runner231 is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Jun 2018
    Posts
    6
    Quote Originally Posted by June7 View Post
    Can be done with code. But why would you want a payment record if there is no payment?
    How can it be done? Can guide me? Thank you.

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    Add this code to the form that adds records to table A.
    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub Form_AfterInsert()
        Dim sSQL As String
            sSQL = "Insert Into tblB (SalesOrderID,PaymentAmt,PaymentDate) " _
            & "Values (" & TempVars!tvSalesOrderID & ", " & 0 & ", #" & Date & "#);"
            Debug.Print sSQL
            CurrentDb.Execute sSQL, dbFailOnError
        TempVars.Remove "tvSalesOrderID"
    End Sub
    
    '---------------------------------------------------------------------------------------
    ' Method : SalesOrderID_AfterUpdate
    ' Author : davegri
    ' Date   : 6/23/2018
    ' Purpose: Two things here:
    ' 1. Check to see if the SalesOrderID already exists in tblA, if so warn and no update.
    ' 2. The values in the form are not available after the insert into tblA, so save the value
    '    of SalesOrderID in a tempvar as soon as it is entered into the form.
    '---------------------------------------------------------------------------------------
    Private Sub SalesOrderID_AfterUpdate()
        Dim nID As Long
        nID = Nz(DLookup("SalesOrderID", "tblA", "SalesOrderID=" & [SalesOrderID]), 0)
        Select Case nID
            Case 0
                TempVars!tvSalesOrderID = SalesOrderID.Value
                Exit Sub
            Case Else
                MsgBox "SalesOrderID already exists", vbOKOnly, "    D A T A   E N T R Y   W A R N I N G   "
                Me.Undo
        End Select
    End Sub
    Last edited by davegri; 06-23-2018 at 01:22 PM. Reason: added code to prevent dupes

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

Similar Threads

  1. Replies: 3
    Last Post: 09-05-2016, 10:56 AM
  2. Replies: 3
    Last Post: 06-04-2015, 09:48 AM
  3. How to auto-populate a field based on a query on another table
    By lissy_vincent@yahoo.com in forum Forms
    Replies: 2
    Last Post: 02-17-2014, 04:08 PM
  4. Replies: 3
    Last Post: 12-24-2013, 04:20 PM
  5. Replies: 5
    Last Post: 01-20-2011, 11:36 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