Solved

Loop through 2 arrays simultaneously and add the records to a new table

Posted on 2011-03-16
6
651 Views
Last Modified: 2012-05-11
This is an addendum to my previous question which was answered. The code below works when I split a concatenated string from a field in Table A and append the records to a Field in Table B. Now I have added a second concatenated field from Table A, that I need to append concurrently into a different field from the same record of Table B.

So basically:

Table A
Record  1:
Field 1 = 10, 20, 30, 40
Field 2 = 50, 60, 70, 80

becomes . .  .

Table B
Record 1: Field 1 = 10  Field 2 = 50
Record 2: Field 1 = 20  Field 2 = 60
Record 3: Field 1 = 30  Field 2 = 70
Record 4: Field 1 = 40  Field 2 = 80

The code below achieves the original desired result in appending Field 1 from Table A into separate records in Table B. I have tried different coding combinations to include the second split but nothing has worked. Thanks for your help again.

Public Sub SplitSetReps()
Dim rsIn As DAO.Recordset
Dim rsOut As DAO.Recordset
Dim intX As Integer

Set rsIn = CurrentDb.OpenRecordset("SELECT * Table A WHERE ID =  " & Forms!frmMainDetail!StrID, dbOpenDynaset)
Set rsOut = CurrentDb.OpenRecordset("Table B", dbOpenDynaset)

Do While Not rsIn.EOF
    If Not IsNull(rsIn!Reps) Then
        astrNames = Split(rsIn![Reps], ",")
    End If

    For i = 0 To UBound(astrNames)
        rsOut.AddNew
        rsOut![SetsRep] = astrNames(i)
        rsOut![SetsRepID] = rsIn!StrID

        rsOut.Update
    Next
    rsIn.MoveNext
Loop

End Sub
0
Comment
Question by:skennelly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 40

Expert Comment

by:als315
ID: 35149569
Add after string:
astrNames = Split(rsIn![Reps], ",")
this string:
astrNames1 = Split(rsIn![Reps1], ",")
Reps1 - Field 2 name in Table A
and after string
 rsOut![SetsRep] = astrNames(i)
this:
 rsOut![SetsRep1] = astrNames1(i)
SetsRep1 - Field 2 name in Table B
0
 

Author Comment

by:skennelly
ID: 35150258
Thanks als315. I have tried that solution and it works for the most part. However, at times the additional field in (SetsRep1) may be a Null value. When it is, I get a Run Time Error '9'; Subscript Out of Range message. I have tried several If IsNull Statements to alleviate the problem, again to no avail. Thanks again if you can assist me further.
0
 

Author Comment

by:skennelly
ID: 35150872
Let me retract that somewhat. It appears as if the error occurs when the strings do not match with the same number of array components (i). so if Field one is 10, 15, 20, 25, and Field 2 is 35, 45, and 65, I generate the error. I'll keep trying to narrow down this error. Thanks.
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 40

Accepted Solution

by:
als315 earned 500 total points
ID: 35151369
OK. You can add this condition:
if UBound(astrNames1) > i then
 rsOut![SetsRep1] = astrNames1(i)
end if
0
 

Author Comment

by:skennelly
ID: 35153218
Ok, we're getting there. When I insert your code into the condition, it works with the exception of the last value in the array of SetsRep1. So if the string is 10, 15, 20, 25 the field values in Table B are 10, 15, 20. The last value (25) is a null field. The original array (astrNames) contains all the values of the string. I'll keep playing with the condition statement. Thanks.
0
 

Author Comment

by:skennelly
ID: 35153265
Here is how I got it to work. I used the >= in the condition statement, and everything copied over correctly. Thank you for your assistance. I will continue to test all of the conditions, but will definitely award you the points. I appreciate it.

if UBound(astrNames1) >= i then
 rsOut![SetsRep1] = astrNames1(i)
end if
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

690 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