473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with duplicate record, with changed field

Working in Access 2003.
I have managed to duplicate a record using a button on a form with code:
Private Sub Command16_Click()
On Error GoTo Err_Command16_Click


DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append

Exit_Command16_Click:
Exit Sub

Err_Command16_Click:
MsgBox Err.Description
Resume Exit_Command16_Click

End Sub

But this just creates a duplicate of the fields on the form, which is fine, but i would also like to +1 to one of the fields on the new record.

Is this possible? If so how would i do it.



Also.....
If this is possible,
would it also be possible to have a drop down menu in which to select say (n)1-9 and that is how many duplicates it will make with field +(n).
so the first duplicate will be the same but a field will have +1, and the next +2, etc.
for the selected number of fields
Mar 16 '07 #1
12 2111
Let's Assume that there is a table tblStuff with fields txtName, txtAddress, and numID. Let's also assume that you wanted to increment numID and you are copying the last record made.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command16_Click()
  2.     On Error GoTo errCommand16Click
  3.  
  4.     Dim strName as String
  5.     Dim strAddress as String
  6.     Dim intIDNo as Integer
  7.     Dim rstStuff as ADODB.Recordset
  8.  
  9.     With rstStuff
  10.         'Open tblStuff to read and write
  11.         .Open "tblStuff", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
  12.         'Moves to the last record, and then copies its values to temporary variables
  13.         .MoveLast
  14.         strName = rstStuff![txtAddress]
  15.         strAddress = rstStuff![txtAddress]
  16.         intIDNo = rstStuff![numID] + 1 'Adds one to the ID number
  17.         'Add a new record to tblStuff and give it the values pulled from the last record in the table
  18.         .AddNew
  19.         ![txtName] = strName
  20.         ![txtAddress] = strAddress
  21.         ![numID] = intIDNo
  22.         .Update
  23.         .Close
  24.     End With
  25.  
  26.     Set rstStuff = Nothing
  27.  
  28.     Exit Sub
  29.  
  30. errCommand16Click:
  31.     MsgBox Err.Description
  32.  
  33. End Sub
  34.  
In order to make several copies, grab the value from the sub-menu and store it in a variable (such as i) and then use For i = 1 to X just before the With rstStuff and End For just after Set rstStuff = Nothing
Mar 16 '07 #2
Denburt
1,356 Expert 1GB
After the new duplicate record is created then simply refer to the control in question and add 1

Me!YOURTEXTBOX = Me!YOURTEXTBOX+1



To break it down it would simply look like so:


Private Sub Command16_Click()
On Error GoTo Err_Command16_Click
Dim i as integer, cnt as integer

cnt = Me!COUNTTEXTBOXNAME

For i = 0 to cnt
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append

Me!YOURTEXTBOX = Me!YOURTEXTBOX+1

next

Exit_Command16_Click:
Exit Sub

Err_Command16_Click:
MsgBox Err.Description
Resume Exit_Command16_Click

End Sub
Mar 16 '07 #3
Denburt
1,356 Expert 1GB
As a side note I didnt check to see what you were doing with the menubar commands those commands can be troublesome at times (not function correctly) I would suggest doing this in code it would be much more reliable..


Check out the following article:
http://support.microsoft.com/kb/210236
Mar 16 '07 #4
thanks i will give that a try, will that only work for the last record? or will it work for whatever record is currently selected in the form?
Mar 16 '07 #5
what i have already duplicates just the name, date index, and target 1, target 2, target 3 fields, and the autonumber already increases as it should, but i want to +1 to the date index field.
Mar 16 '07 #6
Denburt
1,356 Expert 1GB
If your adding one day to a date it would look more like:

Me!YOURTEXTBOX = DateAdd("d",1,Me!YOURTEXTBOX)


if you want to copy a particular record not just the last one it just needs a few adjustments.

Remove this:

If Not F.NewRecord Then Exit Function


Adjust the following lines to read:


Set RS = F.RecordsetClone
'This line can be removed RS.MoveLast

