Link to home
Start Free TrialLog in
Avatar of Jerry N
Jerry NFlag for United States of America

asked on

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

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?
Avatar of PatHartman
PatHartman
Flag of United States of America image

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.
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

ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jerry N

ASKER

Excellent!  love the code. Easy to manipulate the fields with a select case statement.
Love it.
Thanks
Avatar of Jerry N

ASKER

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?
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

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