Not sure why you're using a timer to store the data, Why not store it as soon as it arrives?
As mentioned previously, you should buffer the data until a complete record has arrived, then process it and wait for the next record. Assuming your AIS receiver is conforming to standards, the $PSHI record and !ADIVDM sentence both terminate with a CR LF pair. You can use that knowledge to unblock the data appropriately.
Below is an idea you could work on
Code:
Option Explicit
Private cn As ADODB.Connection
Private Sub Form_Load()
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Database\DB.mdb"
MSComm1.Settings = "38400,N,8,1"
MSComm1.CommPort = 1
MSComm1.RThreshold = 1
MSComm1.PortOpen = True
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
cn.Close
Set cn = Nothing
MSComm1.PortOpen = False
End Sub
Private Sub MSComm1_OnComm()
Static strBuffer As String
Dim strData As String
Dim strSQL As String
Dim intPos As Integer
Dim boComplete As Boolean
Select Case MSComm1.CommEvent
Case comEvReceive
'
' Read the data, append it to the buffer
'
strData = MSComm1.Input
strBuffer = strBuffer & strData
Do
intPos = InStr(strBuffer, vbCrLf)
'
' Check if a complete record has been received
'
If intPos > 0 Then
'
' Yes - add it to the table
'
strSQL = "INSERT INTO ais_data (data) VALUES ('" & Mid$(strBuffer, 1, intPos - 1) & "'"
cn.Execute strSQL
'
' Is there anything else in the buffer?
'
If intPos + 2 > Len(strBuffer) Then
'
' Yes, move it to the front and go round the loop again
'
strBuffer = Mid$(strBuffer, intPos + 2)
Else
'
' No, flush the buffer and exit
'
strBuffer = ""
boComplete = True
End If
Else
'
' Complete record not yet received, wait for the next comEvReceive event
'
boComplete = True
End If
Loop Until boComplete = True
End Select
End Sub