Json 参考上一个关于VBA为什么不加载所有发票详细信息的问题

Json 参考上一个关于VBA为什么不加载所有发票详细信息的问题,json,vba,Json,Vba,除了前面的问题,我们在销售发票上仍然有相同的装载失败问题: 下面的VBA/Json仍然只加载一行或第一个产品详细信息行,而不是表中与该销售发票合作的所有产品详细信息行 我们希望下面的VBA能够根据参数加载发票详细信息: 示例:如果我们的发票号为0001,包含以下详细信息: Invoice Header -------------------------- Inv Number 0001 Date : 2019-10-10 Customer Name: Lukas Address : USA L

除了前面的问题,我们在销售发票上仍然有相同的装载失败问题:

下面的VBA/Json仍然只加载一行或第一个产品详细信息行,而不是表中与该销售发票合作的所有产品详细信息行

我们希望下面的VBA能够根据参数加载发票详细信息: 示例:如果我们的发票号为0001,包含以下详细信息:

Invoice Header
--------------------------
Inv Number 0001
Date : 2019-10-10
Customer Name: Lukas
Address : USA

Line Details
--------------------------------------------------
(1) Apple    Qty (20)  Unit cost(5) Total (100)
(2) Orange   Qty (30)  Unit cost(5) Total (600)
(3) Lemonade Qty (40)  Unit cost(5) Total (800)
上述细节必须全部显示在Json中,而不仅仅是第一项

Private Sub CmdSales_Click()

'  Const SQL_SELECT As String = "SELECT * FROM Qry3;"

  Dim coll As VBA.Collection
  Dim dict As Scripting.Dictionary
  Dim db As DAO.Database
  Dim rs As DAO.Recordset
  Dim fld As DAO.Field
  Dim qdf As DAO.QueryDef
  Dim prm As DAO.Parameter
  Dim root As Dictionary
    Set root = New Dictionary

    Dim transaction As Dictionary
    Dim transactions As Collection
    Dim item As Dictionary
    Dim items As Collection
    Dim invoice As Dictionary
    Dim invoices As Collection

    Dim i As Long
    Dim j As Long
    Set transactions = New Collection
  Set db = CurrentDb
  Set qdf = db.QueryDefs("Qry4")
For Each prm In qdf.Parameters
    prm = Eval(prm.Name)
Next prm
Set rs = qdf.OpenRecordset()

Set qdf = Nothing
 rs.MoveFirst
    Do While Not rs.EOF
        Set transaction = New Dictionary
        transaction.Add "PosSerialNumber", DLookup("PosSerialNumber", "Qry4", "Inv =" & Me.CboInv)
        transaction.Add "IssueTime", DLookup("IssueTime", "Qry4", "Inv =" & Me.CboInv)
        transaction.Add "Customer", DLookup("CustomerName", "Qry4", "Inv =" & Me.CboInv)
        transaction.Add "TransactionTyp", 0
        transaction.Add "PaymentMode", 0
        transaction.Add "SaleType", 0

        '--- loop over all the items
        Dim itemCount As Long
        itemCount = 2
        Set items = New Collection
        For i = 1 To itemCount
            Set item = New Dictionary
            item.Add "ItemID", i
            item.Add "Description", DLookup("Description", "Qry4", "Inv =" & Me.CboInv)
            item.Add "BarCode", DLookup("BarCode", "Qry4", "Inv =" & Me.CboInv)
            item.Add "Quantity", DLookup("Qty", "Qry4", "Inv =" & Me.CboInv)
            item.Add "UnitPrice", DLookup("unitPrice", "Qry4", "Inv =" & Me.CboInv)
            item.Add "Discount", DLookup("Discount", "Qry4", "Inv =" & Me.CboInv)

            '--- loop over all the invoices
            Dim invoiceCount As Long
            invoiceCount = 3
            Set invoices = New Collection
            For j = 1 To invoiceCount
                Set invoice = New Dictionary
                invoice.Add "Total", DLookup("TotalAmount", "Qry4", "Inv =" & Me.CboInv) + j
                invoice.Add "IsTaxInclusive", DLookup("Inclusive", "Qry4", "Inv =" & Me.CboInv)
                invoice.Add "RRP", DLookup("RRP", "Qry4", "Inv =" & Me.CboInv)
                invoices.Add invoice
            Next j
            item.Add "Taxable", invoices
            items.Add item
        Next i
        transaction.Add "Items", items
        transactions.Add transaction
        rs.MoveNext
    Loop
    root.Add "JSON Created", Now()
    root.Add "Transactions", transactions

    Dim json As String
    json = JsonConverter.ConvertToJson(root, Whitespace:=3)
    Debug.Print json

End Sub
上述代码的当前结果:

