Duplicate record problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,
Im trying to assign a button that will copy all the data in a form(bound to
a table) to a new entry.
I have an autonumber field in the form.
I use the DoCmd.GoToRecord acForm, "xxxxxxx", acNewRec
The problem is that in the table, each time that i hit the duplicate
button,two rows are created. one empty with just a +1 in the autonum field
and the other with all the data i've copied with code.
Thanks a lot in advance
 
Its actually quite easy. But can you post your code so we can see what's
going on?
 
You'll do it by changing the default value for the affected controls.
From www.mvps.org/access
Forms: Carry current value of a control to new records
Author(s)
Dev Ashish


(Q) How do I carry forward the current value of a control so that
it's automatically entered for all new records?

(A) To use the current control value for new records, you need to
assign it to the defaultvalue of the control. For example something
like

'******** Code Start **********
const cQuote="""" 'Thats two quotes
me!Control.DefaultValue = cQuote & me!Control.Value & cQuote
'******** Code End **********

would carry the current value of the control forward if you place
this code behind a command button to conditionally run it.

In the BeforeUpdate event of the form, place an additional line for
each control whose value you want to carry forward.

HTH
 
Thanks a lot for the help.
Here is the code i use:
..
..
InvoiceFooterDetail1 = Me.InvoiceFooterDetail
InvoiceFooter1 = Me.InvoiceFooter
InvoiceFooterAddress1 = Me.InvoiceFooterAddress

DoCmd.GoToRecord acForm, "frmInvoice", acNewRec 'goes to new record

InvoiceDate = InvoiceDate1
Me.InvoiceState = "Draft"
Me.InvoiceType = InvoiceType1
..
..
Also i use the Load_form:

Private Sub Form_Load()
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command
Dim rs As ADODB.Recordset
Dim strSQL As String


DoCmd.GoToRecord , , acNewRec

' Assing public vars
InvoiceState.Value = "Draft"
CompanyBillID.Value = BillCompany
CompanyShipID.Value = ShipCompany
InvoiceFooter.Value = OrmatCompany
InvoiceType.Value = DocType

' Set DB vars
Set cn = CurrentProject.Connection
Set cmd = New ADODB.Command
strSQL = "SELECT * FROM qryCompany Where MISPAR_HBRH_F=" & BillCompany &
" "
With cmd
.ActiveConnection = cn
.CommandText = strSQL
.CommandType = adCmdText
.Execute
End With
Set rs = New ADODB.Recordset
rs.CursorType = adOpenStatic
rs.LockType = adLockReadOnly
rs.Open cmd
If rs.RecordCount = 0 Then
Exit Sub
' DoCmd.Quit
Else
'Enable Form objects according to permission
BillComName = rs.Fields("Expr2")
BillComAddress1 = rs.Fields("F_1ADDRESS_COMPANY")
BillComAddress2 = rs.Fields("F_2ADDRESS_COMPANY")
BillComCountry = rs.Fields("F_3ADDRESS_COMPANY")
End If

Set rs = Nothing
' check whether the ship and bill company is the same
If BillCompany <> ShipCompany Then
Set cmd = New ADODB.Command
strSQL = "SELECT * FROM qryCompany Where MISPAR_HBRH_F=" &
ShipCompany & " "
With cmd
.ActiveConnection = cn
.CommandText = strSQL
.CommandType = adCmdText
.Execute
End With
Set rs = New ADODB.Recordset
rs.CursorType = adOpenStatic
rs.LockType = adLockReadOnly
rs.Open cmd
'Enable Form objects according to permission
ShipComName = rs.Fields("Expr2")
ShipComAddress1 = rs.Fields("F_1ADDRESS_COMPANY")
ShipComAddress2 = rs.Fields("F_2ADDRESS_COMPANY")
ShipComCountry = rs.Fields("F_3ADDRESS_COMPANY")
Else
ShipComName = BillComName
ShipComAddress1 = BillComAddress1
ShipComAddress2 = BillComAddress2
ShipComCountry = BillComCountry
End If

Set cmd = Nothing
Set rs = Nothing
Set cn = Nothing

End Sub
 
Back
Top