Question : Ms Access how do I to run a query based off a table

How can I run a query multiple times with the parameter being a table?  Ie I have a table with 10 entries and I want the query to run 10 times one for each entry.

Answer : Ms Access how do I to run a query based off a table

I am not entirely clear on what you want to do, but here is some code that creates a filtered report for each record in a recordset:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
Private Sub PrintCustomReports()
'Created by Helen Feddema 17-Jan-2010
'Last modified 16-Mar-2010

On Error GoTo ErrorHandler

   Dim strQuery As String
   Dim strContactName As String
   Dim strFileName As String
   Dim strReport As String
   Dim strCurrentPath As String
   Dim strFileNameAndPath As String
   Dim lngID As Long
   Dim rpt As Access.Report
   Dim rstContacts As DAO.Recordset
   Dim strRecordSource As String
   Dim strSQL As String
   
   strRecordSource = "tblContacts"
   Set dbs = CurrentDb
   Set rstContacts = dbs.OpenRecordset("tblContacts")
   strCurrentPath = Application.CurrentProject.Path & "\"
   
   'This report has qrySingleContact as its record source
   strReport = "rptContact"
   strQuery = "qrySingleContact"
   
   With rstContacts
      Do While Not .EOF
         lngID = ![ContactID]
         strContactName = ![FirstName] & " " & ![LastName]
         Debug.Print "Processing Contact ID " & lngID
         strFileName = "Report for " & strContactName & ".pdf"
         strFileNameAndPath = strCurrentPath & strFileName
         Debug.Print "File name and path: " & strFileNameAndPath
         
         'Create filtered query
         strSQL = "SELECT * FROM " & strRecordSource & " WHERE " _
            & "[ContactID] = " & lngID & ";"
         Debug.Print "SQL for " & strQuery & ": " & strSQL
         lngCount = CreateAndTestQuery(strQuery, strSQL)
         Debug.Print "No. of items found: " & lngCount
         If lngCount = 0 Then
            GoTo NextContact
         End If
   
         'Print custom report
         DoCmd.OpenReport ReportName:=strReport, View:=acViewDesign
         Set rpt = Reports(strReport)
         rpt.RecordSource = strSQL
         DoCmd.OpenReport ReportName:=strReport, View:=acViewNormal
         
NextContact:
      Loop
   End With
   
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub
Random Solutions  
 
programming4us programming4us