Solved

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

Posted on 2016-08-17
7
55 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 38

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 500 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

628 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