Solved

Export Access data to Excel using Access VBA

Posted on 2013-01-28
4
407 Views
Last Modified: 2013-01-28
Hi all.

I have the following code that exports my Access data to Excel. But I want to make some changes so instead of the data appearing in column A, B, C, D of row 1, I want it to appear as

Column A, row 1--"Contract Number" data
Column A, row 2--"Status" data
Column A, row 3--"Facility ID" data
Column A, row 4--"Facility Name" data

Any ideas?

Dim xlWb1 'Script for creating excel files
Dim xlWs1

    Set xlApp1 = CreateObject("Excel.Application")
    Set xlWb1 = xlApp1.Workbooks.Add
    Set xlWs1 = xlWb1.Worksheets("Sheet1")
    xlApp1.Visible = True
    xlApp1.UserControl = True
    xlApp1.DisplayAlerts = True
    Dim objCon1  As New ADODB.Connection
    Dim objRS1 As ADODB.Recordset
    Set objRS1 = New ADODB.Recordset
    Dim rsExcel1 As DAO.Recordset
    Set rsExcel1 = CurrentDb.OpenRecordset("Select tblContract_Header.Contract_ID,tblContract_Header.Status,  tblContract_Detail.Facility_ID,tblContract_Detail.Name  FROM tblContract_Header INNER JOIN tblContract_Detail ON tblContract_Header.Contract_Number = tblContract_Detail.Contract_Number where tblContract_Header.[Contract_Number] = " & Me.txtContract__Number)
    xlWs1.Cells.CopyFromRecordset rsExcel1
    xlWs1.Rows("1").Insert
    xlWs1.Cells(1).Value = "Contract Number"
    xlWs1.Cells(2).Value = "Status"
    xlWs1.Cells(3).Value = "Facility ID"
    xlWs1.Cells(4).Value = "Facility Name"
    

    xlApp1.Rows(1).Font.Bold = True
    xlWs1.UsedRange.Borders.LineStyle = 1
    xlApp1.Columns.AutoFit

    Set xlWs1 = Nothing
    Set xlWb1 = Nothing
    Set xlApp1 = Nothing 'End Excel

Open in new window

0
Comment
Question by:printmedia
[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
  • 2
  • 2
4 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38827295
change this part

   xlWs1.Cells(1).Value = "Contract Number"
    xlWs1.Cells(2).Value = "Status"
    xlWs1.Cells(3).Value = "Facility ID"
    xlWs1.Cells(4).Value = "Facility Name"

with

   xlWs1.Cells(1,1).Value = "Contract Number"
    xlWs1.Cells(2,1).Value = "Status"
    xlWs1.Cells(3,1).Value = "Facility ID"
    xlWs1.Cells(4,1).Value = "Facility Name"



post sample excel file  result
0
 

Author Comment

by:printmedia
ID: 38827355
Thanks capricorn.

But that just makes the words "Contract Number", "Status", "Facility ID" and "Facility Name" appear in Column A, but the data is still placed on row 1 Column A, Column B, Column C and Column D.

I have attached a sample excel file of how it will need to appear.
Sample-Excel.xlsx
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 38827385
try this
Dim xlWb1 'Script for creating excel files
Dim xlWs1

    Set xlApp1 = CreateObject("Excel.Application")
    Set xlWb1 = xlApp1.Workbooks.Add
    Set xlWs1 = xlWb1.Worksheets("Sheet1")
    xlApp1.Visible = True
    xlApp1.UserControl = True
    xlApp1.DisplayAlerts = True
    Dim objCon1  As New ADODB.Connection
    Dim objRS1 As ADODB.Recordset
    Set objRS1 = New ADODB.Recordset
    Dim rsExcel1 As DAO.Recordset
    Set rsExcel1 = CurrentDb.OpenRecordset("Select tblContract_Header.Contract_ID,tblContract_Header.Status,  tblContract_Detail.Facility_ID,tblContract_Detail.Name  FROM tblContract_Header INNER JOIN tblContract_Detail ON tblContract_Header.Contract_Number = tblContract_Detail.Contract_Number where tblContract_Header.[Contract_Number] = " & Me.txtContract__Number)
   
   ' xlWs1.Cells.CopyFromRecordset rsExcel1
   
    xlWs1.Cells(1,1).Value = "Contract Number"
	xlWs1.Cells(1,2).Value =  rsExcel1("Contract Number")
    xlWs1.Cells(2,1).Value = "Status"
	 xlWs1.Cells(2,2).Value = rsExcel1("Status")
    xlWs1.Cells(3,1).Value = "Facility ID"
	 xlWs1.Cells(3,2).Value = rsExcel1("Facility ID")
    xlWs1.Cells(4,1).Value = "Facility Name"
	xlWs1.Cells(4,2).Value = rsExcel1("Facility Name")
    

    xlApp1.Rows(1).Font.Bold = True
    xlWs1.UsedRange.Borders.LineStyle = 1
    xlApp1.Columns.AutoFit

    Set xlWs1 = Nothing
    Set xlWb1 = Nothing
    Set xlApp1 = Nothing 'End Excel
                                  

Open in new window

0
 

Author Closing Comment

by:printmedia
ID: 38827416
Thanks!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

624 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