Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Flag for Zambia asked on

Converting VB6.0 to VBA help required in Ms Access

I need to add the missing data part in the serial port VBA Ms Access  code below, I’m trying to convert the VB6.0 code to VBA so that the code can work properly, but unfortunately, I cannot go any further due to knowledge gap here:

Code VB6.0 for
Private Sub cmdWrite_Click()
Dim lBytesWritten As Long
Dim BytesToWrite As Long, wData As Integer
If setFlg = False Then
MsgBox "Please click the Setup Button to do the setup First"
Exit Sub
End If
wData = txtWdata.Text
BytesToWrite = comDCB.ByteSize
openSuccess = WriteFile(hPort, wData, BytesToWrite, lBytesWritten, comOverlap)
End Sub

Open in new window

VBA Code

' Write data to serial port.
    strData = JsonConverter.ConvertToJson(transaction, Whitespace:=3) & Chr$(13)
    lngSize = Len(strData)
    lngStatus = CommWrite(intPortID, strData)
    If lngStatus <> lngSize Then
    ' Handle error.
    End If

Open in new window

Missing data in VBA

Mandatory paramaters required in VBA code
Three local variables are declared at the beginning of this procedure. The first variable,
(1)      lBytesWritten, is the starting address that will store the data written to the serial port.
(2)      The BytesToWrite variable will store the number of bytes to be written to the port, and,
(3)       the wData variable will store the single byte of data to be written to the port.

The reading part of the code are as below:”
VB6.0 Reading Code

Private Sub cmdRead_Click()
Dim lBytesRead As Long
Dim BytesToRead As Long, rData As Integer
If setFlg = False Then
MsgBox "Please click the Setup Button to do the setup First"
Exit Sub
End If
BytesToRead = comDCB.ByteSize
openSuccess = ReadFile(hPort, rData, BytesToRead, lBytesRead, comOverlap)
txtRdata.Text = CStr(rData)
End Sub

Open in new window

Missing data in VBA

As in the Write event procedure, we must first declare three local variables.
(1)      The lBytesRead variable is the starting address that will store the data read from the port.
(2)      The BytesToRead variable will indicate the number of bytes to be read from the serial port.
(3)      The rData variable will store the value of the data to be read from the port.

VBA Code

lngStatus = CommRead(intPortID, strData, 14400)

Set rs = db.OpenRecordset("tblEfdReceipts")
    If lngStatus > 0 Then
' Process data.
Set JSONS = JsonConverter.ParseJson(strData)
    ElseIf lngStatus < 0 Then
MsgBox "There is no data to updated
        ' Handle error.
        On Error Resume Next
    End If
        ' Process data.
    Z = 2
  For Each item In JSONS
           With rs
            rs![TPIN] = item("TPIN")
            rs![TaxpayerName] = item("TaxpayerName")
            rs![Address] = item("Address")
            rs![ESDTime] = item("ESDTime")
            rs![TerminalID] = item("TerminalID")
            rs![InvoiceCode] = item("InvoiceCode")
            rs![InvoiceNumber] = item("InvoiceNumber")
            rs![FiscalCode] = item("FiscalCode")
            rs![TalkTime] = item("TalkTime")
            rs![Operator] = item("Operator")
            rs![Taxlabel] = item("TaxItems")("TaxLabel")
            rs![CategoryName] = item("TaxItems")("CategoryName")
            rs![Rate] = item("TaxItems")("Rate")
            rs![TaxAmount] = item("TaxItems")("TaxAmount")
            rs![VerificationUrl] = item("TaxItems")("VerificationUrl")
            rs![INVID] = Me.InvoiceID
        End With
        Z = Z + 1
      Set rs = Nothing
      Set db = Nothing
      Set JSONS = Nothing

Open in new window

Microsoft AccessVBAGolang

Avatar of undefined
Last Comment
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

8/22/2022 - Mon
Scott McDaniel (EE MVE )

Have you seen this:


This references how to work with serial ports in VBA, and might be a better (easier) fit than trying to convert VB6 code.

John Tsioumpris

I don't see anything in the VB6 code that will stop you from copy-paste it to Access VBA...probably the "missing" parts are code parts that are located somewhere in the sample.

Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

The device require the flowing:

1. Header1
2.Header 2
3. Command1
4. Length
5. Content ( Which the Json data we alread have)
6. CRC
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Scott McDaniel (EE MVE )

I'm not sure what you mean by your last comment. How is that relevant to your problem?

Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

I have seen the link below but the problem the require the following to factored in.

2.Header 2
3. Command1
4. Length
5. Content ( Which the Json data we alread have)
6. CRC

Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc

Just in case some may still not understand what was required , then see below:


A header is information that uses a defined format and appears at the beginning
of a message or other block of data. The header typically consists of a series of
fields, with each field having a defined size and location. The information
included in a header can vary. A header might include some or all of these
• Start-of-communication code.
• Address of the receiving node.
• Address of the sending node.
• Length of the data that follows the header.
• Checksum or other value used for error detection.
• Description of the type of data that follows.
• Time and date information.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.