ACC: How to Dial a Phone Number from MS Access 95/97

ID: Q148857

The information in this article applies to:

SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article shows you how to create a sample user-defined Visual Basic for Applications procedure, DialNumber(), that you can use to dial a telephone number from Microsoft Access by using your computer's modem. This method uses Microsoft Windows application programming interface (API) procedure calls.

NOTE: Although this procedure works in Microsoft Access 7.0 and 97, those versions have an AutoDialer feature that you can use for dialing telephone numbers. Note, however, that the AutoDialer feature may not work on all Windows NT systems because it uses telephony application programming interface (TAPI) procedures to dial a phone, and TAPI is not included with Windows NT 3.51. This article does not use TAPI; therefore, the procedure in this article works on all Win32 systems.

For more information about the AutoDialer feature of Microsoft Access 7.0 and 97, search the Help Index for "AutoDialer."

For more information about using TAPI in Windows 95 and Windows NT 4.0, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q141625
   TITLE:      ACC: Using TAPI to Dial a Phone Under Win95/NT 4.0 (95/97)

MORE INFORMATION

Communications under Win32 have been simplified to use standard Windows file functions, such as CreateFile(), WriteFile(), and CloseHandle().

The following steps show you how to create and use the DialNumber() function in Microsoft Access.

1. Open the sample database Northwind.mdb.

2. Create a module and type the following line in the Declarations

   section.

   NOTE: You may have some Microsoft Windows API functions defined in an
   existing Microsoft Access library; therefore, your declarations may be
   duplicates. If you receive a duplicate procedure name error message,
   remove or comment out the declarations statement in your code.

   NOTE: All Declare statements must be typed exactly as shown, including
   capitalization, because Win32 names are case-sensitive. To help
   eliminate errors by you or others who use your Declare statements,
   create Alias clauses for Declare statements that do not have an
   existing Alias. As long as the Alias is correctly spelled and
   capitalized, it doesn't matter how the function name is capitalized.

      Option Explicit
      Declare Function WriteFile& Lib "kernel32" _
         (ByVal hFile As Long, lpBuffer As Any, _
         ByVal nNumberOfBytesToWrite&, _
         lpNumberOfBytesWritten&, ByVal lpOverlapped&)
      Declare Function CreateFile& Lib "kernel32" Alias "CreateFileA" _
         (ByVal lpFileName$, ByVal dwDesiredAccess&, _
         ByVal dwShareMode&, ByVal lpSecurityAttributes&, _
         ByVal dwCreationDisposition&, ByVal dwFlagsAndAttributes&, _
         ByVal hTemplateFile&)
      Declare Function CloseHandle& Lib "kernel32" (ByVal hObject&)
      Declare Function FlushFileBuffers& Lib "kernel32" (ByVal hFile&)

      ' The number of seconds to wait for the modem to dial before
      ' .. resetting the modem. If the phone hangs up prematurely
      ' .. try increasing this value by small increments.
      Public Const WAITSECONDS = 4

      Public Const ID_CANCEL = 2
      Public Const MB_OKCANCEL = 1
      Public Const MB_ICONSTOP = 16, MB_ICONINFORMATION = 64

3. Type the following procedure:

      Function DialNumber(PhoneNumber, CommPort As String)
      ' PURPOSE: To dial a telephone number using the computer's modem
      ' ARGUMENTS:
      '    PhoneNumber: The telephone number to dial
      '    CommPort: The communications port the modem is connected
      '              to. Typically, modems are found on COM2, however,
      '              they can be configured for any COM port.
      '
      ' EXAMPLE:
      '    Type the following in the Immediate window using a modem
      '    connected to the COM2 port:
      '
      '       ? DialNumber("555-1212", "COM2")
      '
      ' ***********************************************************

         Dim Msg As String, MsgBoxType As Integer, MsgBoxTitle As String
         Dim bModemCommand(256) As Byte, ModemCommand As String
         Dim OpenPort As Long
         Dim RetVal As Long, RetBytes as Long, i as integer
         Dim StartTime

         ' Ask the user to pick up the phone.
         Msg = "Please pickup the phone and choose OK to dial " _
            & PhoneNumber
         MsgBoxType = MB_ICONINFORMATION + MB_OKCANCEL
         MsgBoxTitle = "Dial Number"
         If MsgBox(Msg, MsgBoxType, MsgBoxTitle) = ID_CANCEL Then
            Exit Function
         End If

         ' Open the communications port for read/write (&HC0000000).
         ' Must specify existing file (3).
         OpenPort = CreateFile(CommPort, &HC0000000, 0, 0, 3, 0, 0)
         If OpenPort = -1 Then
            Msg = "Unable to open communication port " & CommPort
            GoTo Err_DialNumber
         End If

         ' Send the telephone number to the modem.
         ModemCommand = "ATDT" & PhoneNumber & vbCrLf
         ' Pack the string in a Byte array.
         For i = 0 To Len(ModemCommand) - 1
            bModemCommand(i) = Asc(Mid(ModemCommand, i + 1, 1))
         Next

         ' Write the string to the Com port.
         RetVal = WriteFile(OpenPort, bModemCommand(0), _
            Len(ModemCommand), RetBytes, 0)
         If RetVal = 0 Then
            Msg = "Unable to dial number " & PhoneNumber
            GoTo Err_DialNumber
         End If

         ' Flush the buffer to make sure it actually wrote
         RetVal = FlushFileBuffers(OpenPort)

         ' Wait WAITSECONDS seconds for the phone to dial.
         StartTime = Timer
         While Timer < StartTime + WAITSECONDS
            DoEvents
         Wend

         ' Reset the modem and take it off line.
         ModemCommand = "ATH0" & vbCrLf
         ' Pack the byte array again.
         For i = 0 To Len(ModemCommand) - 1
            bModemCommand (i) = Asc(Mid(ModemCommand, i + 1, 1))
         Next
         RetVal = WriteFile(OpenPort, bModemCommand(0), _
            Len(ModemCommand), RetBytes, 0)

         'Flush the buffer again.
         RetVal = FlushFileBuffers(OpenPort)

         ' Close the communications port.
         RetVal = CloseHandle(OpenPort)

         Exit Function

      Err_DialNumber:      'This is not an On Error routine.
         Msg = Msg & vbCr & vbCr & _
            "Make sure no other devices are using Com port " & CommPort
         MsgBoxType = MB_ICONSTOP
         MsgBoxTitle = "Dial Number Error"
         MsgBox Msg, MsgBoxType, MsgBoxTitle

      End Function

4. Open the Employees form in Design view.

5. Add a command button next to the Home Phone field and set the following

   properties:

      Command Button:
         Name: btnDialPhone
         Caption: Dial
         OnClick: =DialNumber([HomePhone], "COM2")

6. View the form in Form view. To dial an employee's home phone number,
   click the Dial button.

REFERENCES

For information about how to dial a phone number in Microsoft Access 1.x and 2.0, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q93696
   TITLE     : ACC: How to Dial a Phone Number in Microsoft Access
               (1.x/2.0)

Additional query words: mapi
Keywords          : kbprg
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbhowto

Last Reviewed: November 20, 1998