Create the following form:
Reference:
The Code is as follows:
Option Explicit
Dim WithEvents ServerObj As OPCServer
Dim WithEvents GroupObj As OPCGroup
Dim ItemObj As OPCItem
Private Sub Command_Start_Click ()
Dim OutText As String
On Error GoTo ErrorHandler
Command_Start.Enabled = False
Command_Read.Enabled = True
Command_Write.Enabled = True
Command_Exit.Enabled = True
OutText = "connecting to OPC server"
Set ServerObj = New OPCServer
ServerObj. Connect ("XXXSERVER") 'xxxserver is the name of an OPC server.
OutText = "add group"
Set GroupObj = ServerObj. OPCGroups. Add ("Group ")
OutText = "Adding an Item to the group"
Set ItemObj = GroupObj. OPCItems. AddItem ("XXXITEM", 1) 'xxxitem is the name of the added ITEM
Exit Sub
ErrorHandler: 'If an exception occurs, an error is returned.
MsgBox Err. Description + Chr (13) + _
OutText, vbCritical, "ERROR"
End Sub
Private Sub Command_Read_Click () 'synchronous read
Dim OutText As String
Dim myValue As Variant
Dim myQuality As Variant
Dim myTimeStamp As Variant
On Error GoTo ErrorHandler
OutText = "read ITEM value"
ItemObj. Read OPCDevice, myValue, myQuality, myTimeStamp
Edit_ReadVal = myValue
Edit_ReadQu = GetQualityText (myQuality)
Edit_ReadTS = myTimeStamp
Exit Sub