Forum Moderators: open

Message Too Old, No Replies

Short Date Compare to Current Insert vs Update

ASP Access Issues

         

Cin_Coder

3:39 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



I have a question on a task I'm working on and hope someone can provide a little help.

I have an ASP application with an Access DB backend. When the user logins into the site, I authenticate them and then do one of two things.

I update the number of Accesses to the site for the user and set the Last Access Date to current date in short format

or

if the Last Access Date is not in the current month, I insert a new record setting the Accesses count to one and adding a Last Access Date of current

The issue is that I don't know how to write the SQL to compare the current date month to the last access date month in the DB for the user and then update or insert based on the compare.

Here's what I've got, but it isn't doing the compare correctly.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../Connections/gm_location.asp" -->
<%
If Request.Form("vcodepwd") = "A5{5d87" then
Session("access") = "true"
Session("FirstName") = Request.Form("FirstName")
Session("LastName") = Request.Form("LastName")
Session("cintasRepLocCode") = Request.Form("cintasRepLocCode")
Dim AccessData
Dim MM_insert
Dim update_query
Dim insert_query
Dim todaysDate
todaysDate=date()
Dim rsLastAccess
Dim rsLastAccess_numRows

Set rsLastAccess = Server.CreateObject("ADODB.Recordset")
rsLastAccess.ActiveConnection = MM_gm_location_STRING
rsLastAccess.Source = "SELECT LastAccess FROM AccessData WHERE cintasRepLocCode='" & (Request.Form("cintasRepLocCode")) & "'"
rsLastAccess.CursorType = 0
rsLastAccess.CursorLocation = 2
rsLastAccess.LockType = 1
rsLastAccess.Open()

rsLastAccess_numRows = 0

If month(rsLastAccess)= month(now()) then
Set AccessData = Server.CreateObject("ADODB.Command")
AccessData.ActiveConnection = MM_gm_location_STRING
update_query="UPDATE AccessData SET LastAccess=#" & Date() & "#, Accesses = Accesses + 1 WHERE cintasRepLocCode='" & (Request.Form("cintasRepLocCode")) & "'"
AccessData.CommandText = update_query
AccessData.Execute
AccessData.ActiveConnection.Close
Set AccessData = Nothing
response.Redirect("http://home2.cintas.com/xpedio/groups/public/@gm/@misc/documents/general/MyCintasGMDefault.asp")

Else

If month(rsLastAccess)< month(now()) then
' *** Edit Operations: declare variables

Dim MM_editAction
Dim MM_abortEdit
Dim MM_editQuery
Dim MM_editCmd

Dim MM_editConnection
Dim MM_editTable
Dim MM_editRedirectUrl
Dim MM_editColumn
Dim MM_recordId

Dim MM_fieldsStr
Dim MM_columnsStr
Dim MM_fields
Dim MM_columns
Dim MM_typeArray
Dim MM_formVal
Dim MM_delim
Dim MM_altVal
Dim MM_emptyVal
Dim MM_i

MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
MM_abortEdit = false

' query string to execute
MM_editQuery = ""
%>
<%
' *** Insert Record: set variables

If (CStr(Request("MM_insert")) = "form1") Then

MM_editConnection = MM_gm_location_STRING
MM_editTable = "AccessData"
MM_editRedirectUrl = "http://home2.cintas.com/xpedio/groups/public/@gm/@misc/documents/general/MyCintasGMDefault.asp"
MM_fieldsStr = "cintasRepLocCode¦value¦FirstName¦value¦LastName¦value¦Accesses¦value¦LastAccess¦value"
MM_columnsStr = "cintasRepLocCode¦',none,''¦FirstName¦',none,''¦LastName¦',none,''¦Accesses¦Accesses + 1¦LastAccess¦'#" & Date() & "#"

' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, "¦")
MM_columns = Split(MM_columnsStr, "¦")

' set the form values
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(MM_i+1) = CStr(Request.Form(MM_fields(MM_i)))
Next

' append the query string to the redirect URL
If (MM_editRedirectUrl <> "" And Request.QueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And Request.QueryString <> "") Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
End If
End If

End If
' *** Insert Record: construct a sql insert statement and execute it

Dim MM_tableValues
Dim MM_dbValues

If (CStr(Request("MM_insert")) <> "") Then

' create the sql insert statement
MM_tableValues = ""
MM_dbValues = ""
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_formVal = MM_fields(MM_i+1)
MM_typeArray = Split(MM_columns(MM_i+1),",")
MM_delim = MM_typeArray(0)
If (MM_delim = "none") Then MM_delim = ""
MM_altVal = MM_typeArray(1)
If (MM_altVal = "none") Then MM_altVal = ""
MM_emptyVal = MM_typeArray(2)
If (MM_emptyVal = "none") Then MM_emptyVal = ""
If (MM_formVal = "") Then
MM_formVal = MM_emptyVal
Else
If (MM_altVal <> "") Then
MM_formVal = MM_altVal
ElseIf (MM_delim = "'") Then ' escape quotes
MM_formVal = "'" & Replace(MM_formVal,"'","''") & "'"
Else
MM_formVal = MM_delim + MM_formVal + MM_delim
End If
End If
If (MM_i <> LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End If
MM_tableValues = MM_tableValues & MM_columns(MM_i)
MM_dbValues = MM_dbValues & MM_formVal
Next
MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & ") values (" & MM_dbValues & ")"

If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close

If (MM_editRedirectUrl <> "") Then
Response.Redirect(MM_editRedirectUrl)

rsLastAccess.Close()
Set rsLastAccess = Nothing

End If
End If

End If
End If
End If
End If
%>

CaseyRyan

4:51 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Are these the comparisons that aren't working?

If month(rsLastAccess)= month(now()) then
...
Else
If month(rsLastAccess)< month(now()) then
...
End If
End If

Do you get an error or does it just always do the insert? Please explain.

-=casey=-

Cin_Coder

6:13 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Yes, those are the comparisons that aren't working. What it was doing before I moved some of the code around was updating the record not inserting a new line. Right now though it is throwing the following error :

Microsoft VBScript runtime error '800a000d'

Type mismatch

/gm_location/default1.asp, line 31

CaseyRyan

6:17 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Is the code above, the code that exists now or before you moved stuff around?

Also, is there always a date value in that field? Is it ever null or blank? If so, that might give you the Type Mismatch error.

-=casey=-

Cin_Coder

6:23 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



When I move the code around to below, I now get the following error:

Microsoft VBScript runtime error '800a005b'

Object variable not set

/gm_location/default1.asp, line 32

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../Connections/gm_location.asp" -->
<%
If Request.Form("vcodepwd") = "A5{5d87" then
Session("access") = "true"
Session("FirstName") = Request.Form("FirstName")
Session("LastName") = Request.Form("LastName")
Session("cintasRepLocCode") = Request.Form("cintasRepLocCode")
Dim AccessData
Dim MM_insert
Dim update_query
Dim insert_query
Dim todaysDate
todaysDate=date()
Dim rsLastAccess
Dim rsLastAccess_numRows

Set rsLastAccess = Server.CreateObject("ADODB.Recordset")
rsLastAccess.ActiveConnection = MM_gm_location_STRING
rsLastAccess.Source = "SELECT LastAccess FROM AccessData WHERE cintasRepLocCode='" & (Request.Form("cintasRepLocCode")) & "'"
rsLastAccess.CursorType = 0
rsLastAccess.CursorLocation = 2
rsLastAccess.LockType = 1
rsLastAccess.Open()

rsLastAccess_numRows = 0

rsLastAccess.Close()
Set rsLastAccess = Nothing

If month(rsLastAccess)= month(now()) then
Set AccessData = Server.CreateObject("ADODB.Command")
AccessData.ActiveConnection = MM_gm_location_STRING
update_query="UPDATE AccessData SET LastAccess=#" & Date() & "#, Accesses = Accesses + 1 WHERE cintasRepLocCode='" & (Request.Form("cintasRepLocCode")) & "'"
AccessData.CommandText = update_query
AccessData.Execute
AccessData.ActiveConnection.Close
Set AccessData = Nothing
response.Redirect("http://home2.cintas.com/xpedio/groups/public/@gm/@misc/documents/general/MyCintasGMDefault.asp")

Else

If month(rsLastAccess)< month(now()) then
' *** Edit Operations: declare variables

Dim MM_editAction
Dim MM_abortEdit
Dim MM_editQuery
Dim MM_editCmd

Dim MM_editConnection
Dim MM_editTable
Dim MM_editRedirectUrl
Dim MM_editColumn
Dim MM_recordId

Dim MM_fieldsStr
Dim MM_columnsStr
Dim MM_fields
Dim MM_columns
Dim MM_typeArray
Dim MM_formVal
Dim MM_delim
Dim MM_altVal
Dim MM_emptyVal
Dim MM_i

MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
MM_abortEdit = false

' query string to execute
MM_editQuery = ""
%>
<%
' *** Insert Record: set variables

If (CStr(Request("MM_insert")) = "form1") Then

MM_editConnection = MM_gm_location_STRING
MM_editTable = "AccessData"
MM_editRedirectUrl = "http://home2.cintas.com/xpedio/groups/public/@gm/@misc/documents/general/MyCintasGMDefault.asp"
MM_fieldsStr = "cintasRepLocCode¦value¦FirstName¦value¦LastName¦value¦Accesses¦value¦LastAccess¦value"
MM_columnsStr = "cintasRepLocCode¦',none,''¦FirstName¦',none,''¦LastName¦',none,''¦Accesses¦Accesses + 1¦LastAccess¦'#" & Date() & "#"

' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, "¦")
MM_columns = Split(MM_columnsStr, "¦")

' set the form values
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(MM_i+1) = CStr(Request.Form(MM_fields(MM_i)))
Next

' append the query string to the redirect URL
If (MM_editRedirectUrl <> "" And Request.QueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And Request.QueryString <> "") Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
End If
End If

End If
' *** Insert Record: construct a sql insert statement and execute it

Dim MM_tableValues
Dim MM_dbValues

If (CStr(Request("MM_insert")) <> "") Then

' create the sql insert statement
MM_tableValues = ""
MM_dbValues = ""
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_formVal = MM_fields(MM_i+1)
MM_typeArray = Split(MM_columns(MM_i+1),",")
MM_delim = MM_typeArray(0)
If (MM_delim = "none") Then MM_delim = ""
MM_altVal = MM_typeArray(1)
If (MM_altVal = "none") Then MM_altVal = ""
MM_emptyVal = MM_typeArray(2)
If (MM_emptyVal = "none") Then MM_emptyVal = ""
If (MM_formVal = "") Then
MM_formVal = MM_emptyVal
Else
If (MM_altVal <> "") Then
MM_formVal = MM_altVal
ElseIf (MM_delim = "'") Then ' escape quotes
MM_formVal = "'" & Replace(MM_formVal,"'","''") & "'"
Else
MM_formVal = MM_delim + MM_formVal + MM_delim
End If
End If
If (MM_i <> LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End If
MM_tableValues = MM_tableValues & MM_columns(MM_i)
MM_dbValues = MM_dbValues & MM_formVal
Next
MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & ") values (" & MM_dbValues & ")"

If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close

If (MM_editRedirectUrl <> "") Then
Response.Redirect(MM_editRedirectUrl)

End If
End If

End If
End If
End If
End If
%>

Cin_Coder

6:34 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



The code that I just posted is producing the Object variable not set error.

In regard to the other question you posed here's the thought of what I need to get to:

The first time a user accesses the site, I need to write their info to the DB.

The second time a user comes to the site if it's in the same month as the last access date, then I just up the number of accesses by 1 and set the last access date to today's date.

If the second time a user comes to the site is not in the same month as the entry already in the DB, then I need to insert a new record for the current month.

Basically this is a hit counter by month for a particular user.

CaseyRyan

6:42 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Alrighty, the problem is that you're never pulling the value of the lastAccess column out of the recordset. In your code on line 31, you do month(rsLastAccess). There's 2 problems with that, one is that you're not specifying the column, the other is that the recordset is closed.

What I've done below is added code to pull the value out into a variable (theLastAccess). Then you can use the variable throughout the rest of your code. I've also added some code which will confirm that there is a value for theLastAccess. If not, it assigns a default.

You may want to check the year and the month. Just checking the month may lead you into a problem of incorrect data. ie: If I haven't logged in since December of last year, and I logged in now; the code would think I had already logged in this month because it only checks that.


Dim rsLastAccess
Dim rsLastAccess_numRows
Dim theLastAccess


Set rsLastAccess = Server.CreateObject("ADODB.Recordset")
rsLastAccess.ActiveConnection = MM_gm_location_STRING
rsLastAccess.Source = "SELECT LastAccess FROM AccessData WHERE cintasRepLocCode='" & (Request.Form("cintasRepLocCode")) & "'"
rsLastAccess.CursorType = 0
rsLastAccess.CursorLocation = 2
rsLastAccess.LockType = 1
rsLastAccess.Open()

If Not (rsLastAccess.EOF And rsLastAccess.BOF) Then
theLastAccess = rsLastAccess("LastAccess")
Else
theLastAccess = "1/1/1800"
End If

rsLastAccess_numRows = 0
rsLastAccess.Close()
Set rsLastAccess = Nothing



If month(theLastAccess)= month(now()) then

Set AccessData = Server.CreateObject("ADODB.Command")
AccessData.ActiveConnection = MM_gm_location_STRING
update_query="UPDATE AccessData SET LastAccess=#" & Date() & "#, Accesses = Accesses + 1 WHERE cintasRepLocCode='" & (Request.Form("cintasRepLocCode")) & "'"
AccessData.CommandText = update_query
AccessData.Execute
AccessData.ActiveConnection.Close
Set AccessData = Nothing
response.Redirect("http://home2.cintas.com/xpedio/groups/public/@gm/@misc/documents/general/MyCintasGMDefault.asp")


Else
If month(theLastAccess)< month(now()) then
' *** Edit Operations: declare variables

-=casey=-

Cin_Coder

7:06 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Ok - it's definately getting further now. Yeah! That's a good thing, but now what it does is throw a blank screen and it does not write to the DB.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!--#include file="../Connections/gm_location.asp" -->
<%
If Request.Form("vcodepwd") = "A5{5d87" then
Session("access") = "true"
Session("FirstName") = Request.Form("FirstName")
Session("LastName") = Request.Form("LastName")
Session("cintasRepLocCode") = Request.Form("cintasRepLocCode")
Dim AccessData
Dim MM_insert
Dim update_query
Dim insert_query
Dim todaysDate
todaysDate=date()
Dim rsLastAccess
Dim rsLastAccess_numRows
Dim theLastAccess

Set rsLastAccess = Server.CreateObject("ADODB.Recordset")
rsLastAccess.ActiveConnection = MM_gm_location_STRING
rsLastAccess.Source = "SELECT LastAccess FROM AccessData WHERE cintasRepLocCode='" & (Request.Form("cintasRepLocCode")) & "'"
rsLastAccess.CursorType = 0
rsLastAccess.CursorLocation = 2
rsLastAccess.LockType = 1
rsLastAccess.Open()
theLastAccess = rsLastAccess("LastAccess")
rsLastAccess_numRows = 0
rsLastAccess.Close()
Set rsLastAccess = Nothing

If Trim(theLastAccess&"") = "" Then theLastAccess = "#" & Date() & "#"
If month(theLastAccess)= month(now()) then

Set AccessData = Server.CreateObject("ADODB.Command")
AccessData.ActiveConnection = MM_gm_location_STRING
update_query="UPDATE AccessData SET LastAccess=#" & Date() & "#, Accesses = Accesses + 1 WHERE cintasRepLocCode='" & (Request.Form("cintasRepLocCode")) & "'"
AccessData.CommandText = update_query
AccessData.Execute
AccessData.ActiveConnection.Close
Set AccessData = Nothing
response.Redirect("http://home2.cintas.com/xpedio/groups/public/@gm/@misc/documents/general/MyCintasGMDefault.asp")

Else

If month(theLastAccess)< month(now()) then

' *** Edit Operations: declare variables

Dim MM_editAction
Dim MM_abortEdit
Dim MM_editQuery
Dim MM_editCmd

Dim MM_editConnection
Dim MM_editTable
Dim MM_editRedirectUrl
Dim MM_editColumn
Dim MM_recordId

Dim MM_fieldsStr
Dim MM_columnsStr
Dim MM_fields
Dim MM_columns
Dim MM_typeArray
Dim MM_formVal
Dim MM_delim
Dim MM_altVal
Dim MM_emptyVal
Dim MM_i

MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (Request.QueryString <> "") Then
MM_editAction = MM_editAction & "?" & Server.HTMLEncode(Request.QueryString)
End If

' boolean to abort record edit
MM_abortEdit = false

' query string to execute
MM_editQuery = ""
%>
<%
' *** Insert Record: set variables

If (CStr(Request("MM_insert")) = "form1") Then

MM_editConnection = MM_gm_location_STRING
MM_editTable = "AccessData"
MM_editRedirectUrl = "http://home2.cintas.com/xpedio/groups/public/@gm/@misc/documents/general/MyCintasGMDefault.asp"
MM_fieldsStr = "cintasRepLocCode¦value¦FirstName¦value¦LastName¦value¦Accesses¦value¦LastAccess¦value"
MM_columnsStr = "cintasRepLocCode¦',none,''¦FirstName¦',none,''¦LastName¦',none,''¦Accesses¦Accesses + 1¦LastAccess¦'#" & Date() & "#"

' create the MM_fields and MM_columns arrays
MM_fields = Split(MM_fieldsStr, "¦")
MM_columns = Split(MM_columnsStr, "¦")

' set the form values
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_fields(MM_i+1) = CStr(Request.Form(MM_fields(MM_i)))
Next

' append the query string to the redirect URL
If (MM_editRedirectUrl <> "" And Request.QueryString <> "") Then
If (InStr(1, MM_editRedirectUrl, "?", vbTextCompare) = 0 And Request.QueryString <> "") Then
MM_editRedirectUrl = MM_editRedirectUrl & "?" & Request.QueryString
Else
MM_editRedirectUrl = MM_editRedirectUrl & "&" & Request.QueryString
End If
End If

End If
' *** Insert Record: construct a sql insert statement and execute it

Dim MM_tableValues
Dim MM_dbValues

If (CStr(Request("MM_insert")) <> "") Then

' create the sql insert statement
MM_tableValues = ""
MM_dbValues = ""
For MM_i = LBound(MM_fields) To UBound(MM_fields) Step 2
MM_formVal = MM_fields(MM_i+1)
MM_typeArray = Split(MM_columns(MM_i+1),",")
MM_delim = MM_typeArray(0)
If (MM_delim = "none") Then MM_delim = ""
MM_altVal = MM_typeArray(1)
If (MM_altVal = "none") Then MM_altVal = ""
MM_emptyVal = MM_typeArray(2)
If (MM_emptyVal = "none") Then MM_emptyVal = ""
If (MM_formVal = "") Then
MM_formVal = MM_emptyVal
Else
If (MM_altVal <> "") Then
MM_formVal = MM_altVal
ElseIf (MM_delim = "'") Then ' escape quotes
MM_formVal = "'" & Replace(MM_formVal,"'","''") & "'"
Else
MM_formVal = MM_delim + MM_formVal + MM_delim
End If
End If
If (MM_i <> LBound(MM_fields)) Then
MM_tableValues = MM_tableValues & ","
MM_dbValues = MM_dbValues & ","
End If
MM_tableValues = MM_tableValues & MM_columns(MM_i)
MM_dbValues = MM_dbValues & MM_formVal
Next
MM_editQuery = "insert into " & MM_editTable & " (" & MM_tableValues & ") values (" & MM_dbValues & ")"

If (Not MM_abortEdit) Then
' execute the insert
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close

If (MM_editRedirectUrl <> "") Then
Response.Redirect(MM_editRedirectUrl)

End If
End If

End If
End If
End If
End If
%>

CaseyRyan

7:13 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



You'll want to change this line:
If Trim(theLastAccess&"") = "" Then theLastAccess = "#" & Date() & "#"

The reason I chose 1/1/1800 is because it is a date that would never come up. By you making theLastAccess = Date() you're always going to hit the update on the first time the user hits the site because of the check.

In short change the line I cited above back to:

If Trim(theLastAccess&"") = "" Then theLastAccess = "1/1/1800"

Also, I edited my post above. Changed things around a little bit. You may want to work off of that code instead.

-=casey=-

Cin_Coder

7:20 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Ok - I made the suggested changes and it still throws a blank page which leads me to believe that my insert code is bad....

CaseyRyan

7:23 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



I would use response.write(<variable name>) to output the variable values while testing. COmment them out or remove them after you get it all working. Perfect example would be to piece together your sql statement (insert or update) and response.write it to the screen so you can see what it's executing.

-=casey=-