Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB code to create new record but copying data from previous record 1

Status
Not open for further replies.

DarkOne72

Technical User
Jun 14, 2002
210
US
Hello,

I was wondering if someone could assist me in creating some code that when I click on a Button on the form the it will utilize an Event Procedure to where it creates a new record but copying over data of like 4 fields from the previous record automatically.
Any help is appreciated, thank you!

Code:
Private Sub Tech_Dup_Click()

DoCmd.GoToRecord acDataForm, "Search_Main_Datasheet", acNewRec
Forms!Search_Main_Datasheet!ID = 1

Dim db As Database
Dim rst As DAO.Recordset
Set db = CurrentDb()
Set rst = db.OpenRecordset("Main")
   
   With rst
   .AddNew
![Number] = Me![Number]
![First] = Me![First]
![Last] = Me![Last]
![Supervisor] = Me![Supervisor]
   .Update
End With
rst.Close
    Set rst = Nothing
    Set db = Nothing
End Sub
 
which 4 fields?

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work" <<Edison>>
 
On both forms it is the same fields : Number, First, Last Supervisor are the fields I want to copy over to the new record.
The reason is it elimates errors on entering the above information and less time simply by hitting a button.
 

Could you specify what you mean by "previous record"? Pevious record accessed by user? Pevious record in the table? If so, what order you consider of the records? Previous record with the last Number?

Have fun.

---- Andy
 
how about

Code:
Private Sub Tech_Dup_Click()
dim VarNumber
dim VarFirst
dim VarLast
dim VarSupervisor
VarNumber = me.Number
VarFirst = me.first
VarLast = me.last
VarSupervisor= me.Supervisor

DoCmd.GoToRecord acDataForm, "Search_Main_Datasheet", acNewRec
Forms!Search_Main_Datasheet!ID = 1


Me![Number] = VarNumber
Me![First] = VarFirst
Me![Last] = VarLast
Me![Supervisor] = VarSupervisor
End Sub
 
Works like a charm PWise, your awesome!

Thank you everyone else for your replies as well!
 
How are ya DarkOne72 . . .

In thread702-1382356 see my post [blue]26 Jun 07 18:40[/blue] for another way that may interest you.

See Ya! . . . . . .

Be sure to see faq219-2884 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Aceman,
Thanks for the headsup on the alternate way of doing it; I am able to use it as well.

DarkOne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top