Results 1 to 4 of 4
  1. #1
    geneS is offline Novice
    Windows 8 Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    2

    urgent help needed. Thanks a lot! for loop in module to run the same query many times

    Dear All,

    I am new to module and VBA.




    My database has 200 ID and associated hourly data for each ID. I want to extract hourly data for each ID to Excel file. Currently, I run the query and copy-paste the result to an Excel worksheet. Then change ID in the query and run again a with a particular UnitID and then export it to Excel (or copy-paste). There are more than 200 ID. It would be too time-consuming to do it manually. I don't know enough about Macros but my intuition is there might be a customer-written Macro looping though all 200 units running the same query with different UnitID and export the result automatically to the same Excel files (to different worksheet). Thanks!

    Gene

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,901
    Not macro, VBA.

    Each UnitID must be on separate worksheets in same workbook?

    Looping code to export each set of UnitID records to separate workbooks is easy with the TransferSpreadsheet or OutputTo methods. Exporting to a new sheet of same workbook is trickier. Requires opening workbook as an object in VBA and manipulating that object. Lots of web references on this. Here's one http://www.utteraccess.com/forum/exp...o-t539561.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.

  3. #3
    geneS is offline Novice
    Windows 8 Access 2010 32bit
    Join Date
    Apr 2014
    Posts
    2
    Thanks a lot!!

    I am new to VBA. Can I store a vector of 200 Unit ID in a variable is VBA? e.g., Dim Unit_ID_var = [203, 209, 289, ..., 98553]
    then loop through Unit_ID_var by Unit_ID_var(i) where i goes from 1 to 200.

    The Table or Query contains records for 200 Unit ID. My goal is to export each set of UnitID records to separate worksheet. The link you provided is very inspiring. Thanks a lot! I need to learn more about the syntax.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,901
    A variable is one way to control a loop.

    For i = UnitID_var To UnitID_var + 200
    'do this code
    Next i

    Or open a recordset and loop the records

    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT UnitID FROM tablename;")
    rs.MoveFirst
    While Not rs.EOF
    'code to open another recordset filtered to the UnitID of current record in rs and write data to Excel
    rs.MoveNext
    Wend
    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: 1
    Last Post: 08-26-2013, 10:11 AM
  2. Replies: 3
    Last Post: 11-20-2012, 09:52 AM
  3. Help needed urgent
    By diljot5394 in forum Access
    Replies: 1
    Last Post: 04-23-2012, 02:44 AM
  4. Urgent help needed on forms
    By syedalisajjad in forum Forms
    Replies: 9
    Last Post: 11-04-2011, 10:37 PM
  5. Replies: 3
    Last Post: 08-07-2010, 06:05 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