Results 1 to 8 of 8
  1. #1
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,069

    Export a Query to Excel in VBA

    I have a query written in Access 2010 That pulls the data I need.

    The format of the data that comes out is

    encounter
    Name
    dob
    AdmitDate
    DischargeDate
    DiagDesc
    Attending Provider
    Hospital
    Seq
    Notes

    The purpose is to generate a list of all Members that went to our Urgent Care and within 24 hours were admitted to the Hospital. The Query works fine the issue is that the last 2 fields can have multiple records For example,
    If I omit the last 2 fields the query generated 8 Rows. Including the last 2 fields the query generated 31 Rows. The first 8 rows were repeated depending on how many Notes were entered.

    I need to format the export so that the first 8 fields appear Once for each Patient and then below that the Notes Appear in order

    Something like
    Encounter Name dob admitdate dischargedate diagdesc attending Provider Hospital
    1 this is note1
    2 this is note2

    Encounter Name dob



    I thought the best way was to use VBA Open the recordset and loop through outputting the first 8 fields in a row and then looping through and outputting each note
    when the mbr and admit date change then again output the first 8 fields and continue for the recordset.

    My problem is that it's been far too long since I had to output a query in this manner and I don't remember how to export to EXCEL without using the transferspreadsheet command and that one won't work for me.

    I've included the EXCEL Reference in VBA for the 14.0 Object Library. But not sure where to go from here anybody can point me to where I can research this. Thank you in advance.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Transferspreadsheet works every time.

  3. #3
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,069
    Not if you have to restructure the data.

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    the query does this.

  5. #5
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,069
    How The Query Results look like this

    08/02/2018 6:00 AM John Doe 12/23/1942 07/31/2018 8/01/2018 123.11 Primary Diagnosis My Hospital 1 This is note1
    08/02/2018 6:00 AM John Doe 12/23/1942 07/31/2018 8/01/2018 123.11 Primary Diagnosis My Hospital 2 This is note2
    08/02/2018 6:00 AM John Doe 12/23/1942 07/31/2018 8/01/2018 123.11 Primary Diagnosis My Hospital 3 This is note 3

    It Should look like this
    08/02/2018 6:00 AM John Doe 12/23/1942 07/31/2018 8/01/2018 123.11 Primary Diagnosis My Hospital
    1 This is note1
    2 This is note2
    3 This is note3

    There is no way to get the results in this format in a query that I know of.

  6. #6
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    I would export using Transferspreadsheet,
    then open the XL file,scan the rows,
    then delete the 7 cells after the 'main' record. Shifting the last cell to col.1.
    until the end.

  7. #7
    RayMilhon is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Location
    Southern California
    Posts
    1,069
    What I posted was a sample of the data the query returns 32,000 rows.

  8. #8
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Since you have a specific format in which the records need to be exported, transferspreadsheet won't work. Normally, this would be done by grouping in a report.

    I like the idea of using recordsets and Excel automation using VBA, but it depends on the table designs/structures.
    Are the fields "Seq" & "Notes" in a separate table?


    EDIT:

    The idea being with tables

    tblMembers -----> tblVisits ----> tblNotes

    you could then do something like


    (pseudo code)
    Code:
     sub ExportCustomRecords
    
    Open a record set named rsMembers that has fields "PKField", "Encounter", "Name", "dob", "admitdate", "dischargedate", "diagdesc", "attending Provider", "Hospital"
    
    rsMembers.MoveLast
    (check if records)
    rsMembers.MoveFirst
    
    Do until rsMembers.EOF
    
      write the 8 fields to Excel
    
      Next open a record set named rsNotes with fields "FKField", "Seq", "Notes" Where rsNotes.FKField = rsMembers.PKField
      (check for records)
      Now loop through the recordset, writing the notes
    
      Close rsNotes 
    
      rsMembers.MoveNext
    
    Loop
    
    
    clean up stuff
    
    
    Exit sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Export Query into Excel??
    By drjr in forum Import/Export Data
    Replies: 1
    Last Post: 07-10-2013, 05:45 PM
  2. Replies: 7
    Last Post: 04-25-2013, 03:47 PM
  3. Export query to excel
    By Liam87 in forum Queries
    Replies: 1
    Last Post: 12-05-2012, 12:00 AM
  4. Replies: 3
    Last Post: 10-07-2011, 07:49 AM
  5. Export query to Excel
    By dev82 in forum Queries
    Replies: 15
    Last Post: 02-10-2011, 11:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums