Results 1 to 2 of 2

Thread: Importing data to Excel

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    hongkong
    Posts
    251
    Dear Memebers,

    I wish to ask for your advice on a question:

    I am buidling a standalone MDI application which reads data from an Access database and then wants to plot charts on a child form using Excel.

    I heard I can use OLE container for controlling Excel for charting. But I first have to import data into it from my application. How can I do that?

    I would appreciate very much if you could kindly post a reply. Thanks

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Phew, here you go. You'll need refereences to the DAO project library & the Excel project library from the Project menu...
    Code:
    Private Sub Command1_Click()
        Dim XlsApp As New Excel.Application
        Dim Db As DAO.Database, Rs As DAO.Recordset, Rng As Variant
            Set Db = DAO.OpenDatabase("C:\A_Database_Name_here")
            Set Rs = Db.OpenRecordset("A_Table_Name_Here")
            Set Rng = Range("A1:A10")
        'declare variables for Excel file and Access file, and set up a range of cells to copy to
        
    With XlsApp
        .Workbooks.Open ("C:\A_FileName_Here.xls")
        .Worksheets(1).Select
             For Each cell In Rng
                    cell.Value = Rs!Column_Name_Here
                    Rs.movenext
            Next cell
        .DisplayAlerts = False
        .Workbooks(1).Save
        .Workbooks.Close
    End With
        'open excel file, loop through the access table column and
        'copy the value into the above mentioned range
    
        Set Rng = Nothing
        Set XlsApp = Nothing
        Set Rs = Nothing
        Db.Close
        Set Db = Nothing
        'quit Excel and Access, disregard variables to free memory
    End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width