Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access 2010: Adding a new record based on a current record in a form

Posted on 2016-08-17
7
Medium Priority
?
65 Views
Last Modified: 2016-08-18
I have a table called Requests which has an autogenerated ID field (PK). I have a form that contains information for requests.  Some of these are recurring requests.  When closing out a recurring request, I wish to copy the information to a new record and update the due date to a date that I have calculated (dNextDateDue).

Being a lazy person, I would like to avoid having to copy all the fields from the current record without naming fields.


Anyone have ideas?
0
Comment
Question by:GNOVAK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 39

Expert Comment

by:PatHartman
ID: 41760325
In the AfterUpdate event of the Form, run an append query that copies the current record and appends a new one with the next due date.  To do this, you'll need two hidden fields on your form.  One is the ID of the current record.  This might already be on the form.  The second is the next due date  The append query just needs to reference these two controls.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 41760329
Well.
I have articles about that
https://www.experts-exchange.com/articles/6692/Overcoming-unbound-continuous-forms-limitations-by-staging-data.html

But at the end of the day, there's our friend the Recordset object
Dim rs as recordset
Dim rs1 as recordset
Dim fld as Field

set rs = currentdb.openrecordset("some SQL that gives you the current record")
set rs1 = currentdb.openrecordset("some sql that opens the table(s) but with no records involved")

with rs1
    .AddNew
    For each fld in rs1.Fields
        if fld.Name <> "theNameOfYourPKField"
           fld .Value = rs.Fields(fld.Name)
        end if
    next fld
    .Update
end with

That's quick-and-dirty aircode, but you get the idea
or
for x = 1 to rs1.fields.count
     rs1.Fields(x) = rs.Fields(x)
next x

presuming the PK is field zero in the way the SQL gets written.
Flange in an if...then for the field that isn't a straight copy and have at 'er.

This is production code

Dim db As Database
Set db = CurrentDb
Dim rs As Recordset
Dim rs1 As Recordset
Dim fld As Field
Dim x As Integer
Set rs = db.OpenRecordset("Select * from tblInsdetails where [wo#] = '139286'", dbOpenDynaset, dbSeeChanges)
Set rs1 = db.OpenRecordset("Select * from tblInsdetails where 1=2", dbOpenDynaset, dbSeeChanges)
'MsgBox rs.RecordCount

With rs1
    For x = 1 To 30 '31 fields including the PK
        .AddNew
        For Each fld In rs.Fields
            If fld.Name <> "wo#" And fld.Name <> "jobid" And fld.Name <> "SSMA_Timestamp" Then
                'MsgBox rs.Fields(fld.Name).Value
                .Fields(fld.Name).Value = rs.Fields(fld.Name).Value
            End If
        Next fld
        ![wo#] = "139286-" & x
        .Update
    Next x
End With

Open in new window

0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 2000 total points
ID: 41760633
This has been up many times before. The cleanest method is to use VBA:

Clone Record

Note specifically how to ignore the PK.

/gustav


0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Closing Comment

by:GNOVAK
ID: 41761418
Excellent!  love the code. Easy to manipulate the fields with a select case statement.
Love it.
Thanks
0
 

Author Comment

by:GNOVAK
ID: 41761474
Gustav,
I have eliminated the move to the new record to avoid confusing the users. But I would like to get the new ID (PK) - how could I do that?  Is it available in the rstInsert once the Update is done?
0
 
LVL 26

Expert Comment

by:Nick67
ID: 41761480
After the .Update executes you can pull back the newly created PK with
(say it is a Long value)
myNewPK = rstInsert!ThePKField

Here's production code from a NotInList event
Private Sub ConsigneeID_NotInList(NewData As String, response As Integer)
Dim db As Database
Dim rs As Recordset
Dim lngOwnerID As Long
If vbYes = MsgBox("'" & StrConv(NewData, vbProperCase) & "' is not entered as a current Client." & vbCrLf & "Do you wish to add it?", vbQuestion + vbYesNo, " ") Then
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * FROM [tblClients] WHERE 1=2;", dbOpenDynaset, dbSeeChanges)
    With rs
        .AddNew
        ![Client Name] = StrConv(NewData, vbProperCase)
        .Update
        .Bookmark = .LastModified
       '**********Retrieve the new PK**********
        lngOwnerID = ![Client ID]
       '**********Retrieve the new PK**********
    End With
    rs.Close    
    Set rs = Nothing      
    response = acDataErrAdded
Else
    response = acDataErrContinue
End If
End Sub

Open in new window

1
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 41761520
Yes, you should be able to pick it up here after the Update:

        .Update
        NewID = !ID.Value   ' or similar.
        ' Go to the new record and sync form.
        .MoveLast

/gustav
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question