{
   "JSON Created": "2019-10-10",
   "Transactions": [
      {
         "PosSerialNumber": "102010",
         "IssueTime": "2019-09-15",
         "Customer": "J J Zingalume",
         "TransactionTyp": 0,
         "PaymentMode": 0,
         "SaleType": 0,
         "Items": [
            {
               "ItemID": 1,
               "Description": "Apple (Rgb 350 ML)",
               "BarCode": "6009803227328",
               "Quantity": 15,
               "UnitPrice": 41,
               "Discount": 0,
               "Taxable": [
                  {
                     "Total": 616,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 617,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 618,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  }
               ]
            },
            {
               "ItemID": 2,
               "Description": "Apple (Rgb 350 ML)",
               "BarCode": "6009803227328",
               "Quantity": 15,
               "UnitPrice": 41,
               "Discount": 0,
               "Taxable": [
                  {
                     "Total": 616,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 617,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 618,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  }
               ]
            }
         ]
      },
      {
         "PosSerialNumber": "102010",
         "IssueTime": "2019-09-15",
         "Customer": "J J Zingalume",
         "TransactionTyp": 0,
         "PaymentMode": 0,
         "SaleType": 0,
         "Items": [
            {
               "ItemID": 1,
               "Description": "Apple (Rgb 350 ML)",
               "BarCode": "6009803227328",
               "Quantity": 15,
               "UnitPrice": 41,
               "Discount": 0,
               "Taxable": [
                  {
                     "Total": 616,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 617,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 618,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  }
               ]
            },
            {
               "ItemID": 2,
               "Description": "Apple (Rgb 350 ML)",
               "BarCode": "6009803227328",
               "Quantity": 15,
               "UnitPrice": 41,
               "Discount": 0,
               "Taxable": [
                  {
                     "Total": 616,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 617,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 618,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  }
               ]
            }
         ]
      },
      {
         "PosSerialNumber": "102010",
         "IssueTime": "2019-09-15",
         "Customer": "J J Zingalume",
         "TransactionTyp": 0,
         "PaymentMode": 0,
         "SaleType": 0,
         "Items": [
            {
               "ItemID": 1,
               "Description": "Apple (Rgb 350 ML)",
               "BarCode": "6009803227328",
               "Quantity": 15,
               "UnitPrice": 41,
               "Discount": 0,
               "Taxable": [
                  {
                     "Total": 616,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 617,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 618,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  }
               ]
            },
            {
               "ItemID": 2,
               "Description": "Apple (Rgb 350 ML)",
               "BarCode": "6009803227328",
               "Quantity": 15,
               "UnitPrice": 41,
               "Discount": 0,
               "Taxable": [
                  {
                     "Total": 616,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 617,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  },
                  {
                     "Total": 618,
                     "IsTaxInclusive": "True",
                     "RRP": 52.8
                  }
               ]
            }
         ]
      }
   ]
}

所有发票详细信息必须按照参数查询显示,请尝试以下操作:

(为了清晰起见,多行)

注意过滤器参数还包括
i

"Inv =" & Me.CboInv & " AND LineItemID = " & CStr(i)
CStr
将数字转换为字符串


我假设您的行项目id列名为
LineItemID
。您的问题不清楚您是否有lineitemid列

为什么不使用一些断点和
Debug.Print
来计算代码中发生了什么。这将比我们尝试处理您的代码板快得多。当您分配项目(以及发票值)时,您只根据
Me.CboInv
进行查找,这永远不会改变,是吗?所以你只需要一遍又一遍地重复同一句话。您还需要根据
i
进行查找,但是您仅将其限制为两行。因此,假设您有一个项目行号,您需要将该筛选器添加到
DLookup
好的,我可以调试.print,但我的问题是我在这里哪里出错了?解决这个问题的办法是什么?我拥有的项目编号,但如何放入dlookup?您如何筛选
dlookup
中的
Me.CboInv
?我在下面贴了一个答案。现在我可以看到第2行,但第3行仍然缺失,那么总数也不正确,现在每个总数加1,2,3。请注意,总数应该在每个产品的末尾。并不是每个产品的三个总数都与产品总数相同,并且重复了几次。是否可以在线访问此应用程序以方便参考?然而,你几乎是在整理它!好的,现在所有三个产品都在那里,唯一的问题是,它打印相同的产品3次,因为它们是3,参数查询工作正常,有没有办法阻止这一点,并且不需要顶部带有日期的json头。你的评论对我来说没有意义。您是说所有行项目都具有相同的lineitemid吗?请编辑您的问题,并发布一个带有列名的
Qry4
中实际内容的示例。另外,请始终参考本问题中的内容。产品是否与商品相同?
"Inv =" & Me.CboInv & " AND LineItemID = " & CStr(i)