Solved

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

Posted on 2014-09-22
4
201 Views
Last Modified: 2014-09-23
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)!!!!
0
Comment
Question by:Megin
  • 2
4 Comments
 

Author Comment

by:Megin
ID: 40337994
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
 
LVL 33

Expert Comment

by:ste5an
ID: 40338837
Create a query which generates your output. Then use the Range.CopyFromRecordset method to transfer the data.
0
 
LVL 15

Accepted Solution

by:
JimFive earned 500 total points
ID: 40339253
When the STO changes set theStaffName = ""
That would be around line 28 in your snippet.
0
 

Author Closing Comment

by:Megin
ID: 40339474
That did it!

Thank you!!!!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

825 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