Library code snippets

Dialer

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

  1. 27 Apr 2007 at 20:55

    First off I am new to VB.  I am getting 5 errors with this code. 

    1. At "If Err() Then" - This gives an error message - Value of Type Microsoft.VisualBasic.ErrObject cann't be converted to 'Boolean'

    2. At "dummy = DoEvents()" - Error message - Name 'DoEvents' is not declared

    3. At "Temp = lblMsg" - Error message- Value of type 'System.Windows.Forms.Label' cannot be converted to 'String'

    4. At "lblMsg = "Dialing - " +  Number" - Error message- Value of type 'String' cannot be converted to 'System.Windows.Forms.Label'

    5. At "lblMsg = Temp" - Error message - Value of type 'String' cannot be converted 'System.Windows.Forms.Label'

    I am using VB 2005 Express Edition and Windows XP.  I would appreciate any help especially since I am very new to VB.  Thank you

  2. 24 Nov 2006 at 07:33

    and how do i get to verify my username and password this way??it does't do that with the code snippet given here..

     

  3. 24 Nov 2006 at 07:25

    hi again..

    another alternative.how can i invoke an already existing dial up on my VB application? i just wanna place an icon on my app so that on clicking it,it'l connect to the network...i'm working on windows.

    help!!!!

  4. 24 Nov 2006 at 06:30

    hi!

    i'm working with VB 6.0 and i'm trying to invoke a dial up in my application..i used the code snippet given here,but it does't seem to be working..the modem does't connect.wut am i doing wrong?

  5. 01 Jun 2006 at 16:21

    I am having the same problem with releasing the comm port.  So, whilest debugging it will work the first time (even get OK response from modem) and then nothing on subsequent attempts.

    Trying all of this in vs 2003.

  6. 04 Aug 2005 at 04:48
    I used the code and I did not get any error messages. But I don't know if it works. What kind of head set do I need to get.

    Thanks

    roywyma@sbcglobal.net
  7. 26 Jul 2005 at 23:58

    Hi,


    I am trying to get the dialer program to work.  I have followed all of the instructions, but get these error messages:


    (92): 'Option' statements must precede any declarations or 'Imports' statements.
    (121): Value of type 'Microsoft.VisualBasic.ErrObject' cannot be converted to 'Boolean'.
    (122): Method arguments must be enclosed in parentheses.
    (134): Name 'DoEvents' is not declared.
    (142): Method arguments must be enclosed in parentheses.
    (170): Value of type 'System.Windows.Forms.Label' cannot be converted to 'String'.
    (171): Value of type 'String' cannot be converted to 'System.Windows.Forms.Label'.
    (174): Method arguments must be enclosed in parentheses.
    (179): Value of type 'String' cannot be converted to 'System.Windows.Forms.Label'.


    My code is available at this link:
    http://cybereshop.lmhost2.com/dialer.vb
    Please reply with how I can fix this or updated code.  Thank you.

  8. 18 Oct 2004 at 03:09

    Respected Sir
    I have Tata Indicom Phone, that I am using for Internet Surfing.When I saw this code. I tried in vb6.0 Using the same phone. But I am getting messege like
    com1 : not available. Change the CommPort property to another port."


    I changed commport  value to 2
    alse to  3 and  4 . Still then I am getting this messege. Please help me...............
         

  9. 31 Aug 2003 at 23:46

    i'm using VS.net 2003 to develop the web mobile application, one of the functionalities is dialing phone number but i couldn't find MSComm control component you mentioned in VS.net 2003.Is there other ways to write this functionality beside using MSComm control?Please help...my assignment gonna due soon



  10. 15 Apr 2003 at 09:11

    I used this code but when i disconnect the call the modem
    can't accept a new call.You can't connect as if the modem isn't
    fully released.
    Do i need to add something?
    Thanks.

  11. 27 Jul 2002 at 03:01

    please hel me ...give me some tips and source code ..i'm try try to convert vb code and create some com server compnent ...but it's not ok ..it too slow and not retrive any data where i start dial up phone from browser ..can anyone help me ...


    this my sample com server , t think it's because loop-doevent


    Dim MSComm1 As Object
    Private betulker As Boolean
    Private FromModem As String
    Private outputString As String


    Public Function output() As String
    output = outputString
    End Function


    Public Sub setRTUstr(inputString As String)
       outputString = inputString
    End Sub


    Public Function getRTUstr() As String
       getRTUstr = outputString
    End Function


    Public Sub Dial(number As String, portmane As Integer, bauds As String)
       
       On Error Resume Next
       
       Dim DialString As String, dummy As String
       DialString = "ATDT" & "0" & number & Chr$(13)
       
       MSComm1.CommPort = CInt(portmane)
       MSComm1.Settings = CStr(baunds) + ",N,8,1"
       
       On Error Resume Next
       MSComm1.PortOpen = True
       If Err Then
          If MSComm1.InBufferCount Then
             FromModem = FromModem + MSComm1.Input
             MsgBox FromModem
                'Text1.Text = FromModem
                If InStr(FromModem, "BUSY") Then
                      MsgBox "Line tengah Busy"
                Else
                      If InStr(FromModem, "R") Then
                        betulker = True
                      Else
                        betulker = False
                      End If
              End If
          End If
       Loop


    End Sub


    Public Sub rtu()


    On Error GoTo ErrHandler
       Dim sLastString As String
       Dim sOutput As String
       Dim sBuffer As String
       
       With MSComm1
           .output = "*1R" & Chr$(13) & Chr$(13)
       End With
       
       Exit Sub
    ErrHandler:
       MsgBox "ada error..la kat sini,Sila semak dulu", vbCritical
       
    End Sub



    Public Sub semak()
    If betulker = True Then
      setRTUstr Right(FromModem, 10)
    Else
      MsgBox "belum lagi nie..Tunggu sikit Lagi", vbCritical, "amaran"
    End If
    End Sub


    Private Sub Class_Initialize()
     Set MSComm1 = CreateObject("MSCommLib.MSComm")
     MSComm1.InBufferSize = 1
     MSComm1.RThreshold = 1
     MSComm1.InputLen = 0
     MSComm1.OutBufferSize = 512
     MSComm1.SThreshold = 1
     
     MSComm1.NullDiscard = True
     MSComm1.RTSEnable = True
     MSComm1.DTREnable = True
    End Sub


    Private Sub Class_Terminate()
     MSComm1.PortOpen = False
    End Sub

  12. 07 May 2002 at 02:31

    what dont you understand? Do you know VB at all?


    P.S. I havent looked at this example so you'll need to be a little more specific

  13. 07 May 2002 at 01:58

    i need the whole source, meaing the .vbp .frm all the files for this program. I don't quite understand this......
    Thanks...

  14. 01 Jan 1999 at 00:00

    This thread is for discussions of Dialer.

Leave a comment

Sign in or Join us (it's free).

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!