Microsoft KB Archive/93696

= ACC: How to Dial a Phone Number in Microsoft Access (1.x/2.0) =

Article ID: 93696

Article Last Modified on 5/9/2003

-

APPLIES TO


 * Microsoft Access 1.0 Standard Edition
 * Microsoft Access 1.1 Standard Edition
 * Microsoft Access 2.0 Standard Edition

-



This article was previously published under Q93696



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

This article describes a sample user-defined Access Basic function, DialNumber, that you can use to dial a telephone number from Microsoft Access using your computer's modem. This method uses Microsoft Windows application programming interface (API) function calls.

Although this function will work with Microsoft Access version 2.0, version 2.0 has a new AutoDialer feature that can be used for dialing telephone numbers. For more information about the AutoDialer feature, search for &quot;AutoDialer&quot; using the Microsoft Access 2.0 Help menu.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x, or the &quot;Building Applications&quot; manual in version 2.0.



MORE INFORMATION
To dial a phone number in Microsoft Access, you can use Microsoft Windows API calls in the dynamic-link library (DLL) USER.EXE. There are 17 communication API calls available in USER.EXE, but only the following 3 are used to dial a phone number: OpenComm, CloseComm, and WriteComm.

The following steps demonstrate how to create and use the DialNumber function.

NOTE: In the following sample code, an underscore (_) is used as a line- continuation character. Remove the underscore from the end of the line when re-creating this code in Access Basic.

 Open the sample database NWIND.MDB.  Create a new Access Basic module with the following in the Declarations section:

     Option Explicit

' 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. Const WAITSECONDS = 4

Declare Function OpenComm Lib &quot;User&quot; (ByVal lpComName$, _        ByVal wInQueue%, ByVal wOutQueue%) As Integer Declare Function CloseComm Lib &quot;User&quot; (ByVal nCid%) As Integer Declare Function WriteComm Lib &quot;User&quot; (ByVal nCid%, _        ByVal lpBuf$, ByVal nSize%) As Integer

Const ID_CANCEL = 2 Const MB_OKCANCEL = 1 Const MB_ICONSTOP = 16, MB_ICONINFORMATION = 64   Add the following function to the module:

     ' *********************************************************** ' FUNCTION: DialNumber '     ' 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(&quot;555-1212&quot;, &quot;COM2&quot;) '     ' ***********************************************************      Function DialNumber (PhoneNumber, CommPort As String) Dim Msg As String, MsgBoxType As Integer, MsgBoxTitle As String Dim ModemCommand As String Dim OpenPort As Integer Dim RetVal As Integer Dim StartTime Dim CR As String: CR = Chr$(13) Dim LF As String: LF = Chr$(10)

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

' Open the communications port. OpenPort = OpenComm(CommPort, 1024, 128) If OpenPort < 0 Then Msg = &quot;Unable to open communication port &quot; & CommPort GoTo Err_DialNumber End If

' Send the telephone number to the modem. ModemCommand = &quot;ATDT&quot; & PhoneNumber & CR & LF        If WriteComm(OpenPort, ModemCommand, Len(ModemCommand)) < 0 Then Msg = &quot;Unable to dial number &quot; & PhoneNumber GoTo Err_DialNumber End If

' 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 = &quot;ATH0&quot; & CR & LF        RetVal = WriteComm(OpenPort, ModemCommand, Len(ModemCommand))

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

Exit Function

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

End Function  Open the Employees form in Design view.  Add a command button with the following properties to the form next to the Home Phone field.

     Microsoft Access 1.x:

ControlName: btnDialPhone Caption: Dial OnPush: =DialNumber([Home Phone], &quot;COM2&quot;)

Microsoft Access 2.0:

Name: btnDialPhone Caption: Dial OnClick: =DialNumber([Home Phone], &quot;COM2&quot;) </li> View the form in Form view. To dial an employee's home phone number, choose the Dial button.</li></ol>

<div class="references_section">