Results 1 to 4 of 4
  1. #1
    Bkper087 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    May 2014
    Posts
    81

    Filtering a RecordSet in vba using the CopyFromRecordset to Export to Excel

    I watched a YouTube video to help me get the code below, and it seems to work okay, except I need to filter the records based on a form control. I tried entering it as a criteria in the query; however, it returned a "Too few parameters: Expected 1" error. I thought it might work better to filter it in the vba code itself, but the section I added in and attempted to run locked up access and in the end didn't filter it. Any ideas?


    ___________

    Private Sub btnExporttoExcel_Click()
    Dim XL As Excel.Application
    Dim wbTarget As Workbook


    Dim qdfEA As QueryDef
    Dim rsEA As Recordset


    Set qdfEA = CurrentDb.QueryDefs("qryExpenseActual_ExcelExport" )


    Set rsEA = qdfEA.OpenRecordset()


    With rsEA


    .Filter = "[EventsID]=" & Me.EventsID
    End With



    Set XL = CreateObject("Excel.application")


    Set wbTarget = XL.Workbooks.Open("C:\Destination.xlsx")


    wbTarget.Worksheets("ExpenseActual").Cells.ClearCo ntents


    wbTarget.Worksheets("ExpenseActual").Cells(1, 1).CopyFromRecordset rsEA


    wbTarget.Save

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    A query would be the easiest way. In the criteria field it would be : =Forms!formname!fieldname

  3. #3
    Bkper087 is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    May 2014
    Posts
    81
    I tried that, and I got a "Too few parameters: Expected 1" error. Not sure why.

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Did you copy the criteria as typed above? Are you running the query in the VBA subroutine?

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

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2015, 04:15 PM
  2. Replies: 4
    Last Post: 07-14-2014, 02:26 PM
  3. Export Recordset to Excel
    By bimcompu in forum Programming
    Replies: 1
    Last Post: 01-08-2014, 05:53 PM
  4. Replies: 6
    Last Post: 12-03-2013, 11:14 PM
  5. Replies: 3
    Last Post: 03-12-2013, 11:34 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