We help IT Professionals succeed at work.

Converting VB6.0 to VBA help required in Ms Access

Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc asked
on
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

Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:

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.

John TsioumprisSoftware & Systems Engineer

Commented:

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.

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
Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:

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
Sorry people , I do not have the answer , the device require the following to be sent to it after configuring the following:


1  Header1

This is starting point or address packate which is mandatory to be part of the string to be sent

2.Header 2

This is ending point or address packate which is mandatory to be part of the string to be sent

3. Command1

This is command which must have ( Commstart, CommOPen, CommWrite, CommRead & Error checking)

4. Length
This tells the device the length of the content

5. Content ( Which the Json data we alread have)

6. CRC

This checks the integrity of the data sent


Finally the string to sent to the device will look like below:

<Header1><Haeder2<>CmdID<>Length<>Content<>CRC>

= 1114?2

Assuming the Lenght(Bytes) of is 864 then the full string will look like below:

= 11148642



As you can see the link below has no such facilities, but merely simple serial port data sending  

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


Anyone with full idea on how to add the above parameters will be highly appreciated and many people will benefit from the same.


Regards

Chris
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.