使用记录集中的字段作为每个查询的参数,为记录集中的每条记录运行和追加查询
Posted
技术标签:
【中文标题】使用记录集中的字段作为每个查询的参数,为记录集中的每条记录运行和追加查询【英文标题】:Run and Append Query for each record in a recordset using fields in record set as parameters for each query 【发布时间】:2018-07-22 19:52:59 【问题描述】: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
这是我的记录集和我要运行的查询。我想使用记录集中的字段作为查询中的参数对记录集中的每条记录运行查询。我想附加所有查询。期望的结果是返回一个表或查询,其中记录集的数量等于记录集中的记录数量,每组记录包含的记录数量等于其在记录集中的对应值
喜欢这个
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
我将它用于订单我的所有产品都在同一个表中,并通过类型、序列号和位置进行标识。
忽略 qryAMT,我只是在测试 Dcount 函数并用定义的整数改变我的查询结果。我将用另一个表达式替换 qryAMT 的值,例如 (dcount-dcount+loop step=loopstep 时表的数量字段)
【问题讨论】:
【参考方案1】: Private Sub Command373_Click()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strQNT As String
Dim srtQNTV As Integer
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim strWID As Integer
strQNT = "Quantity"
Set db = CurrentDb
Set rs = db.OpenRecordset("tblOrderDetails", dbOpenForwardOnly)
DoCmd.OpenTable "tblECR", acViewNormal
On Error Resume Next
DoCmd.DeleteObject acTable, "tblECR"
On Error GoTo 0
On Error Resume Next
DoCmd.DeleteObject acQuery, "testQryA"
On Error GoTo 0
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM tblECR"
Do While Not rs.EOF
Debug.Print rs![Quantity]
Debug.Print rs![WeaponID]
strQNTV = rs!Quantity.Value
strWID = rs!WeaponID.Value
strSQL = "INSERT INTO tblECR ( OrderDetailID, OrderID, Quantity, ArmorerID,
RecieverID, RecieverNumber, UnitName, PickUpDate, SerialNumber,
Nomenclature, IssueCount, WeaponID, StockNumber, Status ) " & vbCrLf & _
"SELECT TOP " & strQNTV & " tblOrderDetails.OrderDetailID, tblOrderDetails.OrderID, tblOrderDetails.Quantity, tblOrder.ArmorerID, tblOrder.RecieverID, tblOrder.RecieverNumber, tblOrder.UnitName, tblOrder.PickUpDate, tblWeapons.SerialNumber, tblWeapons.Nomenclature, tblWeapons.IssueCount, tblWeapons.WeaponID, tblWeapons.StockNumber, tblWeapons.Status " & vbCrLf & _
"FROM tblOrder INNER JOIN (tblOrderDetails INNER JOIN tblWeapons ON tblOrderDetails.WeaponID = tblWeapons.WeaponID) ON tblOrder.OrderID = tblOrderDetails.OrderID " & vbCrLf & _
"WHERE (((tblOrderDetails.OrderID)=[Forms]![frmOrderEntry]![OrderID]) AND ((tblWeapons.WeaponID)= " & strWID & ") AND ((tblWeapons.Status)=""AVAILABLE"")) " & vbCrLf & _
"ORDER BY tblWeapons.IssueCount, tblWeapons.WeaponID, tblWeapons.StockNumber;"
On Error Resume Next
DoCmd.DeleteObject acQuery, "testQryA"
On Error GoTo 0
Set qdf = db.CreateQueryDef("testQryA", strSQL)
DoCmd.OpenQuery ("testQryA")
rs.MoveNext
Loop
rs.Close
DoCmd.SetWarnings True
End Sub
原来如此。有用。那里有一些垃圾代码,但它可以工作。谢谢大家的帮助。
【讨论】:
以上是关于使用记录集中的字段作为每个查询的参数,为记录集中的每条记录运行和追加查询的主要内容,如果未能解决你的问题,请参考以下文章