Menu
Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Newsgroup Archive
Access Newsgroups
Access Beginners
Newbee - how to loop through table and delete unwanted records
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Newbee Adam" data-source="post: 1930928"><p>My table has records in it that are not exact duplicates but I only want 1</p><p>record per group number. Note it is always the 1st record of the new groupNo</p><p>I want.</p><p></p><p></p><p>example:</p><p>GroupNo GroupName_and_Location Termination Date</p><p></p><p>1201 GBKI Inc</p><p>20080101</p><p>1201 GBKI Inc - Houston</p><p>20080101</p><p>1201 GBKI Inc - Chicago</p><p>20080101</p><p></p><p></p><p>I tried several different</p><p>I tried several complex querys from several peoples suggestion with no</p><p>success. So I have decided to try to loop though the table and compare the</p><p>groupNo value to the groupNo value in the previos record I just looped</p><p>through. If it is the same ,then delte this current record I am on and an go</p><p>to next loop and compare, else just loop to next record. This is my first tim</p><p>eto loop through records like thius. Can I do this and if so , how do I tell</p><p>it to delete the current record. As you can see My plan now is to:</p><p>1.make and SQL string : SQL = "DELETE * FROM GroupLookUp WHERE GroupNo '" &</p><p>"Delete Me!" & "';"</p><p>2. When I loop through a record if the group value is the same as the</p><p>previous record,</p><p>I set the value of the current record's field of GroupNo (which is a string)</p><p>to "Delete Me!" (I have not run the code this far I am sure I will get an</p><p>error on that line, I am not sure if you can change a field value without an</p><p>sql statement...or can I ??)</p><p>3. Then the next line of code runs the SQL delete string I made in step 1.</p><p>SQL = "DELETE * FROM GroupLookUp WHERE [gmgpno] = '" & "Delete Me!" & "'"</p><p>'If I cannot do above SQL then try something like this: "DELETE * FROM</p><p>GroupLookUp WHERE RecordPos = Counter"</p><p></p><p></p><p>Do Until rs.EOF</p><p>Counter = Counter + 1</p><p>RecordPos = (rs.RecordCount - (rs.RecordCount - Counter))</p><p></p><p>If Counter = 1 Then</p><p>PreviousLoopGroupNo = [gmgpno]</p><p>Else</p><p></p><p>ThisLoopsGroupNo = [gmgpno]</p><p></p><p>If ThisLoopsGroupNo = PreviousLoopsGroupNo Then</p><p>[gmgpno] = "Delete Me!"</p><p>DoCmd.RunSQL (SQL)</p><p>Counter = Counter - 1 'Need this for accurate recordCount</p><p>Else</p><p>PreviousLoopsGroupNo = ThisLoopsGroupNo</p><p>End If</p><p>End If</p><p></p><p>Loop</p><p></p><p>End Sub</p></blockquote><p></p>
[QUOTE="Newbee Adam, post: 1930928"] My table has records in it that are not exact duplicates but I only want 1 record per group number. Note it is always the 1st record of the new groupNo I want. example: GroupNo GroupName_and_Location Termination Date 1201 GBKI Inc 20080101 1201 GBKI Inc - Houston 20080101 1201 GBKI Inc - Chicago 20080101 I tried several different I tried several complex querys from several peoples suggestion with no success. So I have decided to try to loop though the table and compare the groupNo value to the groupNo value in the previos record I just looped through. If it is the same ,then delte this current record I am on and an go to next loop and compare, else just loop to next record. This is my first tim eto loop through records like thius. Can I do this and if so , how do I tell it to delete the current record. As you can see My plan now is to: 1.make and SQL string : SQL = "DELETE * FROM GroupLookUp WHERE GroupNo '" & "Delete Me!" & "';" 2. When I loop through a record if the group value is the same as the previous record, I set the value of the current record's field of GroupNo (which is a string) to "Delete Me!" (I have not run the code this far I am sure I will get an error on that line, I am not sure if you can change a field value without an sql statement...or can I ??) 3. Then the next line of code runs the SQL delete string I made in step 1. SQL = "DELETE * FROM GroupLookUp WHERE [gmgpno] = '" & "Delete Me!" & "'" 'If I cannot do above SQL then try something like this: "DELETE * FROM GroupLookUp WHERE RecordPos = Counter" Do Until rs.EOF Counter = Counter + 1 RecordPos = (rs.RecordCount - (rs.RecordCount - Counter)) If Counter = 1 Then PreviousLoopGroupNo = [gmgpno] Else ThisLoopsGroupNo = [gmgpno] If ThisLoopsGroupNo = PreviousLoopsGroupNo Then [gmgpno] = "Delete Me!" DoCmd.RunSQL (SQL) Counter = Counter - 1 'Need this for accurate recordCount Else PreviousLoopsGroupNo = ThisLoopsGroupNo End If End If Loop End Sub [/QUOTE]
Verification
Post reply
Forums
Archive
Newsgroup Archive
Access Newsgroups
Access Beginners
Newbee - how to loop through table and delete unwanted records
Top