I want to control the comevent cycle to display messages on good reads vs bad reads as is appropriate. However, after a bad read, the program wants to cycle thru twice, so I don't know where to put my messages and color changes. It almost acts like it's polling.
CODE
Option Explicit
Public prod As String
Public lot As String
Public scan As String
Public conn As ADODB.Connection
Public rs As ADODB.Recordset
Public cmd As ADODB.Command
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Form_Load()
'Set conn = DataEnvironment1.Connection1
'Set rs = DataEnvironment1.rsCommand1
On Error GoTo commerr
With MSComm1
MSComm1.CommPort = 1
MSComm1.PortOpen = True
MSComm1.Handshaking = comRTS
MSComm1.RThreshold = 13
MSComm1.RTSEnable = True
MSComm1.Settings = "9600,E,7,1"
MSComm1.InputLen = 0
End With
commerr:
If Err.Number = 8005 Then
MSComm1.PortOpen = True
Resume Next
End If
End Sub
Private Sub MSComm1_OnComm()
scan = ""
lot = ""
prod = ""
txtdesc.Text = ""
txtscandata.Text = ""
txtscandata.BackColor = vbWhite
txtdesc.Visible = False
txtdesc.BackColor = vbWhite
If MSComm1.CommEvent = comEvReceive Then
HandleInput
Else
txtscandata.Text = "No Data"
txtscandata.BackColor = vbRed
Beep
Beep
'Beep
Sleep 2000
End If
'Sleep 3000
'txtscandata.Text = "Ready to Scan"
'txtscandata.BackColor = vbWhite
'scan = ""
'lot = ""
'prod = ""
'txtdesc.Visible = False
'txtdesc.BackColor = vbWhite
'txtdesc.Text = ""
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub
Public Sub HandleInput()
Dim desc As String
Dim noread As String
noread = "NOREAD"
txtscandata.Text = MSComm1.Input
scan = Trim(txtscandata.Text)
scan = Trim(scan)
If Len(Trim(scan)) >= 13 And InStr(scan, noread) = 0 Then
GoTo gooddata
Else
txtscandata.Text = "Bad scan- scan it again!"
txtscandata.BackColor = vbRed
Beep
Beep
Beep
Sleep 4000
GoTo Exsub
End If
gooddata:
prod = Mid(scan, 1, 6)
lot = Mid(scan, 8, 13)
On Error GoTo Errorexit
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "pValidateWSScan"
cmd.Parameters.Append cmd.CreateParameter("@prod", adChar, adParamInput, 10, Trim(prod))
cmd.Parameters.Append cmd.CreateParameter("@lot", adChar, adParamInput, 20, Trim(lot))
cmd.Parameters.Append cmd.CreateParameter("@desc", adChar, adParamOutput, 20, Trim(desc))
cmd.Execute
desc = cmd(2)
If Trim(desc) = "Invalid Product!" Or Trim(desc) = "" Then
txtdesc.BackColor = vbRed
txtdesc.Visible = True
txtdesc.Text = "Invalid Product!"
Beep
Beep
Beep
Sleep 2000
Set cmd = Nothing
Else
txtscandata.BackColor = vbGreen
txtdesc.BackColor = vbGreen
txtdesc.Visible = True
txtdesc.Text = desc
Set cmd = Nothing
Sleep 6000
End If
Errorexit:
If Err.Number = 94 Then
Resume Next
End If
Exsub:
End Sub