Link to home
Create AccountLog in
Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScFlag 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
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

Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Have you seen this:


http://www.thescarms.com/vbasic/commio.aspx


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

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.

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

ASKER

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

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

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

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


http://www.thescarms.com/vbasic/commio.aspx
ASKER CERTIFIED SOLUTION
Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Flag of Zambia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Just in case some may still not understand what was required , then see below:

Header


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
items:
• 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.