Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-08-17
7
Medium Priority
?
67 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
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 40

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 52

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 52

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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

885 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