Although I have an option to recapture the received Json manually from the report into both the parent and child tables, but I see this option to be somehow risk because wrong data can be easily captured we are human and this is also alot of work
So, in view of the above I have struggled to figure out how to do the insert in two tables at one go, below is my VBA code I have not yet tested it I will do that at 22:00 hours today when I have enough time. My worries are stated below, and so any possible correction will be appreciated.
(1) Opening the two record sets correctly (rs for the header & rst for the details)
(2) Iterating through the fields for the header & details as per response received , see response below or last page
(3) What if there four purchases invoices details received can this method save them individually NOT combined , preferably individually
(4) How to grab the parent table primary key and insert it in the child table as a foreign key, this way the record will remain linked
VBA CODE
Below is the response Json body expected from the internet :
Response
So, in view of the above I have struggled to figure out how to do the insert in two tables at one go, below is my VBA code I have not yet tested it I will do that at 22:00 hours today when I have enough time. My worries are stated below, and so any possible correction will be appreciated.
(1) Opening the two record sets correctly (rs for the header & rst for the details)
(2) Iterating through the fields for the header & details as per response received , see response below or last page
(3) What if there four purchases invoices details received can this method save them individually NOT combined , preferably individually
(4) How to grab the parent table primary key and insert it in the child table as a foreign key, this way the record will remain linked
VBA CODE
Code:
Private Sub CmdlocalPurchasesSearch_Click()
On Error GoTo Err_Handler
Dim n As Integer
Dim z As Integer
Dim JSON As Object
Dim item As Object
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rst As DAO.Recordset
Dim Request As Object
Dim strData As String
Dim stUrl As String
Dim Response As String
Dim requestBody As String
Dim Company As New Dictionary
Set Company = New Dictionary
stUrl = "http://hostname:8080/nector/trnsPurchase/selectTrnsPurchase"
Set Request = CreateObject("MSXML2.XMLHTTP")
Company.Add "tpin", "1002623668"
Company.Add "bhfId", "000"
Company.Add "lastReqDt", Format((Me.txtlastdateLocalPurchases), "YYYYMMDD000000")
strData = JsonConverter.ConvertToJson(Company, Whitespace:=3)
requestBody = strData
With Request
.Open "POST", stUrl, False
.setRequestHeader "Content-type", "application/json"
.send requestBody
Response = .ResponseText
End With
If Request.Status = 200 Then
MsgBox Request.ResponseText, vbCritical, "Internal Audit Manager"
Set rst = db.OpenRecordset("tblpurchases", dbOpenDynaset, dbSeeChanges)
Set rs = db.OpenRecordset("tblPurchasesDetails", dbOpenDynaset, dbSeeChanges)
Set JSON = ParseJson(Request.ResponseText)
'i = 1
For Each item In JSON("data")("saleList")
'Process data.
z = 1
'Purchases header details to saved in the parent table
rs.AddNew
rs("OurTPIN") = item("spplrTpin")
rs("bhfId") = item("spplrBhfId")
rs("spplrInvcNo") = item("spplrInvcNo")
rs("rcptTyCd") = item("rcptTyCd")
rs("pmtTyCd") = item("pmtTyCd")
rs("cfmDt") = item("cfmDt")
rs("pchsDt") = item("salesDt")
rs("wrhsDt") = item("stockRlsDt")
rs("totItemCnt") = item("totItemCnt")
rs("taxblAmtA") = item("taxblAmtA")
rs("taxblAmtB") = item("taxblAmtB")
rs("taxblAmtC1") = item("taxblAmtC1")
rs("taxblAmtC2") = item("taxblAmtC2")
rs("taxblAmtC3") = item("taxblAmtC3")
rs("taxblAmtD") = item("taxblAmtD")
rs("taxRtA") = item("taxRtA")
rs("taxRtB") = item("taxRtB")
rs("taxRtC1") = item("taxRtC1")
rs("taxRtC2") = item("taxRtC2")
rs("taxRtC3") = item("taxRt.C3")
rs("taxRtD") = item("taxRtD")
rs("taxRtB") = item("taxRtB")
rs("taxAmtA") = item("taxAmtA")
rs("taxAmtB") = item("taxAmtB")
rs("taxAmtC1") = item("taxAmtC1")
rs("taxAmtC2") = item("taxAmtC2")
rs("taxAmtC3") = item("taxAmtC3")
rs("taxAmtD") = item("taxAmtD")
rs("totTaxblAmt") = item("totTaxblAmt")
rs("totTaxAmt") = item("totTaxAmt")
rs("totAmt") = item("totAmt")
rs("remark") = item("remark")
rs("regrNm") = "Admin"
rs("regrId") = "Admin"
rs("modrNm") = "Admin"
rs("modrId") = "Admin"
rs.Update
'Purchases details lines to saved in the child table
rst.AddNew
rst("itemSeq") = item("itemList")("itemSeq")
rst("itemCd") = item("itemList")("itemCd")
rst("itemClsCd") = item("itemList")("itemClsCd")
rst("itemNm") = item("itemList")("itemNm")
rst("bcd") = item("itemList")("bcd")
rst("pkg") = item("itemList")("pkg")
rst("qtyUnitCd") = item("itemList")("qtyUnitCd")
rst("qty") = item("itemList")("qty")
rst("prc") = item("itemList")("prc")
rst("splyAmt") = item("itemList")("splyAmt")
rst("dcRt") = item("itemList")("dcRt")
rst("dcAmt") = item("itemList")("dcAmt")
rst("taxblAmt") = item("itemList")("taxblAmt")
rst("taxAmt") = item("itemList")("vatAmt")
rst("totAmt") = item("itemList")("totAmt")
rst("PurchID") = DLast("PurchID", "tblpurchases")
rst.Update
z = z + 1
Next
rs.Close
rst.Close
Set rs = Nothing
Set rst = Nothing
Set db = Nothing
Set JSON = Nothing
Set item = Nothing
Exit Sub
End If
Exit_CmdlocalPurchasesSearch_Click:
Exit Sub
Err_Handler:
MsgBox Err.Number & Err.Description, vbExclamation, "Error"
Resume Exit_CmdlocalPurchasesSearch_Click
End Sub
Below is the response Json body expected from the internet :
Response
Code:
{
"resultCd": "000",
"resultMsg": "It is succeeded",
"resultDt": "20240308182347",
"data": {
"saleList": [
{
"spplrTpin": "1002623668",
"spplrNm": "CHRISTOPHER HANKWEMBO",
"spplrBhfId": "000",
"spplrInvcNo": 610,
"rcptTyCd": "S",
"pmtTyCd": "01",
"cfmDt": "2024-03-08 09:16:08",
"salesDt": "20240308",
"stockRlsDt": "2024-03-08 09:16:08",
"totItemCnt": 1,
"taxblAmtA": 0,
"taxblAmtB": 360,
"taxblAmtC1": null,
"taxblAmtC2": null,
"taxblAmtC3": 0,
"taxblAmtD": 0,
"taxblAmtRvat": null,
"taxblAmtE": null,
"taxblAmtF": null,
"taxblAmtIpl1": null,
"taxblAmtIpl2": null,
"taxblAmtTl": null,
"taxblAmtEcm": null,
"taxblAmtExeeg": null,
"taxblAmtTot": null,
"taxRtA": 16,
"taxRtB": 16,
"taxRtC1": null,
"taxRtC2": null,
"taxRtC3": null,
"taxRtD": 0,
"taxRtE": null,
"taxRtRvat": null,
"taxRtF": null,
"taxRtIpl1": null,
"taxRtIpl2": null,
"taxRtTl": null,
"taxRtEcm": null,
"taxRtExeeg": null,
"taxRtTot": null,
"taxAmtA": 0,
"taxAmtB": 0,
"taxAmtC1": null,
"taxAmtC2": null,
"taxAmtC3": 49.66,
"taxAmtD": 0,
"taxAmtRvat": null,
"taxAmtE": null,
"taxAmtF": null,
"taxAmtIpl1": null,
"taxAmtIpl2": null,
"taxAmtTl": null,
"taxAmtEcm": null,
"taxAmtExeeg": null,
"taxAmtTot": null,
"totTaxblAmt": 300,
"totTaxAmt": 49.66,
"totAmt": 300,
"remark": null,
"itemList": [
{
"itemSeq": 1,
"itemCd": "ZM2NTU00000014",
"itemClsCd": "40000014",
"itemNm": "CEMENT",
"bcd": null,
"pkgUnitCd": "NT",
"pkg": 1,
"qtyUnitCd": "U",
"qty": 2,
"prc": 150,
"splyAmt": 150,
"dcRt": 0,
"dcAmt": 0,
"vatCatCd": "B",
"iplCatCd": null,
"tlCatCd": null,
"exciseTxCatCd": null,
"taxblAmt": 360,
"vatAmt": 49.66,
"iplAmt": null,
"tlAmt": null,
"exciseTxAmt": null,
"totAmt": 360
}
]
},