Connecting Tech Pros Worldwide Forums | Help | Site Map

Running SQL query for each record in table

Newbie
 
Join Date: Jul 2007
Posts: 1
#1: Jul 10 '07
Warning:: I am very new to programming, having never had to use VBA before, traditionally i am a SQL/Crystal Reports writer... with that in mind:

I am currently writing ar eport to extract data from a table into seperate excel worksheets and workbooks.

In the simpliest case the table_one has three fields, Account, Region, status.

each region requires it's own Excel file, and each status it's own worksheet. I have managed to perform the simple tasks of exporting the work data from access by manually setting up a code for each of the variable in region and status, but these are constantly changing.

How would i write a query that would extract this data for every varying region and status?


I have been looking around and managed to get to
Expand|Select|Wrap|Line Numbers
  1. Public Sub search_query()
  2.  
  3. Dim results As String
  4. Dim objXLSheet As Object
  5. Dim myrecordset As New ADODB.Recordset
  6.  
  7. myrecordset.Open "[table_one]", CurrentProject.Connection
  8.  
  9. Dim myfield As Object
  10.  
  11. For Each myfield In myrecordset.Fields
  12.  
  13. Set objXLSheet = objxlapp.worksheets("2")
  14. Else
  15. Next
  16. objXLSheet.Range("a2").CopyFromRecordset myrecordset
  17.  
  18. End Sub
  19.  
However this produces the first row from each column, maybe i need more books!

Reply