473,587 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

please help - think this code needs a slight tweak

14 New Member
Im using the following code to duplicate a record varCnt(retrieve d 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 1211
MMcCarthy
14,534 Recognized Expert Moderator MVP
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
4520
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 go to great lengths to restructure my code just to look concise and make it more manageable. When I say this, I'm also referring to the way I...
18
2486
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 assigned a memory block but the programmer forgets to free them in the destructor by usng delete operator. 2. ok, the delete operator is used .....
23
3254
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 to create certain textboxes, labels, and combo boxes? Any ideas would be appreciated. Thanks
22
2341
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 the user autoscrolls the usercontrol (other things that are on the usercontrol I want to scroll, but haven't included any here). Here are the...
88
8021
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
9613
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 and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the...
259
6958
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 like 5 lines to do it in C++ because of all the nifty algorithms that come with the language (I think BOOST is going to get bolted on to the C++...
27
3053
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 the source (gcc-4.3.1/gcc/c-common.c) of gcc 4.3.1, is a syntax error. I'm inclined to agree, as it is like no C I have ever met. what is ...
0
2182
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 problems later). Apparently Point is a struct, a value type, and it does not behave like a classic structure (in my mind's eye, and see below)....
0
7923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.