Within a single form I have used id and date as primary key, so while we are saving it onto a recordset, how can we check the duplicate entry? -
If rs.EOF Then
-
conn.Execute "insert into saldetail(id, date, othours, otamt, taxrate, netsal, province) values ('" & store & "','" & dtpay & "','" & txtoh & "','" & txtotamt & "','" & txttr & "','" & txtnsal & "','" & cbostate & "')"
-
MsgBox "INSERTED NEW RECORDS", vbOKOnly, "ADDING NEW RECORDS"
-
Else
-
'ID Found In DB (Duplicate)
-
MsgBox "Duplicate Entry"
-
End If
-
rs.Close
-
I have to insert into this code???
Please guide me through.
43 2616
Hi
If i were you, i would not use date as primary key. You can also use it if you want : ). For your problem, try to use Count.
Have a nice day!
Rey Sean
Check for existence of the record using COUNT.
Insert only if the record does not exist.
Hi
If i were you, i would not use date as primary key. You can also use it if you want : ). For your problem, try to use Count.
Have a nice day!
Rey Sean
that im using it not for me but,to calculate the overtime work of a particular employee for amonth
eg:
MII/101 has worked 12 hours on 31
10 hours on 30
8 hours on 29
As 8 is the normal time,
such that if u click MII/101 in alist view
the id +the date shoule be shown in another list view
which date you you want to display . all the date or only the dates when the employee overworked ?
Hi
As Debasisdas said, count checks the for the existence of the record. If the count is greater than 0, then it should prompt duplicate values. You can also use RecordCount. Good Luck : )
Rey Sean
which date you you want to display . all the date or only the dates when the employee overworked ?
all the dates corresponding to the particular id.....MII/101
all the dates corresponding to the particular id.....MII/101
you can simply do that by using SQL statment and by passing id in the where clause.
you can simply do that by using SQL statment and by passing id in the where clause.
But have to check the dates being inserted while we are saving it...
now it checks the id,but have to check the date also....
But have to check the dates being inserted while we are saving it...
now it checks the id,but have to check the date also....
you need to check both date and id using count before inserting.
Another possible approach is just to go ahead and do the insert, and use error-handling to deal with the situation where it already exists.
Hi,
What SQL you are using to check for duplication (RS?)
Use this : - dim sSQL As String
-
sSQL = "Select * From saldetail Where id='" & store & "' And date = #" & dtpay & "#"
-
Open RS with this select statement..
Dont use "Date" as field name. It is a keyword.
So what is the FieldType of Date?
By looking into your Insert statement, you must have declared as Text type?
Regards
Veena
Within a single form ...
You can modify your code as follows to overcome the problem: -
On error goto errHndlr
-
conn.Execute "insert into saldetail(id, date, othours, otamt, taxrate, netsal, province) values ('" & store & "','" & dtpay & "','" & txtoh & "','" & txtotamt & "','" & txttr & "','" & txtnsal & "','" & cbostate & "')"
-
MsgBox "INSERTED NEW RECORDS", vbOKOnly, "ADDING NEW RECORDS"
-
rs.Close
-
-
Exit Sub
-
errHndlr:
-
If Err.Number = -2147217873 Then
-
'ID Found In DB (Duplicate)
-
MsgBox "Duplicate Entry"
-
Resume Next
-
End If
-
End Sub
-
In Oracle, error number -2147217873 is returned when a unique constraint is violated. Here we have trapped this error number and simply resumed next if such an error occurs.
You can modify your code as follows to overcome the problem: -
On error goto errHndlr
-
conn.Execute "insert into saldetail(id, date, othours, otamt, taxrate, netsal, province) values ('" & store & "','" & dtpay & "','" & txtoh & "','" & txtotamt & "','" & txttr & "','" & txtnsal & "','" & cbostate & "')"
-
MsgBox "INSERTED NEW RECORDS", vbOKOnly, "ADDING NEW RECORDS"
-
rs.Close
-
-
Exit Sub
-
errHndlr:
-
If Err.Number = -2147217873 Then
-
'ID Found In DB (Duplicate)
-
MsgBox "Duplicate Entry"
-
Resume Next
-
End If
-
End Sub
-
In Oracle, error number -2147217873 is returned when a unique constraint is violated. Here we have trapped this error number and simply resumed next if such an error occurs.
that too isnt working out...........
that too isnt working out...........
have you tried using count() ?
have you tried using count() ?
yes,but have some errors while debugging........
have you tried using count() ?
But have to check the dates being inserted while we are saving it...
now it checks the id,but have to check the date also....
What's the problem with error trapping method?
I mean, what message are you getting when using this? I don't find any problem using this method. If you are not using Oracle, then you can have error number for the db (you are using) that is thrown when a unique constraint is violated.
This has always worked for me without any fault.
Tell me in detail so I can help you out.
What's the problem with error trapping method?
I mean, what message are you getting when using this? I don't find any problem using this method. If you are not using Oracle, then you can have error number for the db (you are using) that is thrown when a unique constraint is violated.
This has always worked for me without any fault.
Tell me in detail so I can help you out.
but my questn. is that once an id is saved with one particular date,how can we add another date with the same id???
I am confused...each time it needs to check id and date,but when id is there,n other date can be saved,rite???t.,
but my questn. is that once an id is saved with one particular date,how can we add another date with the same id???
I am confused...each time it needs to check id and date,but when id is there,n other date can be saved,rite???t.,
how can the same employee have the same date entered twice ?
check using - select count(*) from table_name where user_id='id' and user_date = 'date'
before inserting values
insert only if this returns 0
also takecare not to use DATE as field name in any table.
how can the same employee have the same date entered twice ?
check using - select count(*) from table_name where id='id' and date = 'date'
before inserting values
insert only if this returns 0
see to that,one can work for different hours for different dates right???
As in above replies:
MII/101 added to database as 02/01/08 with 10 hours
the same emp.id should be enetered with 8 hours in 03/01/08
Did u understand???Oer else explanation needed?
see to that,one can work for different hours for different dates right???
As in above replies:
MII/101 added to database as 02/01/08 with 10 hours
the same emp.id should be enetered with 8 hours in 03/01/08
Did u understand???Oer else explanation needed?
so what , your date is different or not
and you are having composit key on id and date , right ?
so what , your date is different or not
and you are having composit key on id and date , right ?
yes,the date is different
yes,the date is different
i hope you got that right now .
i hope you got that right now .
yes,but not getting it in prper way.......ny way thanks
yes,but not getting it in prper way.......ny way thanks
what is the problem now ?
If the primary key is made up of the ID and date fields, then any attempt to store a record which duplicates both of these fields must produce some sort of error (unless your database allows duplicate primary keys, which seems rather odd). Why can't you trap that error and deal with it accordingly?
If the primary key is made up of the ID and date fields, then any attempt to store a record which duplicates both of these fields must produce some sort of error (unless your database allows duplicate primary keys, which seems rather odd). Why can't you trap that error and deal with it accordingly?
killer,now im keeeping only date as a primary key...would it help??? sugeest if its not a good idea?
but the code count isnt working out,because im selecting date from a datepicker
and store date as 12/18/2007
anyone please guide me through?
Kindly post how you are trying to use count that it is not working.
killer,now im keeeping only date as a primary key...would it help??? sugeest if its not a good idea?
I don't know enough about database design to tell you whether it's a good or a bad idea. I was merely making the point that if your primary key was made up of both fields, then you couldn't create duplicate records even if you tried - the database software would not allow it.
Um... in this case, using the date alone as the PK is probably not a good idea. Since the PK must be unique, it means you can only store one record per date. And I don't think this is what you want.
Um... in this case, using the date alone as the PK is probably not a good idea. Since the PK must be unique, it means you can only store one record per date. And I don't think this is what you want.
To avoid this both id and date must me marked as composit primary key.
so that id can be duplicate ,date can be duplicate but the combination of both can't be.
To avoid this both id and date must me marked as composit primary key.
so that id can be duplicate ,date can be duplicate but the combination of both can't be.
im rite now using sql server where i kept both as a primary key....making errors?
To avoid this both id and date must me marked as composit primary key.
so that id can be duplicate ,date can be duplicate but the combination of both can't be.
I have a little knowledge in database, but can you explain to me what does the composite primary key mean? I think it has been discussed to us by our professor but I think I wasn't listening at that time.
Composite primary key means more than one field combination works as the primary key. In Oracle a maximum of 32 key combination can be used as composite key.
Composite primary key means more than one field combination works as the primary key. In Oracle a maximum of 32 key combination can be used as composite key.
how about sql server???
Keep in mind, it's not some special type of field called a "composite primary key". The word "composite" refers to any key (or "index") which is made up of more than one field.
If you want the database software to automatically prevent record duplication, you need to define a "unique key". I think the meaning of this one is fairly obvious - it only allows unique values in that key. So a record which would produce a duplicate key value cannot be stored.
Any key may be defined as unique, regardless of whether it is the primary one, and regardless of how many fields are used to build it.
But I'm pretty sure that by definition, the primary key is always unique.
Hi Lotus,
"Composite Primary Key", means in a Table, Primary Key is composed of more than one Column (all together )..
Say in the above Example ,
Date + ID is Composite PK, Like Same Date Can Have Many ID's and Same ID can have Many Dates, but when combined together, there is only one Unique Record:
Date ID
01-01-2008 001
01-01-2008 002
01-01-2008 003
01-01-2008 004
02-01-2008 001
03-01-2008 001
04-01-2008 001
05-01-2008 001
Regards
Veena
Hi Lotus,
"Composite Primary Key", means in a Table, Primary Key is composed of more than one Column (all together )..
Say in the above Example ,
Date + ID is Composite PK, Like Same Date Can Have Many ID's and Same ID can have Many Dates, but when combined together, there is only one Unique Record:
Date ID
01-01-2008 001
01-01-2008 002
01-01-2008 003
01-01-2008 004
02-01-2008 001
03-01-2008 001
04-01-2008 001
05-01-2008 001
Regards
Veena
any meansd for sql server?
any meansd for sql server?
Hi,
You can create Composite PK in SQL Server also.
Create table syntax is : -
CREATE TABLE MYTABLE (
-
TDATE DATE NOT NULL,
-
ID INTEGER NOT NULL,
-
ENAME VARCHAR(50),
-
PRIMARY KEY (TDATE, ID)
-
)
-
-
Regards
Veena
Hi Lotus,
"Composite Primary Key", means in a Table, Primary Key is composed of more than one Column (all together )..
Say in the above Example ,
Date + ID is Composite PK, Like Same Date Can Have Many ID's and Same ID can have Many Dates, but when combined together, there is only one Unique Record:
Date ID
01-01-2008 001
01-01-2008 002
01-01-2008 003
01-01-2008 004
02-01-2008 001
03-01-2008 001
04-01-2008 001
05-01-2008 001
Regards
Veena
OK. I got it. Very well explained Veena. To Debasisdas and Killer42, thank you also. From now on, I am going to pay attention to our class.
OK. I got it. Very well explained Veena. To Debasisdas and Killer42, thank you also. From now on, I am going to pay attention to our class.
code( vb)
If Val(Form2.txttamt.Text) = 0 Then
MsgBox "Sorry,Ur Not Allowed To Do So!!!", vbOKOnly, "ERROR"
Exit Sub
Else
strName = Trim(ListView1.SelectedItem)
strId = ListView1.SelectedItem.SubItems(1)
rs.Open "select * from empdetail Where name = '" & strName & "'", conn, adOpenStatic, adLockOptimistic
store = rs!id
rs.Close
Set rs = Nothing
rs1.Open "select * from saldetail where id = '" & store & "'", conn, adOpenStatic, adLockOptimistic
storDate = rs1!Date
storId = rs1!id
If rs1.EOF Then
If Form2.dtpay.Value = "'" & storDate & "'" And strId = "'" & store & "'" Then
MsgBox "duplicate entry"
Else
conn.Execute "insert into saldetail(id,date,othours,otamt,taxrate,netsal,pro vince)values ('" & store & "','" & dtpay & "','" & txtoh & "','" & txtotamt & "','" & txttr & "','" & txtnsal & "','" & cbostate & "')"
MsgBox "INSERTED NEW RECORDS", vbOKOnly, "ADDING NEW RECORDS"
rs1.Close
Set rs1 = Nothing
End If
End If
End If
code using composit key??? i am not gettng it........used with sql server and vb???
Hi,
First Create Composite PK (ID + Date), and change last part of your coding ...: -
rs1.Open "select * from saldetail where id = '" & store & "' and Date = '" & dtPay & "'", conn, adOpenStatic, adLockOptimistic
-
If Not rs1.EOF Then
-
MsgBox "duplicate entry"
-
Else
-
conn.Execute "insert into saldetail(id,date,othours,otamt,taxrate,netsal,pro vince)values ('" & store & "','" & dtpay & "','" & txtoh & "','" & txtotamt & "','" & txttr & "','" & txtnsal & "','" & cbostate & "')"
-
MsgBox "INSERTED NEW RECORDS", vbOKOnly, "ADDING NEW RECORDS"
-
rs1.Close
-
Set rs1 = Nothing
-
End IF
-
Regards
Veena
Hi,
First Create Composite PK (ID + Date), and change last part of your coding ...: -
rs1.Open "select * from saldetail where id = '" & store & "' and Date = '" & dtPay & "'", conn, adOpenStatic, adLockOptimistic
-
If Not rs1.EOF Then
-
MsgBox "duplicate entry"
-
Else
-
conn.Execute "insert into saldetail(id,date,othours,otamt,taxrate,netsal,pro vince)values ('" & store & "','" & dtpay & "','" & txtoh & "','" & txtotamt & "','" & txttr & "','" & txtnsal & "','" & cbostate & "')"
-
MsgBox "INSERTED NEW RECORDS", vbOKOnly, "ADDING NEW RECORDS"
-
rs1.Close
-
Set rs1 = Nothing
-
End IF
-
Regards
Veena
pk can be set as if in ms-access in sql server,so kept both as primary...but im not getting it,the way i meant to be...
hi
i think every possible way has been suggested by the experts. if still there is any problam then you can try following logic. - rs.Open "select id, date, id & format(date,'dd/mm/yyyy') as key from table"
-
-
rs.Movefirst
-
rs.Find "key = '" & txtid.text & format(txtdate.text,"dd/mm/yyyy") & "'"
-
If Not rs.Eof Then
-
MsgBox "duplicate entry"
-
Else
-
'insert new values
-
End If
regards
manpreet singh dhillon hoshiarpur
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Philip Yale |
last post by:
I'm probably going to get shot down with thousands of reasons for
this, but I've never really heard or read a convincing explanation, so
here goes ...
Clustered indexes are more efficient at...
|
by: Thomas R. Hummel |
last post by:
Hello all,
I am trying to write a query that compares a member's enrollment period
with the products that their group has had during that period (all
members belong to a group and the products...
|
by: Fred |
last post by:
Hi.
What is the usual and what are the possible fields to use for the primary
key of an intersecting table of a many-to-many relationship?
I would think the typical, most common fields would...
|
by: Emi Lu |
last post by:
Hello all,
I have a question about "date" & "timestamp" types in PostgreSQL. I want
to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for
them. However, it seems that PostgreSQL...
|
by: rAinDeEr |
last post by:
I have a Parts Table with the following structure..
I need to decide which makes the primary key of the table.
I cant have any sequences nor can I add any new fields in this Table.
Table...
|
by: Pedro Pinto |
last post by:
I'm new at access and i'm dealling with a simple problem on positioning the form record seeking a date primary field.
I have simple table with two data fields:
1- date field as short-date...
|
by: keri |
last post by:
Hi,
I am creating a table where I want to use the date as the primary key -
and to automatically create a record for each working date (eg Mon to
Fri) until 30 June 2007. Is this possible? I do...
|
by: scolivas |
last post by:
Hi all, I have a situation that is requiring me to make an Entry Date part of the Primary Key. Problem I am having is that it includes the time too.
What I have is an inventory return table.
It...
|
by: Dr Al |
last post by:
I have a table with four date fields, some of which may not be filled
in based on our data entry needs. I have a criteria set as <date()-180
which is supposed to pull dates older than 180 days ago....
|
by: kadavu |
last post by:
I have a database with just 2 tables
Table 1 "tblReadingDateTime"
ID Indexed Auto Number and primary key
Date Date/Time
Time Date/Time
Systolic Number (Single)
Diastolic Number...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |