Connecting Tech Pros Worldwide Help | Site Map

Duplicating record

  #1  
Old November 12th, 2005, 06:16 PM
pwys
Guest
 
Posts: n/a
Hello to everyone

WOndering if there anyone could help me with this.

I have a Primary & a secondary table with the unique key (InvoiceNo)

Waht i want is to make duplicate copy of a selected invoice no (both Primary
& secondary) into a new invoice no.

can some one help me in this?

TQ



  #2  
Old November 12th, 2005, 06:16 PM
Allen Browne
Guest
 
Posts: n/a

re: Duplicating record


This code duplicates the invoice in a form, along with the detail lines in
the subform. It uses DAO to duplicate the main record so you can get the
InvoiceID for the new record, since you need that value for the child
records. The child records are duplicated with an Append query statement.

The code assumes a command button on the main form.

Private Sub cmdDupe_Click()
Dim sSQL As String
Dim db As DAO.Database
Dim lngInvID As Long

Set db = DBEngine(0)(0)

If Me.Dirty Then 'Save any changes.
Me.Dirty = False
End If
If Me.NewRecord Then
MsgBox "Select the record to duplicate."
Else

'Duplicate the main record
With Me.RecordsetClone
.AddNew
!InvoiceDate = Date
!ClientID = Me.ClientID
'etc for other fields.
.Update
.Bookmark = .LastModified
lngInvID = !InvoiceID 'The new primary key value.

'Duplicate the related records.
If Me.fInvoiceDetail.Form.RecordsetClone.RecordCount > 0 Then
sSQL = "INSERT INTO tInvoiceDetail ( InvoiceID, Item,
Amount ) " & _
"SELECT " & lngInvID & " As NewInvoiceID, Item, Amount "
& _
"FROM tInvoiceDetail " & _
"WHERE (InvoiceID = " & Me.InvoiceID & ");"
db.Execute sSQL, dbFailOnError
Else
MsgBox "Main record duplicated, but there were no related
records."
End If

'Display the duplicate.
Me.Bookmark = .LastModified
End With
End If

Set db = Nothing
End Sub

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"pwys" <niteking@hotmail.com> wrote in message
news:3feb9cd9$1_2@news.tm.net.my...[color=blue]
>
> WOndering if there anyone could help me with this.
>
> I have a Primary & a secondary table with the unique key (InvoiceNo)
>
> Waht i want is to make duplicate copy of a selected invoice no (both[/color]
Primary[color=blue]
> & secondary) into a new invoice no.[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA - duplicating record sets based off 1 field quantity and 1 field reference? Kanashii answers 2 April 23rd, 2007 02:52 PM
Duplicating Record Problem jpatchak answers 6 December 6th, 2006 07:45 PM
Duplicating record John answers 2 November 13th, 2005 12:47 PM
How to avoid clip board message when duplicating record Danny answers 2 November 13th, 2005 12:17 AM