473,406 Members | 2,356 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,406 software developers and data experts.

please help - think this code needs a slight tweak

Im using the following code to duplicate a record varCnt(retrieved from a combo box on the form) many times, and it only duplicates the record with the fields present on the form for that record.
The duplicate fields are [childs name] and [target 1] [target 2] (upto target 5) and then it duplicates the [weekly index] but adds 1 to each duplicate for this field.
But if the targets are updated again it keeps the old duplicates for future weeks, causing there to be 2 sets of records for those future weeks.
So ive added a bit to the code when they are added to search for duplicates, but what it does is delete all but the last updated record.
But what i need it to do is not to delete the last updated record, but the last varCnt record, so in other words if varCnt is 3, then it deletes all but the last 3.
heres the code, i reckon this must be quite simple, but i cant work it out, ive tried playing around a bit, but im not a coder:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command16_Click()
  2. On Error GoTo Err_Command16_Click
  3. Dim i As Integer, cnt As Integer, varBk As String
  4. Dim intCriteriaCount As Integer, MyDB As Database, MyRS As Recordset
  5. Dim intCounter As Integer
  6.  
  7. varBk = Me.Bookmark
  8. cnt = Me!Combo17
  9.  
  10. For i = 0 To cnt - 1
  11. DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
  12. DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
  13. DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 'Paste Append
  14.  
  15. Me!Text22 = Me!Text22 + 1
  16.  
  17. Next
  18.  
  19. Exit_Command16_Click:
  20. Me.Bookmark = varBk
  21.  
  22. intCriteriaCount = DCount("*", "FindDuplicatesWeeklyPoints")
  23.  
  24. 'Are there Records meeting the Criteria?
  25. If intCriteriaCount > 0 Then
  26.   Set MyDB = CurrentDb()
  27.   'Order by your Primary Key Ascending
  28.   Set MyRS = MyDB.OpenRecordset("SELECT * FROM  FindDuplicatesWeeklyPoints ORDER BY [Weekly Index]")
  29.   MyRS.MoveLast: MyRS.MoveFirst
  30.     For intCounter = 1 To intCriteriaCount - 1      'All but the Last Record
  31.       MyRS.Delete
  32.       MyRS.MoveNext
  33.     Next
  34. Else
  35.   Exit Sub
  36. End If
  37.  
  38. MyRS.Close
  39.  
  40. Exit Sub
  41.  
  42. Err_Command16_Click:
  43. MsgBox Err.Description
  44.  
  45. Resume Exit_Command16_Click
  46.  
  47. End Sub
  48.  

thanks
Adam
Mar 18 '07 #1
1 1192
MMcCarthy
14,534 Expert Mod 8TB
Hi Adam,

Will this work ...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command16_Click()
  2. On Error GoTo Err_Command16_Click
  3. Dim i As Integer, cnt As Integer
  4. Dim varBk As String
  5. Dim intCriteriaCount As Integer
  6. Dim MyDB As Database
  7. Dim MyRS As Recordset
  8. Dim intCounter As Integer
  9.  
  10. varBk = Me.Bookmark
  11. cnt = Me!Combo17
  12.  
  13. For i = 0 To cnt - 1
  14.    DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
  15.    DoCmd.DoMenuItem acFormBar, acEditMenu, 2, , acMenuVer70
  16.    DoCmd.DoMenuItem acFormBar, acEditMenu, 5, , acMenuVer70 
  17.  
  18.    'Paste Append
  19.    Me!Text22 = Me!Text22 + 1
  20.  
  21. Next
  22.  
  23. Exit_Command16_Click:
  24. Me.Bookmark = varBk
  25.  
  26. intCriteriaCount = DCount("*", "FindDuplicatesWeeklyPoints")
  27.  
  28. 'Are there Records meeting the Criteria?
  29. If intCriteriaCount > 0 Then
  30.    Set MyDB = CurrentDb()
  31.    'Order by your Primary Key Ascending
  32.    Set MyRS = MyDB.OpenRecordset("SELECT * FROM FindDuplicatesWeeklyPoints ORDER BY [Weekly Index]")
  33.    MyRS.MoveLast: MyRS.MoveFirst
  34.    For intCounter = 1 To intCriteriaCount - cnt      'All but the Last Record
  35.       MyRS.Delete
  36.       MyRS.MoveNext
  37.    Next
  38. Else
  39.    Exit Sub
  40. End If
  41.  
  42. MyRS.Close
  43.  
  44. Exit Sub
  45.  
  46. Err_Command16_Click:
  47. MsgBox Err.Description
  48.  
  49. Resume Exit_Command16_Click
  50.  
  51. End Sub
  52.  
Mar 22 '07 #2

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

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
18
by: free2cric | last post by:
Hi, I attanded an interview on C++ Question asked were and my answers to them.. 1. In a CPP program what does memory leak occure? -- i said.. In a constructor , the pointer variables were...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
22
by: Colin McGuire | last post by:
I apologize for posting yet another scrollbar question. Here is my code. All I want is for a diagonal line to appear from coordinates (0,0) to (width,height) in a usercontrol regardless of whether...
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
259
by: user923005 | last post by:
It would be really nice if C could adopt a really nice algorithms library like C++'s STL + BOOST. The recent "reverse the words in this sentence" problem posted made me think about it. It's...
27
by: Dave | last post by:
I'm having a hard time tying to build gcc 4.3.1 on Solaris using the GNU compilers. I then decided to try to use Sun's compiler. The Sun Studio 12 compiler reports the following code, which is in...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.