Use a MSComm control. Initialize it in the Form Load event (port #, port settings, open the port etc).
Then use the OnComm event to get incoming characters and trap errors. The OnComm event is raised everytime something happens at the port; you then test the CommEvent property to find out what happened and act accordingly.
Here's some typical initialization code:
'Initialize the MS Comm control
With comCtl
'Set the port
.CommPort = 2
'Set the settings
.Settings = "9600,N,1,1"
'Input buffer size in bytes
.InBufferSize = 1024
'Character threshold at which OnComm event fires
.RThreshold = 1
.SThreshold = 1
'Return the whole buffer with the input property
.InputLen = 0
'Input mode is text
.InputMode = comInputModeText
'3rd party hardware uses XOnXOff handshaking
.Handshaking = comXOnXOff
'Open the port
.PortOpen = True
End With
Here's an example of using the OnComm event to receive a string (in this case caller info such as name and address), parse it into database fields and create a record. The incoming chars are removed from the input buffer and stored in a global variable until the char that signifies the end of a record has been received. The code is actually running in Lotus Approach but the language is almost identical to VBA.
Sub Oncomm(Source As Mscomm)
'--- When the MS Comm control raises the OnComm event
Dim strErrMsg As String 'Error message
Dim intErr As Integer 'Error number
Dim intLocked As Integer 'fCreateIncident returns True in this parameter if the system record was locked
Dim strIn As String 'Buffer for received characters
Dim strMsg As String 'Msgbox text
Dim udtData As INCIDENTDATA 'Buffer the data when parsed into fields
On Error Goto ErrTrap
'Process the event
Select Case Source.CommEvent
'Errors
Case comEventBreak 'A Break was received
intErr = comEventBreak
strErrMsg = ERR_BREAK
Case comEventFrame 'Framing Error
intErr = comEventFrame
strErrMsg = ERR_FRAME
Case comEventOverrun 'Data Lost
intErr = comEventOverrun
strErrMsg = ERR_OVERRUN
Case comEventRxOver 'Receive buffer overflow
intErr = comEventRxOver
strErrMsg = ERR_RXOVER
Case comEventRxParity 'Parity Error
intErr = comEventRxParity
strErrMsg = ERR_RXPARITY
Case comEventTxFull 'Transmit buffer full
intErr = comEventTxFull
strErrMsg = ERR_TXFULL
Case comEventDCB 'Unexpected error retrieving DCB
intErr = comEventDCB
strErrMsg = ERR_DCB
'Communication events
Case comEvCD 'Change in the CD line
'Do nothing
Case comEvCTS 'Change in the CTS line
'Do nothing
Case comEvDSR 'Change in the DSR line
'Do nothing
Case comEvRing 'Change in the Ring Indicator
'Do nothing
Case comEvReceive 'Received RThreshold # of chars
'Remove the char from the buffer
strIn = Source.Input
If gintRx911 Then
'If we are receiving a data block
Select Case Asc(strIn)
Case 3 'It's the ETX character to indicate the end of data
'Set the receiving flag
gintRx911 = False
'Send an ACK character to acknowledge data received
'else the Power 911 will rety per its Retry setting
Source.Output = Chr$(6)
'Create the incident record
'Parse the data from the buffer into the udt
If fParseCallerData(gstrData, udtData) Then
'Data parsed successfully, create the record
If Not fCreateIncident(udtData, intLocked) Then
If intLocked Then
'Function failed because System record was locked
strMsg = "Cannot create an incident record for the incoming call." & fAddCF(2)
strMsg = strMsg & "Unable to return the next serial number from System.dbf."
strMsg = strMsg & " The system record was locked by another user."
Msgbox strMsg, 16, "System Error"
Else
'An unknown error occurred that didn't raise a run-time error
Msgbox "An unknown error occurred. Unable to create a new incident record.", 16, "System Error"
End If
End If
Else
'No data parsed, report the error
Msgbox "The Power 911 system has transmitted STX and ETX characters but no data could be extracted.", 16, "Data Parsing Error"
Goto ExitSub
End If
Case Else
'Any other character; add it to the buffer
gstrData = gstrData & strIn
End Select
Else
'We are not receiving data
'Check for the STX character which indicates the start of a data block
Select Case Asc(strIn)
Case 2 'It's the STX character to indicate the start of data
'Clear the data buffer
gstrData = ""
'Set the receiving flag
gintRx911 = True
Case Else
'Do nothing - ignore the character
End Select
End If
Case comEvSend 'There are SThreshold number of characters in the transmit buffer
'Do nothing
Case comEvEof 'An EOF charater was found in the input stream
'Do nothing
End Select
'If an error occurred, report it
If intErr > 0 Then
'Set the receive flag to False
gintRx911 = False
'Clear the data buffer
gstrData = ""
strErrMsg = strErrMsg & fAddCF(2) & "The incident report record could not be created."
'Raise the error
Error intErr, strErrMsg
End If
Goto ExitSub
ErrTrap:
Msgbox "Error " & Format$(Err, "#0"

& " - message: " & Error$, 16, "COM Port Error"
Resume ExitSub
ExitSub:
End Sub
Paul Bent
Northwind IT Systems