Solved

Loop through Recordset for Duplicate Values / Append Value to Separate Table

Posted on 2011-03-19
2
640 Views
Last Modified: 2012-06-27
I need to loop through a filtered recordset to check the value of a field (Table A, Field A). If each value of the field in the recordset is the same, I need to take that value and append it to a field in a separate table (Table B, Field A).

So, it would look something like this:

Table A
Record 1, Field A = 6
Record 2, Field A = 6
Record 3, Field A = 6
Record 4, Field A = 6

becomes . . .

Table B
Record 1, Field A = 6

If the recordset values are not the same, then the field value in Table B is a Null. Thanks.
0
Comment
Question by:skennelly
2 Comments
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
ID: 35172008
This procedure should do the job:
Public Sub CheckSame()
'Created by Helen Feddema 19-Mar-2010
'Last modified by Helen Feddema 19-Mar-2011

On Error GoTo ErrorHandler

   Dim rstData As DAO.Recordset
   Dim rstNewData As DAO.Recordset
   Dim lngTestValue As Long
   Dim lngOldTestValue As Long
   
   Set rstData = CurrentDb.OpenRecordset("tblData", dbOpenDynaset)
   Set rstNewData = CurrentDb.OpenRecordset("tblNewData")
   
   rstData.MoveFirst
   lngTestValue = rstData![TestAmount]
   
   Do While Not rstData.EOF
      If rstData![TestAmount] <> lngTestValue Then
         GoTo ErrorHandlerExit
      Else
         rstData.MoveNext
      End If
   Loop
   
   rstNewData.AddNew
   rstNewData![TestAmount] = lngTestValue
   rstNewData.Update
   rstData.Close
   rstNewData.Close
   
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in CheckSame procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub

Open in new window

0
 

Author Comment

by:skennelly
ID: 35173378
Thank you Helen, I reworked the code to fit my needs, and it worked seamlessly. Thanks again.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now