Results 1 to 3 of 3

Thread: Access to Excel

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129

    Question Access to Excel

    Hi All,

    I am trying to link fields from a recordset to specific cells within an excel worksheet. For example;

    "select * from test_table" returns:

    row1 row2 row3
    a b c
    d e f

    I would like to be able to say that the value from record 1 , row 1 is in cell A1 of an excel worksheet.

    This is what i am using so far;

    Dim dbCH As Database, rsCH As DAO.Recordset
    Dim CHApp As Object

    Set dbCH = CurrentDb
    Set rsCH = dbCH.OpenRecordset("test_table2", dbOpenDynaset)

    Set CHApp = CreateObject("Excel.Application")
    CHApp.Visible = True

    Dim chwb As Excel.Workbook
    Dim chws1 As Excel.worksheet

    Set chwb = Excel.Workbooks.Add
    Set chws1 = Excel.Worksheets.Add
    chws1.Range("A1").Value = "Test"

    ------------------------------------------------ problem here ---------------
    'chws1.Range("A2").CopyFromRecordset (rsCH)
    ---------------------------------------------------------------------------------
    any help appreciated

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Here you go:
    VB Code:
    1. ' From the tools menu in Access, click References & check
    2. ' "Microsoft DAO x.x" and "Microsoft Excel x.0" Object
    3. ' library references.
    4. Private Sub Command0_Click()
    5.     Dim dbCH As Database
    6.     Dim rsCH As DAO.Recordset
    7.    
    8.     Dim CHApp As Excel.Application
    9.     Dim chws1 As Excel.worksheet
    10.    
    11.     Set dbCH = CurrentDb
    12.     Set rsCH = dbCH.OpenRecordset("test_table2")
    13.    
    14.     Set CHApp = New Excel.Application
    15.    
    16.     CHApp.Visible = True
    17.     CHApp.Workbooks.Add
    18.  
    19.     Set chws1 = CHApp.Workbooks(1).Worksheets(1)
    20.    
    21.     chws1.Range("A1").Value = "Test"
    22.     chws1.Range("A2").CopyFromRecordset rsCH
    23.    
    24.     chws1.SaveAs "C:\Test.xls"
    25.     Set chws1 = Nothing
    26.    
    27.     CHApp.Workbooks(1).Close
    28.     CHApp.Quit
    29.     Set CHApp = Nothing
    30.    
    31.     rsCH.Close
    32.     Set rsCH = Nothing
    33.    
    34.     Set dbCH = Nothing
    35. End Sub
    I just want to add a couple of things here too to help:

    1) You don't need to enclose methods in parethesis in access (i.e.on the line with the error, place the recordset variable outside of the brackets - remove them completely).

    2) DAO is an older technology & if you can learn it's replacement (ADO) you'll get much quicker and more efficient code.

    3) When you get 5 mins, look up LATE BINDING and EARLY BINDING in the access/msdn help files to see why I've created the initial Excel object this way - If you're coding for different versions of Excel & did that purposely, then I'll say sorry now & shut up, but if not thentry to look this up.

    Cheers!

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2004
    Location
    North east UK
    Posts
    129
    Many thanks for your help, apologies for the long delay in replying. (pooter problems)

    Thanks again

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