I want to implement the error handling mechanism in the code below, the objectives of the error handling are to give the error message and once the user click okay the code must exit the processing immediately. This will help to ensure that the stock levels are only updated once there no errors found in the code.
Code:
Public Sub CmdSavePosMasterQTY_Click()
Dim Cancel As Integer
If IsNull(Me.txtJsonReceived) Then
Beep
MsgBox "Please Select the Product name you want to transfer data to smart invoice", vbInformation, "Wrong data"
Cancel = True
Exit Sub
End If
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim Company As New Dictionary
Dim strData As String
Dim n As Long
Dim Json As Object
Dim itemCount As Long
Dim i As Long
Dim itemz As New Dictionary
Dim transactions As Collection
Dim prm As DAO.Parameter
Dim qdf As DAO.QueryDef
On Error Resume Next
Set db = CurrentDb
Set transactions = New Collection
Set qdf = db.QueryDefs("QryStockAdjustmentPOSSalesExportZRA")
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 Company = New Dictionary
Company.Add "tpin", Forms!frmLogin!txtsuptpin.Value
Company.Add "bhfId", Forms!frmLogin!txtbhfid.Value
Company.Add "regrNm", Forms!frmLogin!txtpersoning.Value
Company.Add "regrId", Forms!frmLogin!txtpersoning.Value
Company.Add "modrNm", Forms!frmLogin!txtpersoning.Value
Company.Add "modrId", Forms!frmLogin!txtpersoning.Value
Company.Add "stockItemList", transactions
'--- loop over all the items
itemCount = Me.txtinternalaudit
For i = 1 To itemCount
Set itemz = New Dictionary
transactions.Add itemz
itemz.Add "itemSeq", i
itemz.Add "itemCd", rs!itemCd.Value
itemz.Add "rsdQty", rs!CurrentStock.Value
strData = JsonConverter.ConvertToJson(Company, Whitespace:=3)
rs.MoveNext
Next
Loop
Dim Request As Object
Dim stUrl As String
Dim Response As String
Dim requestBody As String
stUrl = "http://localhost:8080/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Set Request = CreateObject("MSXML2.XMLHTTP")
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, vbInformation, "Internal Audit Manager"
End If
Dim rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("select resultCd FROM [tblPOSStocksSold] WHERE [ItemSoldID] = " & Me.txtJsonReceived, dbOpenDynaset)
Set Json = JsonConverter.ParseJson(Request.responsetext)
'Process data.
rst.Edit
rst![resultCd] = Json("resultCd")
rst.Update
'Cleanup:
rs.Close
Set db = Nothing
Set rs = Nothing
Set qdf = Nothing
Set prm = Nothing
Set Json = Nothing
Set Company = Nothing
Set itemz = Nothing
Set transactions = Nothing
Call YYYYYYYYYYYYYY
DoCmd.OpenReport "RptPosReceipts", acViewPreview, "", "", acNormal
DoCmd.RunCommand acCmdPrint
End Sub