Results 1 to 2 of 2
  1. #1
    dssrun is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2010
    Posts
    83

    Generate Multiple Queries per Field value

    I have attempted to write VBA code that looks for all the distinct values in my department field and writes the number of queries per unique value as my criteria. However, I am not a pro coder and self taught myself VBA 6 months ago so I have attempted many times but I cannot figure it out. I am doing it this way because I need to export the different queries to one workbook but each has their own sheet. Thanks in advance!



    Code:
        Dim mySQL As String
        Dim myRecord As Recordset
        Dim myValue as String
        Dim myDB as Database
        Dim myField as Integer
    
        Set myDB = Currentdb
        Set myField = 'How do I set it to my departments field?
    
        For Each myValue in myField
            'Find all the values in the department column (there are 6 unique  
             departments but thousands of records)
             mySQL = "SELECT * FROM tblDept WHERE" & myField = myVariable"
             DoCmd.RunSQL mySQL
             Set myRecord = myDB.OpenRecordSet mySQL
             'Do I need to save the recordset, if so how?
             DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9,     
             myRecordSet, "My File Location",True
             Set myRecord = nothing
        Next
    
        Set myDB = nothing
        Set myField = nothing
    
    End Function

  2. #2
    dssrun is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Oct 2010
    Posts
    83
    If anyone ever needs the solution I figured it out

    Code:
    Dim myRst As DAO.Recordset
    Dim myRst2 As DAO.QueryDef
    Dim mySQL As String
    Dim mySQL2 As String
    Dim myCriteria As String
    
    mySQL = "SELECT DISTINCT Field1 FROM Table1"
    Set myRst = CurrentDb.OpenRecordset(mySQL)
    
    DoCmd.SetWarnings False
    
    On Error Resume Next
    
    Do While Not myRst.EOF
            myCriteria = myRst("Field1")
            mySQL2 = "SELECT * From Table1 WHERE Field1 =""" & myCriteria _
             & """"
             Set myRst2 = CurrentDb.CreateQueryDef(myCriteria, mySQL2)
             DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, myCriteria, "C:\My Documents\myfile.xls"
            Set myRst2 = Nothing
            myRst.MoveNext
    Loop
    
    Set myRst = Nothing
    
    End Sub

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

Similar Threads

  1. Multiple Queries to excel with Save As...
    By BED in forum Import/Export Data
    Replies: 7
    Last Post: 12-01-2010, 11:55 AM
  2. Sums of Multiple Queries
    By flsticks in forum Queries
    Replies: 5
    Last Post: 09-16-2010, 09:32 AM
  3. Multiple database queries
    By G0zzy in forum Access
    Replies: 4
    Last Post: 08-28-2009, 12:06 PM
  4. Recordsource with Multiple queries
    By darshita in forum Programming
    Replies: 1
    Last Post: 08-10-2009, 03:17 PM
  5. Help writing multiple queries
    By wz72n01 in forum Queries
    Replies: 1
    Last Post: 05-24-2009, 12:30 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