This topic is locked

Avoid Duplicate entry by a user for same date selected

3/23/2009 11:47:58 AM
ASPRunnerPro General questions
S
sunnynayak author

Hi,

I have a table Worklog which i use for daily time entry for my team.

I want to write an event which will check if a record for the date selected already exists in the database.

The field that should not have a duplicate entry is Date.
Regards,

Sunil

J
Jane 3/24/2009

Hi,
use Before record added event on the Eventstab for this purpose.

Here is a sample:

str = "select * from TableName where FieldName='" & dict("FieldName") & "'"

Set rstmp = server.CreateObject("ADODB.Recordset")

rstmp.open str,dbConnection

if not rstmp.eof then

response.write "a record for the date selected already exists in the database"

BeforeAdd = False

rstmp.close

set rstmp=nothing
BeforeAdd = True

S
sunnynayak author 3/24/2009

Hi,

use Before record added event on the Eventstab for this purpose.

Here is a sample:


jane thanks for your reply... I need some clarification.

There are 10 people in my team, so there need to be 10 entries of time which will have same date...

will the above event work or will we have to look for Username and Date fields..

If yes.. what will be the code..
Regards,

Sunil Nayak

J
Jane 3/25/2009

Hi,
please see my changes below:

str = "select * from TableName where FieldName1='" & dict("FieldName1") & "' and FieldName2='" & dict("FieldName2") & "'"

S
sunnynayak author 3/26/2009

I entered the code

<%

' Before record added

Function BeforeAdd(dict,message,inline)

' Parameters:

' dict - Scripting.Dictionary object.

' Each field on the Add form is represented as a 'Field
name'-'Field value' pair
'** Custom code ****

' put your custom code here

str = "select * from Worklog where Name='" & dict("Name") & "' and Date='" & dict("Date") & "'"

Set rstmp =
server.CreateObject("ADODB.Recordset")

rstmp.open str,dbConnection

if not rstmp.eof then

response.write "A
record for the date selected already exists in the database"

BeforeAdd = False

rstmp.close

set rstmp=nothing
BeforeAdd = False
' set BeforeAdd to True if you like to proceed with adding new record

' set it to False
otherwise
End Function ' BeforeAdd

%>


I get an error

Microsoft VBScript compilation error '800a03f4'

Expected 'If'
/training/portal/include/Worklog_events.asp, line 27
End Function ' BeforeAdd

----^

J
Jane 3/27/2009

Sorry for my fault.

Here is the correct code:

str = "select * from Worklog where Name='" & dict("Name") & "' and Date='" & dict("Date") & "'"

Set rstmp = server.CreateObject("ADODB.Recordset")

rstmp.open str,dbConnection

if not rstmp.eof then

response.write "A record for the date selected already exists in the database"

BeforeAdd = False

else

BeforeAdd = True

end if

rstmp.close

set rstmp=nothing