We help IT Professionals succeed at work.
Get Started

Converting VB6.0 to VBA help required in Ms Access

128 Views
Last Modified: 2020-01-28
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
Beep
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
         
            .AddNew
            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
            rs.Update
        End With
        Z = Z + 1
    Next
      
      rs.Close
      Set rs = Nothing
      Set db = Nothing
      Set JSONS = Nothing

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE