The first thing we need to code is the setVisitor
function. This function will allow us to add a new visitor when they visit our new web site. Notice the signature of the function. We will pass in both the first and last name of the new user as a variant. This is important!
Remember, everything must be a variant so we can have consistent VB and ASP data types.
Also, note the return value is a variant. When we add our new user to the database, we will grab the AutoNumber
value that the database assigns to the cookieID
field and return that to the caller, visitor.asp
. This will be used to identify the user the next time they log in.
Let's start by adding a function to our .DLL. Add this function to our visitors.cls
module. Please enter the code as shown:
Public Function setVisitor(ByVal firstName As Variant, ByVal lastName As Variant) As Variant
Dim adoConnection As ADODB.Connection
Dim adoRecordset As ADODB.Recordset
Dim connectString As String
connectString = "Provider=Microsoft.Jet.OLEDB.3.51;" & _
"Data Source=C:\BegDB\visitors.mdb"
Set adoConnection = CreateObject("ADODB.Connection")
Set adoRecordset = CreateObject("ADODB.Recordset")
adoConnection.Open connectString
adoRecordset.Open "SELECT * FROM SiteVisitors", adoConnection, _
adOpenDynamic, adLockOptimistic
With adoRecordset
.AddNew
!firstName = firstName
!lastName = lastName
!previousVisit = Now()
!totalVisits = 1
.Update
End With
'-- Return a unique ID so we can set the cookie
setVisitor = adoRecordset!cookieID
'-- Close the recordset and the connection
adoRecordset.Close
adoConnection.Close
End Function
We now are familiar with opening an ADO connection and creating a recordset consisting of all visitors to our site. So we are just retrieving the records from the database and adding a new one for this user. First, we call the .AddNew
method of the recordset that creates the record for us. Then we add the first and last names of the new user that were passed in when this method was called. The previousVisit
field is set to the system time using Now()
. And since this is the first visit, set the totalVisits
field to 1 and then we update the record to commit it to the database:
With adoRecordset
.AddNew
!firstName = firstName
!lastName = lastName
!previousVisit = Now()
!totalVisits = 1
.Update
End With
Once the record is added, Jet adds the unique AutoNumber in the cookieID
field. Since we are at the end of the recordset, and all new records are added to the end, we just take that new value and assign it to the return value of our function, setVisitor
. And remember, the return value is a variant. This is the number that will be added to the cookie we write to the user's PC:
'-- Return a unique ID so we can set the cookie
setVisitor = adoRecordset!cookieID
If this were a production system, we would movelast
so we don't rely on the default behavior of the recordset. But for the sake of clarity we know we will be at the end of the recordset, so we just take the value. We then proceed to close the ADO recordset and connection as usual. Not too bad.
Next, we need to code the getVisitor
method. This is used when our form, visitor.asp
, gets a valid cookie from the login.asp
form. The unique number is passed into the getVisitor
method (Ahmm, function) and is used to locate the information on the current visitor.