In this series of articles I will demonstrate build an application that will save incoming caller id information to a SQL Server database. Then, I will build a Web service that exposes methods for reading and deleting the information. And, finally, I'll build a proxy class for consuming the Web service as well as a asp.net page that can be used to access the service. For this article I assume you have the following:
- A fairly cheap computer running any flavor of a Windows Server.
- A caller id enabled modem connected to the server.
- Caller id service from your local phone company.
- An always-on internet connection capable of serving Web content.
- A dns client if you do not have a static IP address.
I'll start by writing the Visual Basic (VB) program to run on the server and update the database with incoming callers.
The code assumes that you have MSComm32.OCX installed and registered on the machine that will be serving the caller id information.
To begin, open Visual Basic 6 and choose Standard EXE as the type of project.

Then add a label to the form and change its properties as shown in the following screenshot.

The most important part of this program is the use of the MSComm component. We'll need to add the component to the project. To do this, click the Project menu and choose Components.

Then check the Microsoft Comm Control 6.0 and click okay.

Now there will be a small yellow telephone icon in the toolbox.

Double click the telephone icon to add the component to the form. The icon will not be visible at runtime, it shows up on the form just to allow you to set properties of the component using the IDE.

Double click on the form to bring up the code for the form. By default the Form_load Sub's skeleton will be written.

Copy and paste the following code into the code area. (Overwrite the contents)
Public Call_Name As String
Public Call_Number As String
Private Sub Form_Load()
With MSComm1
.Settings = "9600, N, 8, 1" '// Baud, Parity, Data Bits, Stop Bits
.CommPort = 2 '// Change to the port of your modem
If .PortOpen = False Then '// If the port is not already open
.PortOpen = True '// open it
End if
.RThreshold = 0
.InputLen = 0
.Output = "AT#CID=1" & Chr(13) '// Send the modem the
'// appropriate AT command
'// to enable the modem to
'// sit and wait for
'// incoming calls and capture
'// the caller id information
'// Check the modem's documentation
'// for the correct string
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False '// Close the port when the
'// program is closed
End Sub
Private Sub MSComm1_OnComm()
Dim Buffer As String '// Will hold the string
'// from the modem
'// I was having problems dealing with
'// the returns and line feeds so I deleted
'// them from the Buffer
Buffer = Replace(MSComm1.Input, Chr(13), "")
Buffer = Replace(Buffer, Chr(10), "")
GetCallerInfo (Buffer)
End Sub
Private Sub GetCallerInfo(Caller_Id_string As String)
If InStr(Caller_Id_string, "NAME") > 0 Then
Call_Name = Mid(Caller_Id_string, (InStr(Caller_Id_string, "NAME = ") + 7), _
((InStr(Caller_Id_string, "NMBR = ") - 7) - InStr(Caller_Id_string, "NAME = ")))
End If
If InStr(Caller_Id_string, "NMBR") > 0 Then
Call_Number = Mid(Caller_Id_string, (InStr(Caller_Id_string, "NMBR = ") + 7))
End If
If Len(Call_Name) > 1 Or Len(Call_Number) > 1 Then
Database_Update()
End If
End Sub
Private Function Database_Update()
Dim Command_Text As String
Command_Text = "Insert Into tbl_Caller_Id (Call_Name, Call_Number)" & _
"Values ('" & Call_Name & "', '" & Call_Number & "')"
Dim myConnection As New ADODB.Connection
myConnection.Open "Driver={SQL Server};" & _
"Server=localhost;" & _
"Database=mydb;" & _
"Uid=;" & _
"Pwd=;"
myConnection.Execute (Command_Text)
myConnection.Close
Call_Name = "" '// Set the values back to nothing
Call_Number = ""
End Function

All that's left is to go to the file menu and choose the make .exe option.

A .zip file containing the form, code and project for the above application can be found here.
Run this program on the Web server that is connected to the Internet and a phone line using a caller id capable modem.
The program receives the caller id information from the modem, parses the information and dumps the output to a SQL server database. The code can be easily modified to dump to an Access database. If you have trouble with the modification e-mail me.
In part two I'll build a Web service that exposes methods for reading and deleting the caller id information from the database.