KB 0002
In Visual Basic, how do I pass an integer value greater than 32767 to the ActiveX control's .DataWord property?


CATEGORY

ActiveX Controls

 

DESCRIPTION

Visual Basic Integers are in the range -32768..32767. An attempt to load a value above 32767 to the ActiveX control's .Data property will generate an overflow error.

For more information and additional conversions, refer to MSDN Article ID: 189323

 

SOLUTION

The function shown below will take a value greater than 32767 and convert it to its signed equivalent. Once converted, the signed number can be passed to the ActiveX control's .DataWord property. The underlying protocol ignores the sign and simply passes the 16 bit stream across the wire to the PLC.

Private Const OFFSET_2 = 65536
Private Const MAXINT_2 = 32767

Function UnsignedToInteger(Value As Long) As Integer
  If (Value < 0) Or (Value >= OFFSET_2) Then Error 6 ' Overflow

  If Value <= MAXINT_2 Then
    UnsignedToInteger = Value
  Else
    UnsignedToInteger = Value - OFFSET_2
  End If
End Function

Example:
Dim I as Integer, L as Long
L= 40000
I = UnsignedToInteger(L)
Asabtcp1.DataWord(0) = I 'The value 40000 is converted to -25536 for signed storage