Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Copy Invoice
James Hopkins 
     
2 years ago
I been trying Duplicate the Invoice Record and its Invoice Detail Child Records into a New Invoice, and then Open up the Invoice Form to the New Invoice ID. Some way I got an ERROR Message.

Here's the Code and Error:

DetailsPrivate Sub BtnDuplicateInvoice_Click()

    Dim db As DAO.Database
    Dim rsInvoice As DAO.Recordset
    Dim rsInvoiceDetail As DAO.Recordset
    Dim strSQL As String
    Dim intNewInvoiceID As Integer
    Dim fld As DAO.Field

    ' Assuming InvoiceID is an Autonumber field in your Invoice Table
    Dim intInvoiceIDToCopy As Integer

    ' Open a reference to the current database
    Set db = CurrentDb

    ' Prompt the user to enter the InvoiceID to copy
    intInvoiceIDToCopy = InvoiceID

    ' Check if the entered InvoiceID exists in the Invoice Table
    strSQL = "SELECT COUNT(*) FROM InvoiceT WHERE InvoiceID = " & intInvoiceIDToCopy
    If DLookup("COUNT(*)", "InvoiceT", "InvoiceID = " & intInvoiceIDToCopy) = 0 Then
        MsgBox "EstimateID " & intInvoiceIDToCopy & " does not exist.", vbExclamation
        Exit Sub
    End If

    ' Open a recordset for the Invoice to copy
    Set rsInvoice = db.OpenRecordset("SELECT * FROM InvoiceT WHERE InvoiceID = " & intInvoiceIDToCopy)
Estima
    ' Add a new record in the Invoice Table to copy the order
    rsInvoice.AddNew

    ' Copy all fields except the Autonumber field (assuming InvoiceID is Autonumber)
    For Each fld In rsInvoice.Fields
        If fld.Name <> "InvoiceID" Then
            rsInvoice.Fields(fld.Name).Value = fld.Value
        End If
    Next fld

    ' Save the new Invoice record
    rsInvoice.Update

    ' Get the new InvoiceID
    intNewInvoiceID = rsInvoice.Fields("InvoiceID").Value

    ' Close the Invoice recordset
    rsInvoice.Close

    ' Open a recordset for the Invoice Details to copy
    Set rsInvoiceDetail = db.OpenRecordset("SELECT * FROM InvoiceDetailT WHERE InvoiceID = " & intInvoiceIDToCopy)

    ' Loop through each Invoice Detail record and add it to the new Invoice
    Do While Not rsInvoiceDetail.EOF
        rsInvoiceDetail.AddNew
        ' Copy all fields except the Autonumber field (assuming InvoiceDetailID is Autonumber)
        For Each fld In rsInvoiceDetail.Fields
            If fld.Name <> "InvoiceDetailID" Then
                rsInvoiceDetail.Fields(fld.Name).Value = fld.Value
            End If
        Next fld

        ' Set the new InvoiceID for the copied order detail record
        rsInvoiceDetail.Fields("InvoiceID").Value = intNewInvoiceID

        ' Save the new order detail record
        rsInvoiceDetail.Update

        ' Move to the next order detail record
        rsInvoiceDetail.MoveNext
    Loop

    ' Close the Order Detail recordset
    rsInvoiceDetail.Close

    ' Clean up
    Set rsInvoice = Nothing
    Set rsInvoiceDetail = Nothing
    Set db = Nothing

    ' Notify the user
    MsgBox "Invoice and Invoice Details copied successfully. New InvoiceID: " & intNewInvoiceID, vbInformation

    ' Open the Invoice Form
    DoCmd.OpenForm "InvoiceF", , , "InvoiceID = " & intNewInvoiceID, acFormEdit
    
End Sub
Richard Rost  @Reply  
          
2 years ago

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/2/2026 8:52:40 AM. PLT: 1s