Results 1 to 2 of 2
  1. #1
    elios115 is offline Novice
    Windows 7 Access 2003
    Join Date
    Jul 2008
    Posts
    5

    primary key at add new record time

    dear all,

    i have a database with 2 tables say tb1 and tb2.
    tb1 with primary key tb1ID
    tb2 is linked to tb1 with tb1ID

    at some point i need to add a record to the table of tb1 using recordset.addnew(VBA)
    and assign the primary key of the newly added record to the field tb1Id in tb2 to link them.
    but im gettin the error that the tb1ID not found at creation time.

    is there anyway to retrieve the newly added primary key and assign it to the field in tb2?



    thx hope i ll get some help

  2. #2
    jzwp11 is offline VIP
    Windows 7 Access 2007
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    Here is a code snippet that might help; I use a variable hdpkAddressID to hold the primary key when the record is create.

    Code:
     With myRSAddress
                .AddNew
                !txtAddress = Me.txtAddress
                !fkStreetTypeID = Me.cboStreet
                !txtApt = Me.txtApt
                !fkCityStateID = Me.cboCity
                !txtZip = Me.txtZip
                hdpkAddressID = !pkAddressID
                .Update
            End With

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

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2010, 03:52 PM
  2. Primary Key Issue
    By Ciwan in forum Access
    Replies: 5
    Last Post: 10-23-2009, 10:04 PM
  3. Primary Key Help
    By phoenix13 in forum Access
    Replies: 4
    Last Post: 07-30-2009, 12:36 PM
  4. about the primary key!
    By Yuesko in forum Access
    Replies: 1
    Last Post: 05-29-2009, 04:20 PM
  5. reset primary key
    By emilylu3 in forum Access
    Replies: 1
    Last Post: 12-09-2005, 03:27 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