Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    imsants is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    13

    Copying the Current Record into a new Record (with designated new primary key) CONT..

    Hi, I have a basic database since I am new to coding but I need help with this very same issue posted:


    https://www.accessforums.net/forms/c...ted-27599.html

    I am using her code, and got everything working, but I do have subforms, how can I implement what was stated here:
    http://www.allenbrowne.com/ser-57.html
    In with her code?

    So basically, I have a form with 3 subforms. Please excuse my lack of Access terminology, but I have the one table which is the main data on the form with a primary key that is not an autonumber, it's designated. Which the code in the first link works. Then I have 3 other subforms, on the same form, whose data/tables have referential integrity to the first table main recordsource to the form (not cascading: not sure if necessary). The 3 other tables/subforms have autonumbers generated. This is where I get lost, how do I include something like the lngID in the one example to generate a new autonumber to just the 3 subforms, yet be able to designate an ID on the main form itself.

    Also, in the link reference in her question:
    https://www.accessforums.net/access/...ord-15269.html
    He/She talks about making a form/pop up it seems to ask the user to select an ID Number previously known to copy the record from. This is something that I would like to implement but not sure how to link that to the code either.

    Thank you in advance, I hope I did this right, first time on a forum.
    Mars

  2. #2
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Welcome! Not sure where you're stuck. The general concept is to create the new main record, get the ID that was created for it, then use that ID to create the new child records. If yours is designated, you should be able to grab it. Perhaps seeing your existing code will clarify matters.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    imsants is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    13
    Hi thanks for replying so quickly, I thought I posted something earlier but I guess I'm using the forum wrong. Will get my code up shortly, thanks.

  4. #4
    imsants is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    13
    Ok, so I know I am not grasping the dim part of this code, this is why I keep getting errors on the me.RecordsetClone. Below is the code I have which I just grabbed from the other threads (listed above) and modified as explained. I have tblSite, tblCustomer, tblAccounts, tblSystem. On the form(frmTransferRecord) tblSite is the main recordsource and the rest are subforms.

    My database opens to a swithboard/splash screen where there are different command buttons. The one button (= Transfer) opens a pop up form where the user selects the Customer ID = tblSite.SITE_ID from a combo box, and it has an execute command button with the code below. I know I have to setup a FindFirst option and a search since I am not running it from a current record. Not sure where in the code to put this, or if I'm off base here please let me know.

    Ok, so what I would like to happen is that the code launched the duplicate record, open the frmTransferRecord so I can change the Customer ID to the designated ID, and change the new customer information. Essentially the other 2 subforms don't change. Once the changes are made I want to save the record and with the same action open frmUpdateRecord = the old record we just copied. Here I have to change the form from active to inactive and put transfer date and reason, the rest remains the same. I don't know if this is relevant but at the end, when I search the site/primarily the address, I need to show the current customer and all it's previous tenants that lived there. I hope this makes sense, it's got several actions and I just want to be able to do it all in as little steps as possible. Anyway, here is the code, I am learning and am eager to learn and have a better understanding of how this works. Any help and suggestions are greatly appreciated. Thanks again.

    Here is the code I have so far:

    Private Sub btnTRNSFRPrevTenXfr_Click()
    'On Error GoTo Err_Handler
    'Purpose: Duplicate the main form record and related records in the subform.

    Dim strSql As String 'SQL statement.
    Dim lngID As Long 'Primary key value of the new record.

    'Save any edits first
    If Me.Dirty Then
    Me.Dirty = False
    End If

    'Make sure there is a record to duplicate.
    If Me.NewRecord Then
    MsgBox "Select the record to duplicate."
    Else
    'Duplicate the main record: add to form's clone.
    With Me.RecordsetClone 'this is where the debug takes me on the invalid reference error I am getting. Also is it 'With' or Set rs = or Set rst? Question is it Me.Recordset.Clone with the period or leave as is?
    .AddNew
    !SITE_ID = Me.[tblSite].[SITE_ID].Value + 1
    !SYSTEM_ID = Me.[tblSite].[SYSTEM_ID]
    !ACCOUNT_ID = Me.[tblSite].[ACCOUNT_ID]
    !CUSTOMER_ID = Me.[tblSite].[CUSTOMER_ID]
    !STREET = [tblSite].[STREET]
    !CITY = [tblSite].[CITY]
    !STATE = [tblSite].[STATE]
    !ZIP = [tblSite].[ZIP]
    !ACTIVE = [tblSite].[ACTIVE]
    !STATUS = [tblSite].[STATUS]
    !COMMERCIAL = [tblSite].[COMMERCIAL]
    !RESIDENTIAL = [tblSite].[RESIDENTIAL]
    !MAINSITE = [tblSite].[MAINSITE]
    'etc for other fields.
    .Update

    'Save the primary key value, to use as the foreign key for the related records.
    .Bookmark = .LastModified
    lngID = ![tblSite].[COMPANY_ID] 'this is where I am lost, not sure what needs to reference here, from the code I used from the other thread.

    'Duplicate the related records: append query.
    If Me.[frmCustomer].Form.RecordsetClone.RecordCount > 0 Then
    strSql = "INSERT INTO [tblCustomer] (TITLE, FIRSTNAME, MINITIAL, LASTNAME, SUFFIX, STATUS, PSWRD, ALIAS, PHONE1, PHONEXT1, PHTYPE1, FAX, PHONE2, PHONEXT2, PHTYPE2, PHONE3, PHONEXT3, PHTYPE3) " & _
    "SELECT " & lngID & " As NewID, (TITLE, FIRSTNAME, MINITIAL, LASTNAME, SUFFIX, STATUS, PSWRD, ALIAS, PHONE1, PHONEXT1, " & _
    "FROM [tblCustomer] WHERE CUSTOMER_ID = " & Me.[tblSite].[COMPANY_ID] & ";"
    DBEngine(0)(0).Execute strSql, dbFailOnError
    Else
    MsgBox "Main record duplicated, but there were no related records."
    End If

    'Display the new duplicate. Here I am not sure how to docmd to open the frmTransferRecord, before or after?
    Me.Bookmark = .LastModified
    End With
    End If

    Exit_Handler:
    Exit Sub

    Err_Handler:
    MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdDupe_Click"
    Resume Exit_Handler

    End Sub

  5. #5
    imsants is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    13
    Oh and I am aware that I need to add the other two subforms to the append query to duplicate, I just put the one as a starter point to test.

  6. #6
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I personally would add directly to the table rather than the recordsetclone, though Allen is smarter than I am. Try this (though I'm not sure about the form references). Also, your SQL will fail because the fields don't match. You need the ID field; I've added what I think it might be, but you should double check.

    Code:
    Private Sub btnTRNSFRPrevTenXfr_Click()
      'On Error GoTo Err_Handler
      'Purpose: Duplicate the main form record and related records in the subform.
      Dim rs                      As DAO.Recordset
      Dim db                      As DAO.Database
      Dim strSql                  As String    'SQL statement.
      Dim lngID                   As Long    'Primary key value of the new record.
    
      'Save any edits first
      If Me.Dirty Then
        Me.Dirty = False
      End If
    
      'Make sure there is a record to duplicate.
      If Me.NewRecord Then
        MsgBox "Select the record to duplicate."
      Else
        'Duplicate the main record: add to form's clone.
        'With Me.RecordsetClone    'this is where the debug takes me on the invalid reference error I am getting. Also is it 'With' or Set rs = or Set rst? Question is it Me.Recordset.Clone with the period or leave as is?
        Set db = CurrentDb
        Set rs = db.OpenRecordset("tblSite", dbAppendOnly)
        With rs
          .AddNew
          !SITE_ID = Me.[tblSite].[SITE_ID].Value + 1
          !SYSTEM_ID = Me.[tblSite].[SYSTEM_ID]
          !ACCOUNT_ID = Me.[tblSite].[ACCOUNT_ID]
          !CUSTOMER_ID = Me.[tblSite].[CUSTOMER_ID]
          !STREET = [tblSite].[STREET]
          !City = [tblSite].[City]
          !State = [tblSite].[State]
          !zip = [tblSite].[zip]
          !ACTIVE = [tblSite].[ACTIVE]
          !Status = [tblSite].[Status]
          !COMMERCIAL = [tblSite].[COMMERCIAL]
          !RESIDENTIAL = [tblSite].[RESIDENTIAL]
          !MAINSITE = [tblSite].[MAINSITE]
          'etc for other fields.
          .Update
    
          'Save the primary key value, to use as the foreign key for the related records.
          .Bookmark = .LastModified
          lngID = !SITE_ID
          'lngID = ![tblSite].[COMPANY_ID]    'this is where I am lost, not sure what needs to reference here, from the code I used from the other thread.
    
          'Duplicate the related records: append query.
          If Me.[frmCustomer].Form.RecordsetClone.RecordCount > 0 Then
            strSql = "INSERT INTO [tblCustomer] (SITE_ID, TITLE, FIRSTNAME, MINITIAL, LASTNAME, SUFFIX, STATUS, PSWRD, ALIAS, PHONE1, PHONEXT1, PHTYPE1, FAX, PHONE2, PHONEXT2, PHTYPE2, PHONE3, PHONEXT3, PHTYPE3) " & _
                     "SELECT " & lngID & " As NewID, TITLE, FIRSTNAME, MINITIAL, LASTNAME, SUFFIX, STATUS, PSWRD, ALIAS, PHONE1, PHONEXT1, " & _
                     "FROM [tblCustomer] WHERE CUSTOMER_ID = " & Me.[tblSite].[COMPANY_ID] & ";"
            db.Execute strSql, dbFailOnError
          Else
            MsgBox "Main record duplicated, but there were no related records."
          End If
    
          'Display the new duplicate. Here I am not sure how to docmd to open the frmTransferRecord, before or after?
          Me.Requery
          Me.Bookmark = lngID
        End With
      End If
    
    Exit_Handler:
      Set db = Nothing
      Set rs = Nothing
      Exit Sub
    
    Err_Handler:
      MsgBox "Error " & Err.number & " - " & Err.Description, , "cmdDupe_Click"
      Resume Exit_Handler
    
    End Sub
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    imsants is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    13
    Hi there, ok so just like you said the sql failed. So I had the main table, tblSite setup with with SITE_ID as a field. The primary key is designated with a sequence of LEW123. I made the customer table, tblCustomers, generate an autonumber in CUSTOMER_ID field. Should I have made it the same as the SITE_ID? I just created fields with CUSTOMER_ID, ACCOUNT_ID etc in the tblSite to relate everything. Not sure where I'm going wrong here, thanks.

  8. #8
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Can you post the db here?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    imsants is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    13
    Here is a copy of what I have, still in the works. I removed the data so I don't know if I did it correctly. Thanks.
    Last edited by imsants; 02-05-2013 at 08:59 AM.

  10. #10
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Certainly don't want any personal info posted on the internet, but can you leave a record in each of the tables involved in this process and take out anything personal so I can test the process? I'd add data, but odds are I'd put something in differently and it would work for me and not on real data.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    imsants is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    13
    Good morning Paul, thanks for clarifying, here is the attachment as you instructed.
    Attached Files Attached Files

  12. #12
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I'm confused (not an uncommon state). The code is on an unbound form, but refers to a bunch of form controls that don't exist. Am I looking at the wrong thing?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    imsants is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    13
    Well, that is where I am confused and not sure how to code. The unbound form, to clarify frmTransferPopUp, should open frmTransferRecord once the append is complete. At least that is how I planned on it functioning. The problem for me was how to duplicate the record without a form or record open. Or should it relate to the main screen, then append, then open the frmTransferRecord for editing the ID. The main screen is just for preview, not editing. I thought I was referring to the tables not the forms, so that may be my confusion.

  14. #14
    pbaldy's Avatar
    pbaldy is online now Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Well, these lines would be pointing at the form:

    !SITE_ID = "'Me.SITE_ID.Value + 1'"
    !STREET = "'Me.STREET'"

    except when you enclose them in quotes you are going to get the literal text. What you had before:

    Me.[tblSite].[SYSTEM_ID]

    is not valid to refer to the table, if that's what you were trying to do. You can either refer to the main form if that's the record you want copied:

    Forms!MainFormName.ControlName

    or open a recordset on the table.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    imsants is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    13
    Hi Paul, busy morning. I thought I did the open recordset on the table, tblSite right after set db = currentdb. I think I am just really confused and my sql is not the best. Should I have a sqlstring instead? I would like to open the recordset on a new form, append the clone on the form for edits and then save as a new record. Any suggestion on the best way to do this. I know I used someones code and being new I'm having a hard time grasping what is what. Thanks again.

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

Similar Threads

  1. Replies: 3
    Last Post: 09-19-2012, 07:34 AM
  2. Replies: 5
    Last Post: 08-24-2012, 10:32 AM
  3. Set Current Record based on Primary Key Input
    By andersonEE in forum Forms
    Replies: 2
    Last Post: 06-24-2011, 08:23 AM
  4. Replies: 2
    Last Post: 03-25-2011, 12:22 PM
  5. Replies: 0
    Last Post: 02-25-2011, 09:40 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