Moving information from Access to Excel using VBA: "Do Until" code

I know the title of this question is vague, but I am not sure how to explain this simply.


I have a piece of code that moves information from an Access Database to an excel spreadsheet.

Right now it won't allow duplicates records to be shown on the spreadsheet. This is great with values like TheTO and TheSTOname. But I need to have the TheStaffName repeat with each of TheSTOnames.

I have tried to change the code to say "If rs!STO AND rs!TeamName = TheSTOname AND TheStaffName" and then remove the Else and End If from the section of the code
that moves TheSTOname and TheStaffName so that there is no extra Else statement, but that ends the process really quickly and only a few items from my database end up added to the spreadsheet.

Below is the code I am working with and some notes about what I need to have happen.


Do Until rs.EOF
'This piece does not allow the TO to repeat. If the TO is repeated, then it will just move to the next record until it finds one that does not repeat
    If rs!TO = TheTO Then
    'Same with the STO. Keeps the STO from repeating. If the STO is a duplicate, it goes to the next one.
        If rs!STO = TheSTOname Then
        'If the staff name duplicates, then it goes to the next record. THIS IS A PROBLEM. I need the staff names to repeat with the STOs
            If TheStaffName = rs!TeamName Then
            'If the ActDesc repeats, it will move to the next line.
                If theActDesc = rs!ActDesc Then
                    rs.MoveNext
                Else
                'Starts writing the ActDesc to the spreadsheet
                    theRow = theRow + 1
                    oSheet.Cells(theRow, 4).Value = rs!ActDesc
                    rs.MoveNext
                End If
            Else
            'Starts moving the staff names to the spreasheet. I want these connected to the sto name.
                theRow = theRow + 1
                oSheet.Cells(theRow, 3).Value = rs!TeamName
                TheStaffName = rs!TeamName

                rs.MoveNext
            End If
        Else
            theRow = theRow + 1
            oSheet.Cells(theRow, 2).Value = rs!STO
            TheSTOname = rs!STO
            rs.MoveNext
        End If
    Else
        theRow = theRow + 1
        oSheet.Cells(theRow, 1).Value = rs!TO
        TheTO = rs!TO
        rs.MoveNext
    End If
skip:
Loop

Open in new window



Thank you (in advance)!!!!
MeginAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MeginAuthor Commented:
So, what it does is shown here (without the TO field). I have highlighted where I need to have the staff name duplicated. I don't want it duplicated for every activity, but just for every subtask.
example.jpg
0
ste5anSenior DeveloperCommented:
Create a query which generates your output. Then use the Range.CopyFromRecordset method to transfer the data.
0
JimFiveCommented:
When the STO changes set theStaffName = ""
That would be around line 28 in your snippet.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MeginAuthor Commented:
That did it!

Thank you!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.