Private Sub btnMakeECR_Click()
Me.OrderDate = Date
Dim rs As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rs = db.OpenRecordset("SELECT tblOrderDetails.Quantity,
tblOrderDetails.WeaponID " & vbCrLf & _
"FROM tblOrderDetails")
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim qrtAMT As Integer
qryAMT = DCount("[tblOrderDetails.OrderID]", "tblOrderDetails", "
[tblOrderDetails.OrderID] = [Forms]![frmOrderEntry]![OrderID]")
On Error Resume Next
DoCmd.DeleteObject acQuery, "testQry"
On Error GoTo 0
strSQL = "SELECT TOP " & qryAMT & " tblOrder.OrderID,
tblOrderDetails.OrderID, tblOrder.ArmorerID, tblOrder.RecieverID,
tblOrder.OrderDate, tblOrder.RecieverNumber, tblOrder.UnitName,
tblOrder.PickUpDate, tblOrder.Returned, tblOrder.Cleaned,
tblOrder.Notes, tblOrder.DateReturned, tblOrder.DateCleaned,
tblOrderDetails.OrderDetailID, tblWeaponGroups.WeaponID,
tblWeapons.InventoryID, tblWeapons.SerialNumber,
tblOrderDetails.WeaponID, tblWeapons.StockNumber,
tblWeapons.IssueCount, tblOrderDetails.Quantity, tblWeapons.Status
" & vbCrLf & _
"FROM (tblWeaponGroups INNER JOIN tblWeapons ON
tblWeaponGroups.WeaponID = tblWeapons.WeaponID) INNER JOIN
(tblOrder INNER JOIN tblOrderDetails ON tblOrder.OrderID =
tblOrderDetails.OrderID) ON tblWeaponGroups.WeaponID =
tblOrderDetails.WeaponID " & vbCrLf & _
"WHERE (((tblWeapons.Status)=""AVAILABLE"")) " & vbCrLf & _
"ORDER BY tblOrderDetails.WeaponID, tblWeapons.StockNumber,
tblWeapons.IssueCount, tblOrderDetails.Quantity;"
Set qdf = db.CreateQueryDef("testQry", strSQL)
DoCmd.OpenQuery ("testQry")
End Sub
This is my recordset and the query I want to run. I want to run the query for each record in the recordset using fields in the recordset as parameters in the query. I want to append all queries. The desired result would be to return a table or query with a number of sets of records equal to the number of records in the recordset an each set of records would contain an amount of records equal to its corresponding value in the record set
like this
RECORDSET QUERY END RESULT OF QUERY
A 2 A AFGHANISTAN 2 A AFGHANISTAN
B 1 A ALBANIA 2 A ALBANIA
C 4 A ALGERIA 1 B BAHRAIN
D 1 B BAHRAIN 4 C CAMBODIA
B BARBADOS 4 C CANADA
B CAMBODIA 4 C CHILE
C CANADA 4 C CHINA
C CHILE 1 D DENMARK
C CHINA
C CUBA
D DENMARK
D DOMINICA
I am using this for orders all my products are in the same table and are identified by Type, serial number and location.
ignore qryAMT, i was just testing the Dcount function and varying the results of my query with a defined integer. I am going to substitute the value of qryAMT with another expression something like (quantity field for table when dcount-dcount+loop step=loopstep)