Results 1 to 10 of 10
  1. #1
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    172

    Variable to reference a spreadsheet from Access

    I have button on an Access form that opens an existing Excel spreadsheet. All the code is listed below. But the one thing I am having trouble is the following line: "Worksheets("2015").Range("A5").CopyFromRecord set rst". Is there anyway the use variables instead of the "2015" and the "A5" which is the worksheet name and range?


    Private Sub btnAddToExcel_Click()
    Dim strYear As String
    Dim fn As String
    Dim ln As String
    Dim ph As String


    Dim db As Database
    Dim rst As Recordset
    Set db = DBEngine(0)(0)
    Set rst = db.OpenRecordset("Customers", dbOpenDynaset)

    Dim objXLApp As Object
    Dim objXLBook As Object
    Set objXLApp = CreateObject("Excel.Application")
    Set objXLBook = objXLApp.Workbooks.Open("C:\Dev\P&L.xls")
    strYear = "2014"
    With rst
    fn = ![FirstName]
    ln = ![LastName]
    ph = ![Phone]
    Worksheets("2015").Range("A5").CopyFromRecordset rst
    End With
    objXLApp.Application.Visible = True
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Yes. Try it. How do you want to set the variables?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    172
    The variable would come from another Access sub as a public variable like pubNum = 2015.

  4. #4
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    172
    I have already tried it. It don't work. I added the next 2 lines-
    Dim strYear As String
    strYear = "2015"
    and changed the following line as shown below.
    Worksheets(strYear).Range("A5").CopyFromRecordset rst

    I run the code, the first time it opens the spreadsheet but inserts NO data. Push the button again and I get -
    Run Time Error '1004: Method 'Worksheets' of object - Global Failed.

    Any ideas?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    The code works for me.

    However, the workbook must already exist with a worksheet named '2015'.

    If you need the worksheet created, that is another issue.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Try using objXLBook.Worksheets(strYear).Range("A5").CopyFrom Recordset rst

    I have found that Excel can be very fussy about not having qualifiers.

    In your case, it doesn't know which workbook Worksheets refers to, unless you specifically tell it which one.

  7. #7
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    172
    'Worksheets("2015").Range("A5").CopyFromRecordset rst' works and there is a worksheet named "2015". However, it does not work when I try to use a variable as:
    Dim strYear As String
    strYear = "2015"
    and changed the following line as shown below.
    Worksheets(strYear).Range("A5").CopyFromRecordset rst

    I run the code, the first time it opens the spreadsheet but inserts NO data. Push the button again and I get -
    Run Time Error '1004: Method 'Worksheets' of object - Global Failed.

    Any help is appreciated.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    As previously noted in post 5: If you need the worksheet created, that is another issue.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    lawdy is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Jan 2013
    Posts
    172
    It is already created.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Okay, also noted the technique works for me. Don't know why your code fails. If you want to provide files for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 0
    Last Post: 08-10-2011, 11:59 AM
  2. Replies: 8
    Last Post: 05-26-2011, 07:44 AM
  3. Replies: 14
    Last Post: 11-16-2010, 03:56 PM
  4. Variable within form control reference
    By Tyork in forum Programming
    Replies: 2
    Last Post: 10-13-2010, 09:55 AM
  5. Replies: 2
    Last Post: 05-09-2010, 04:10 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