Public Sub CmdCwrite_Click()
Call CmdTotalClasses_Click
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rst As DAO.Recordset
Dim Company As New Dictionary
Dim strData As String
Dim Json As Object
Dim data As New Dictionary
Dim transactions As Collection
Dim itemCount As Long
Dim i As Long
Dim n As Integer
Dim z As Integer
Dim item As New Dictionary
Dim items As New Collection
Dim prm As DAO.Parameter
Dim qdf As DAO.QueryDef
Set db = CurrentDb
Set qdf = db.QueryDefs("QryJson")
For Each prm In qdf.Parameters
prm = Eval(prm.Name)
Next prm
Set rs = qdf.OpenRecordset(dbOpenSnapshot, dbSeeChanges)
Set qdf = Nothing
rs.MoveFirst
Do While Not rs.EOF
Set data = New Dictionary
Set transactions = New Collection
Set Company = New Dictionary
Company.Add "tpin", rs!suptpin.Value
Company.Add "bhfId", rs!bhfId.Value
Company.Add "cisInvcNo", rs!cisInvcNo.Value
Company.Add "orgInvcNo", Nz(rs!OrignalInvoiceNumber.Value, 0)
Company.Add "custTpin", rs!TPIN.Value
Company.Add "prcOrdCd", IIf((rs!NewprcOrdCd.Value = "0"), 0, "")
Company.Add "custNm", rs!Company.Value
Company.Add "salesTyCd", rs!SalesType.Value
Company.Add "rcptTyCd", rs!DocCodes.Value
Company.Add "pmtTyCd", rs!PaymentIDs.Value
Company.Add "salesSttsCd", "02"
Company.Add "cfmDt", rs!ActualDate.Value
Company.Add "salesDt", Format((rs!ShipDate), "yyyymmdd")
Company.Add "stockRlsDt", IIf(Len(rs!stockreleasing), rs!stockreleasing, Null)
Company.Add "cnclReqDt", Null
Company.Add "cnclDt", Null
Company.Add "rfdDt", Null
Company.Add "rfdRsnCd", rs!rfdRsnCding.Value
Company.Add "totItemCnt", Me.txtinternalaudit
Company.Add "taxblAmtA", IIf((Round(Nz(Me.txtclassA, 0), 2) = ""), CDbl(0), Round(Nz(Me.txtclassA, 0), 2))
Company.Add "taxblAmtB", IIf((Round(Nz(Me.txtclassB, 0), 2) = ""), CDbl(0), Round(Nz(Me.txtclassB, 0), 2))
Company.Add "taxblAmtC1", Round(Nz(Me.txtclassC1, 0), 2)
Company.Add "taxblAmtC2", Round(Nz(Me.txtclassC2, 0), 2)
Company.Add "taxblAmtC3", Round(Nz(Me.txtclassC3, 0), 2)
Company.Add "taxblAmtD", Round(Nz(Me.txtclassD, 0), 2)
Company.Add "taxblAmtRvat", Round(DSum("ReverseBasic", "QryJson", "[InvoiceID] =" & Me.CboEsdInvoices), 2)
Company.Add "taxblAmtE", Round(DSum("Expenses", "QryJson", "[InvoiceID] =" & Me.CboEsdInvoices), 2)
Company.Add "taxblAmtF", Round(DSum("TLTaxable", "QryJson", "[InvoiceID] =" & Me.CboEsdInvoices), 2)
Company.Add "taxblAmtIpl1", CDbl(0)
Company.Add "taxblAmtIpl2", CDbl(0)
Company.Add "taxblAmtTl", IIf((Round(DSum("TLTaxable", "QryJson", "[InvoiceID] =" & Me.CboEsdInvoices), 2) = ""), CDbl(0), Round(DSum("TLTaxable", "QryJson", "[InvoiceID] =" & Me.CboEsdInvoices), 2))
Company.Add "taxblAmtEcm", CDbl(0)
Company.Add "taxblAmtExeeg", CDbl(0)
Company.Add "taxblAmtTot", Round(DSum("TaxableTOT", "QryJson", "[InvoiceID] =" & Me.CboEsdInvoices), 2)
Company.Add "taxRtA", 16
Company.Add "taxRtB", 16
Company.Add "taxRtC1", 0
Company.Add "taxRtC2", 0
Company.Add "taxRtC3", 0
Company.Add "taxRtD", 0
Company.Add "taxRtE", 0
Company.Add "taxRtF", 10
Company.Add "taxRtIpl1", 0
Company.Add "taxRtIpl2", 0
Company.Add "taxRtTl", 0
Company.Add "taxRtEcm", 0
Company.Add "taxRtExeeg", 0
Company.Add "taxRtTot", 0
Company.Add "taxRtRvat", 16
Company.Add "taxAmtA", Round(Nz(Me.txtttaxclassA, 0), 2)
Company.Add "taxAmtB", Round(Nz(Me.txtttaxclassb, 0), 2)
Company.Add "taxAmtC1", 0
Company.Add "taxAmtC2", 0
Company.Add "taxAmtC3", 0
Company.Add "taxAmtD", 0
Company.Add "taxAmtE", 0
Company.Add "taxAmtF", Round(DSum("ServiceCharge", "QryJson", "[InvoiceID] =" & Me.CboEsdInvoices), 2)
Company.Add "taxAmtIpl1", 0
Company.Add "taxAmtIpl2", 0
Company.Add "taxAmtTl", Round(DSum("TLLevyTax", "QryJson", "[InvoiceID] =" & Me.CboEsdInvoices), 2)
Company.Add "taxAmtEcm", 0
Company.Add "taxAmtExeeg", 0
Company.Add "taxAmtTot", 0
Company.Add "taxAmtRvat", Round(DSum("ReversVAT", "QryJson", "[InvoiceID] =" & Me.CboEsdInvoices), 2)
Company.Add "totTaxblAmt", Round(Nz(Me.txttotaxableAB, 0), 2)
Company.Add "totTaxAmt", Round(Nz(Me.txttotaltaxAll, 0), 2) + Round(DSum("ServiceCharge", "QryJson", "[InvoiceID] =" & Me.CboEsdInvoices), 2) + Round(DSum("TLLevyTax", "QryJson", "[InvoiceID] =" & Me.CboEsdInvoices), 2)
Company.Add "totAmt", Round(Nz(Me.txtGrandtotal, 0), 2)
Company.Add "prchrAcptcYn", rs!prchrAcptcYn.Value
Company.Add "remark", rs!TheNotes.Value
Company.Add "regrId", "11999"
Company.Add "regrNm", rs!CreatedBy.Value
Company.Add "modrId", "45678"
Company.Add "modrNm", rs!CreatedBy.Value
Company.Add "receipt", data
data.Add "custTpin", rs!TPIN.Value
data.Add "custMblNo", rs!Phone.Value
data.Add "rptNo", 0
data.Add "trdeNm", rs!Company.Value
data.Add "adrs", rs!Address.Value
data.Add "topMsg", ""
data.Add "btmMsg", "Thank you for choosing us"
data.Add "prchrAcptcYn", rs!prchrAcptcYn.Value
Company.Add "itemList", transactions
'--- loop over all the items
itemCount = Me.txtinternalaudit
For i = 1 To itemCount
Set item = New Dictionary
transactions.Add item
item.Add "itemSeq", i
item.Add "itemCd", rs!itemCd.Value
item.Add "itemClsCd", rs!itemClsCd.Value
item.Add "itemNm", rs!ProductName.Value
item.Add "bcd", Null
item.Add "pkgUnitCd", "NT"
item.Add "pkg", 1
item.Add "qtyUnitCd", "U"
item.Add "qty", rs!Qty.Value
item.Add "prc", rs!UnitPrice.Value
item.Add "splyAmt", rs!Pricing.Value
item.Add "dcRt", Round(rs!Discount.Value, 2)
item.Add "dcAmt", Round(rs!Discamount.Value, 2)
item.Add "isrccCd", Null
item.Add "isrccNm", Null
item.Add "isrcRt", Null
item.Add "isrcAmt", Null
item.Add "vatCatCd", IIf((rs!TaxClassA.Value = ""), "D", rs!TaxClassA.Value)
item.Add "iplCatCd", "IPL1"
item.Add "tlCatCd", "TL"
item.Add "exciseCatCd", "EXEEG"
item.Add "taxblAmt", Round(rs!SupplierAmount.Value, 2)
item.Add "vatAmt", Round(rs!FinalTax.Value, 2) + Round(rs!ReversVAT.Value, 2) + Round(rs!TaxesExpenses.Value, 2)
item.Add "iplAmt", Null
item.Add "tlAmt", Round(rs!TLLevyTax.Value, 2)
item.Add "exciseAmt", Null
item.Add "totAmt", Round(rs!TotalAmount.Value, 2)
strData = JsonConverter.ConvertToJson(Company, Whitespace:=3)
rs.MoveNext
Next
Loop
n = FreeFile()
Open "C:\Users\necto\Desktop\Testing\test.txt" For Output As #n
Print #n, strData
Close #n