Solved

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

Posted on 2016-08-17
7
32 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 34

Expert Comment

by:PatHartman
Comment Utility
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
Comment Utility
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 49

Accepted Solution

by:
Gustav Brock earned 500 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Closing Comment

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

Author Comment

by:GNOVAK
Comment Utility
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
Comment Utility
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 49

Expert Comment

by:Gustav Brock
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now