Results 1 to 4 of 4
  1. #1
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108

    Cycle Through Parameters in VBA

    I have a report that is linked to a form's List Box. Users can select Product A, Product B, Product C, etc. on the form, and the report will populate with data for the corresponding product.



    I'd like to export each version of the report to Excel automatically, and I'm trying to figure out how to use VBA to enter the parameters. Is there a way to cycle through each value in the listbox?

    Here's my code so far.

    Code:
    
    Sub ExportReport()
    
    
    Dim ctr As String
    Dim BillingTemp As String
    Dim qryBilled As DAO.QueryDef
    
    
    
    
    Dim dbs As DAO.Database
    
    
    Set dbs = CurrentDb
    Set qryBilled = dbs.QueryDefs("MyQuery")
    'This query is linked to the form with the listbox.
    
    
    qryBilled.Parameters("Forms!Billing Workbook!listProducts").Value = "Product A"
    
    
    
    
    
    
    ctr = Format(Now(), "yyyymmddhhnnss")
    ExportPath = CheckExportPath() 
    
    
    
    
    BillingTemp = ExportPath & "\BillingTemp_" & ctr & ".xls"
    
    'Fails here. Prompts user to manually enter the parameter.
        DoCmd.OutputTo acOutputReport, "BilledPlanned", "Excel97-Excel2003Workbook(*.xls)", BillingTemp, False, "", , acExportQualityPrint
        
    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
    53,770
    This is a multi-select listbox?

    Example code:
    Code:
        Dim varItem As Variant
        'Loop through the ItemsSelected in the list box.    
        With Me.lstCategory
            For Each varItem In .ItemsSelected
                If Not IsNull(varItem) Then
                    'do something with the item
                    Debug.Print .ItemData(varItem)
                End If
            Next
        End With
    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
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108
    Thanks, that works!

  4. #4
    kestefon is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Nov 2013
    Posts
    108
    Actually, wait. I spoke too soon. My form is single-select, and the report that it feeds into must have just a single selection. (i.e. I can't have multiple products in the same report).

    Here's the query that feeds into the report. Essentially, I'd like to programmatically change "Forms![Billing Workbook]!listProducts" and export the report repeatedly until every product in the list box has been covered.

    Code:
    SELECT [RawData].DataType, [RawData].Media, [RawData].Product, [RawData].RevenueFROM RawData
    WHERE ((([RawData].Product)=Forms![Billing Workbook]!listProducts));
    Another way to think of it is: Instead of manually going to the form and selecting its entries from a listbox, I'd like to use VBA to select the items.

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

Similar Threads

  1. Replies: 7
    Last Post: 10-28-2013, 03:15 PM
  2. Asking For Parameters Twice
    By batowl in forum Reports
    Replies: 3
    Last Post: 03-08-2012, 03:01 PM
  3. Recordset cycle problem
    By free_style in forum Programming
    Replies: 3
    Last Post: 08-25-2011, 02:44 PM
  4. Cycle Text Boxes with a For next loop
    By Gary in forum Programming
    Replies: 3
    Last Post: 07-20-2010, 09:32 AM
  5. Cycle Time
    By Dargo in forum Forms
    Replies: 5
    Last Post: 02-26-2009, 05:14 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