This example allows you to use the MS Comm control to dial a telephone number.
First, add the MSComm control to your form (you will need to add it to your toolbox by going to Project|Components). Next, add a label called lblMsg, and two command buttons called cmdDial and cmdCancel. Finally, add the code below. Note that you will need to change the Com port property (that is set in the Dial sub) to the COM port your modem uses. This is usually 1-4.
Option Explicit
' This flag is set when the user chooses Cancel.
Dim CancelFlag
Private Sub cmdCancel_Click()
' CancelFlag tells the Dial procedure to exit.
CancelFlag = True
cmdCancel.Enabled = False
End Sub
Private Sub Dial(Number As String)
Dim DialString As String, FromModem As String, dummy As
String
' AT is the Hayes compatible ATTENTION command and is
required to send commands to the modem.
' DT means "Dial Tone." The Dial command uses touch
tones, as opposed to pulse (DP = Dial Pulse).
' Numbers is the phone number being dialed.
' A semicolon tells the modem to return to command mode after
dialing (important).
' A carriage return, vbCr, is required when sending commands
to the modem.
DialString = "ATDT" + Number + ";" + vbCr
' Communications port settings.
' Assuming that a mouse is attached to COM1, CommPort is set
to 2
MSComm1.CommPort = 3
MSComm1.Settings = "9600,N,8,1"
' Open the communications port.
On Error Resume Next
MSComm1.PortOpen = True
If Err Then
MsgBox "COM" &
MSComm1.CommPort & ": not available. Change the CommPort property to
another port."
Exit Sub
End If
' Flush the input buffer.
MSComm1.InBufferCount = 0
' Dial the number.
MSComm1.Output = DialString
' Wait for "OK" to come back from the modem.
Do
dummy = DoEvents()
' If there is data in the buffer, then read
it.
If MSComm1.InBufferCount Then
FromModem = FromModem +
MSComm1.Input
' Check for
"OK".
If InStr(FromModem,
"OK") Then
'
Notify the user to pick up the phone.
Beep
MsgBox
"Please pick up the phone and either press Enter or click OK"
Exit Do
End If
End If
' Did the user choose Cancel?
If CancelFlag Then
CancelFlag = False
Exit Do
End If
Loop
' Disconnect the modem.
MSComm1.Output = "ATH" + vbCr
' Close the port.
MSComm1.PortOpen = False
End Sub
Private Sub cmdDial_Click()
Dim Number As String, Temp As String
cmdDial.Enabled = False
cmdCancel.Enabled = True
' Get the number to dial.
Number = InputBox("Enter phone number:", Number)
If Number = "" Then Exit
Sub
Temp = lblMsg
lblMsg = "Dialing - " + Number
' Dial the selected phone number.
Dial Number
cmdDial.Enabled = True
cmdCancel.Enabled = False
lblMsg = Temp
End Sub
Private Sub Form_Load()
' Setting InputLen to 0 tells MSComm to read the entire
' contents of the input buffer when the Input property
' is used.
MSComm1.InputLen = 0
End Sub
Comments