rs.Bookmark=Me.Bookmark
docmd.GoToRecord ,,acNewRec

I think that should work for you let me know.
Good Luck
Mar 16 '07 #7
its a date index, that refers to a date in another table. just need to +1.
Mar 16 '07 #8
one last thing, i think. I've managed to get the form to do what i want using:

Private Sub Command16_Click()
On Error GoTo Err_Command16_Click
Dim i As Integer, cnt As Integer

cnt = Me!Combo17

For i = 0 To cnt
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append

Me!Text22 = Me!Text22 + 1

Next

Exit_Command16_Click:
Exit Sub

Err_Command16_Click:
MsgBox Err.Description
Resume Exit_Command16_Click

End Sub



Now when the records have been added it automatically takes me to the last record added. Is it possible to add the duplicates and stay with the current record on the form?
Mar 16 '07 #9
Denburt
1,356 Expert 1GB
At the beggining of the code mark your location theen at the end return to your original location.

Dim varBk As Integer
varBk = Me.Bookmark






Me.Bookmark = varBk

That should work.
Mar 16 '07 #10
ive tried that, but i get a "type mismatch" error now
Mar 16 '07 #11
Ok, well i got it all sorted now. using this code:
Private Sub Command16_Click()
On Error GoTo Err_Command16_Click
Dim i As Integer, cnt As Integer, varBk As String

varBk = Me.Bookmark
cnt = Me!Combo17

For i = 0 To cnt - 1
DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append

Me!Text22 = Me!Text22 + 1



Next

Exit_Command16_Click:
Me.Bookmark = varBk
Exit Sub

Err_Command16_Click:
MsgBox Err.Description


Resume Exit_Command16_Click


End Sub




But......

Is it also possible to stop this solution making a duplicate duplicate.

What i mean is; this solution creates a duplicate of fields x, y, z and w+1.

So this is not creating exact duplicates, as w value has changed for each record.

Now if you hit this button, or run this, twice, there will be REAL duplicates, as there will be 2 copies of x ,y, z and w+1.

So..

Is it possible to add code that will replace the old duplicate.
Also because z is a variable and i may want this changed on the duplicates, if they already exist i can't just block the new duplicate being sent.
Mar 17 '07 #12
Denburt
1,356 Expert 1GB
You can use something like the following to prevent duplication of data or change it up to suite your needs.

rs= me.recordsetclone
if rs.findfirst (PutWhereConditionHere) Then
rs.close
set rs = nothing
exit sub
end if
Mar 19 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Bill Kellaway | last post by:
Hi there - this should be fairly simple for someone. Basically I can't figure out how to pass the parameters from ASP to a Stored Procedure on SQL. Here's my code: I just need to help in...
0
by: Rob Knowles | last post by:
I have created a script that runs once a week and copies data from one table (phpbb_users is the actual table name) in a database called users I have a table called users. There are two tables...
8
by: Mark | last post by:
When my form goes to a new record, I have a procedure that copies the last record added to the form's underlying table into the form. The intent is that a series of new records may have the same...
8
by: Steph | last post by:
Hi. I'm very new to MS Access and have been presented with an Access database of contacts by my employer. I am trying to redesign the main form of the database so that a button entitled...
3
by: accesstribe | last post by:
Hello, I have DB located on network, and network cable was cut off accidently few days ago... now its fixed, but I can't add new records to the DB with forms... The error I get is this (when...
8
by: DP | last post by:
hi, i've got a price of code, which checks to see if a film is on rent, or available. but how can i actualyl make the cancel button do somthing? because, the cursor gets stuck on the filmID...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
9
by: Tom_F | last post by:
To comp.databases.ms-access -- I just discovered, to my more than mild dismay, that some tables in my Microsoft Access 2003 database have duplicate numbers in the "AutoNumber" field. (Field...
4
theaybaras
by: theaybaras | last post by:
Hi All... I've read both of the requery discussions I could find... Refresh ComboBox After Adding New Values via a Seperate Form and refresh a form but am not quite able to get this to apply...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.