kdwood
asked on
MSComm Control won't work with code called from a module.
Greetings,
I have an inventory control application that triggers the opening of mechanical drawers via the serial port. Using MSComm works great, as long as the code below is embedded as a subroutine behind the calling form. However, I would like to put this code into a module function, as it will be called from multiple locations within the application.
When I try the code below in a module, I get the following error: "Run-Time Error 424 . . . Object Required"
Any help would be appreciated.
Thank you in advance,
Keith
-------------------------- ------
CODE
-------------------------- ------
Function OpenDrawer()
myDrawer = Forms!frmSelectQty.txtDraw er
' Initialize Com Port Parameters
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 1
MSComm1.PortOpen = True
MSComm1.HandShaking = 2
' Check to see if Drawer is already open
If MSComm1.CTSHolding Then
GoTo OpenMe
Else
MsgBox "The Drawer is Currently Open. You must close the open drawer to continue.", vbCritical
Exit Function
End If
' Send String to Drawer to Open
OpenMe:
myTrigger = Chr$(myDrawer)
MSComm1.Output = myTrigger
MSComm1.PortOpen = False
End Function
I have an inventory control application that triggers the opening of mechanical drawers via the serial port. Using MSComm works great, as long as the code below is embedded as a subroutine behind the calling form. However, I would like to put this code into a module function, as it will be called from multiple locations within the application.
When I try the code below in a module, I get the following error: "Run-Time Error 424 . . . Object Required"
Any help would be appreciated.
Thank you in advance,
Keith
--------------------------
CODE
--------------------------
Function OpenDrawer()
myDrawer = Forms!frmSelectQty.txtDraw
' Initialize Com Port Parameters
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 1
MSComm1.PortOpen = True
MSComm1.HandShaking = 2
' Check to see if Drawer is already open
If MSComm1.CTSHolding Then
GoTo OpenMe
Else
MsgBox "The Drawer is Currently Open. You must close the open drawer to continue.", vbCritical
Exit Function
End If
' Send String to Drawer to Open
OpenMe:
myTrigger = Chr$(myDrawer)
MSComm1.Output = myTrigger
MSComm1.PortOpen = False
End Function
Function OpenDrawer()
Dim MyComm as MSComm
Set MyComm = Forms!frmSelectQty.MsComm1 .Object ' Asuuming in this form you have the MsCOmm contrl
myDrawer = Forms!frmSelectQty.txtDraw er
' Initialize Com Port Parameters
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 1
MSComm1.PortOpen = True
MSComm1.HandShaking = 2
Dim MyComm as MSComm
Set MyComm = Forms!frmSelectQty.MsComm1
myDrawer = Forms!frmSelectQty.txtDraw
' Initialize Com Port Parameters
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 1
MSComm1.PortOpen = True
MSComm1.HandShaking = 2
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
arcross,
Your suggestion worked perfectly. Thank you for the quick response.
Regards,
Keith
Your suggestion worked perfectly. Thank you for the quick response.
Regards,
Keith
Public Sub TEst()
Dim MyComm as MsCOmm
' Here refrence the form where you have dragged the control
Set MyComm = Forms.YourFormName.MsCommN
...
Álvaro