Results 1 to 4 of 4
  1. #1
    dfaulkner1 is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Jul 2015
    Posts
    5

    Splitting up a Query by a single field

    Hi, I am trying to split my query using VBA, I have got the following code but I'm just struggling to split the query up by the ledger field, would anybody be able to see what needs adding to the below script for it to do it

    Private Sub Command0_click()

    'Set record set from Ledger
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT DISTINCT Ledger FROM Ledger_No")
    atick = rs!Ledger
    'Start looping through all ledgers
    While Not (rs.EOF)
    While atick

    strPathNm = "\\buzz2\Office on Buzz2\Newonlineusers\"
    ledgernm = rs!Ledger
    strFileNm = strPathNm & ledgernm & "New Online Users" & ".xlsx"

    'Split the Query by Ledger
    DoCmd.OpenQuery "Newly_Created_Accounts", acViewNormal
    DoCmd.OutputTo acOutputQuery, "Newly_created_accounts", acFormatXLSX, "strFileNm"

    DoCmd.Close acQuery, "Newly_created_accounts"
    Wend
    rs.MoveNext



    Wend
    End Sub

    Thanks
    Dan

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    you wouldnt via VBA
    you split a query using different queries.

    Q1 to ledger,
    Q2 to other

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    This is the 3rd thread on this topic by this poster.

    Options:

    1. multiple queries or maybe one query with dynamic parameters but not sure those would work with the export wizard

    2. Excel automation as recommend in the first thread https://www.accessforums.net/access/...ets-53923.html

    3. export filtered report
    Code:
    Private Sub Command0_click()
    Dim strPathFile As String
    'Set recordset from Ledger
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT DISTINCT Ledger FROM Ledger_No")
    'Start looping through all ledgers
    While Not rs.EOF
    strPathFile = "\\buzz2\Office on Buzz2\Newonlineusers\" & rs!Ledger & "New Online Users" & ".xlsx"
    DoCmd.OpenReport "LedgerExport", acViewPreview, , "Ledger=" & rs!Ledger
    DoCmd.OutputTo acOutputReport, , acFormatXLSX, strPathFile
    DoCmd.Close acReport, "LedgerExport", acSaveNo
    rs.MoveNext
    Wend
    End Sub
    4. VBA code that modifies query object with QueryDefs

    The other thread https://www.accessforums.net/access/...ode-54031.html
    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.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Thanks june.

    I have not seen this "Split the Query" term before. Seems OP would get more interest by telling readers the issue in business terms.
    Congrats to ranman for interpreting the post.

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

Similar Threads

  1. Replies: 1
    Last Post: 02-13-2014, 01:50 AM
  2. Replies: 7
    Last Post: 06-04-2013, 01:10 PM
  3. Replies: 1
    Last Post: 06-05-2012, 04:10 PM
  4. Replies: 7
    Last Post: 03-14-2012, 10:56 AM
  5. Replies: 1
    Last Post: 07-07-2009, 01:00 PM

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