Results 1 to 8 of 8
  1. #1
    zkyknight is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Jan 2019
    Posts
    7

    Trouble in getting PK(AutoNumber) value of a new record.

    Hello,

    I have a button that copies current record and paste them into a new record. I am trying to keep the relationship that these records have with other tables. For example, the copied record is of a policy and has a relationship with a location. I have the policy copied, but want to add the location and connect it to the new policy.

    I was using the DoCmd commands to copy and paste the record and whenever I am trying to get the new Policy ID, it came back as a Long instead of an Integer. I need the Integer value so I can continue on with my SQL inserts for the location.

    I have tried using Max(ID), DAO rs.movelast and both returns Long instead of the number.

    Please help and thanks.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Please tell us about copying the current record and pasting into a new record. And the SQL inserts. Perhaps you can give us an example or 2.
    Long is a long integer. Autonumbers when referenced as foreign key use Long (integer).

  3. #3
    zkyknight is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Jan 2019
    Posts
    7
    Quote Originally Posted by orange View Post
    Please tell us about copying the current record and pasting into a new record. And the SQL inserts. Perhaps you can give us an example or 2.
    Long is a long integer. Autonumbers when referenced as foreign key use Long (integer).
    It appears to be a corrupted textbox, but it still did not explain why the select query cannot find the record I'm looking for.

    Sample Code:
    DoCmd.RunCommand acCmdSelectRecord
    DoCmd.RunCommand acCmdCopy
    DoCmd.GoToRecord , , acNewRec
    DoCmd.RunCommand acCmdPaste
    DoCmd.RefreshRecord

    sql = "select [ID] from [Policy Periods]"
    Set rs4 = db.OpenRecordset(sql)
    rs4.MoveLast

    sql = "select [ID], [Location #] from [Locations] where [Policy ID] = " & fixSQLnumOrNull(rs4![ID])
    Set rs3 = db.OpenRecordset(sql)

    At rs3 recordset, it is says it is EOF, but Locations table has the record. Let me paint a bigger picture.

    There is a Policy Form and the Location subForm. When this copies to a new record it copies records in the form and subform. But my rs3 recordset cannot find the record in the Location table even though I can see it in the table. rs4![ID] exists as I was testing this inside the textbox, which was found to be corrupted.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Suggest you post a copy of the db showing only a few records highlighting the issue.
    There is no inherent order to the records in a table.
    Can you take some screen shots to show us what you are seeing?

  5. #5
    zkyknight is offline Novice
    Windows 10 Access 2010 64bit
    Join Date
    Jan 2019
    Posts
    7
    Quote Originally Posted by orange View Post
    Suggest you post a copy of the db showing only a few records highlighting the issue.
    There is no inherent order to the records in a table.
    Can you take some screen shots to show us what you are seeing?
    This first picture shows the result of the code. I made it that if it is not EOF it will return the ID, but if it is EOF, it returns 1.
    At the bottom, where it is green, the number there represent the ID from the Location Table, so it should exist. From my previous code, rs4![ID] returns the Policy ID which is the criteria use to find the Location ID.
    Click image for larger version. 

Name:	Capture.PNG 
Views:	13 
Size:	10.2 KB 
ID:	36878Click image for larger version. 

Name:	Capture.PNG 
Views:	12 
Size:	2.2 KB 
ID:	36882
    The second picture is that of the Locations Table, which clearly shows that the record exist. However, I cannot get this Location ID, which I need to insert to another table.
    Attached Thumbnails Attached Thumbnails Capture.PNG   Capture.PNG  

  6. #6
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    If you copy a policy record wouldn't the location ID would be the same as the one being copied? If you do this on a form can't you reference the control holding the location ID instead of trying to get it from the newly pasted record? And I would try dmax("[ID]","[Policy Periods]") if you want to get the latest ID (autonumber).

    Cheers,
    Vlad

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Be cautious with autonumbers. They are unique, but not necessarily in sequence nor positive.

  8. #8
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    Open rs3 in this way:
    Code:
    sql = "select [ID], [Location #] from [Locations] where [Policy ID] = (select max([ID]) AS LastID from [Policy Periods])
    Set rs3 = db.OpenRecordset(sql)

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

Similar Threads

  1. Get autonumber after new record entered
    By GraeagleBill in forum Forms
    Replies: 4
    Last Post: 08-24-2016, 10:23 AM
  2. Replies: 6
    Last Post: 05-04-2015, 02:18 PM
  3. Replies: 19
    Last Post: 12-21-2012, 06:57 AM
  4. Replies: 1
    Last Post: 05-23-2012, 11:45 AM
  5. Replies: 1
    Last Post: 02-02-2012, 08:34 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