473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Preventing Duplication

72
hi vb master good day! i just wanted to ask for some help here.. i'am trying to prevent duplcation of records that will be save in my table...

here my code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdVerify_Click()
  2. Dim rsShowRec As New ADODB.Recordset
  3. Dim cnn As New ADODB.Connection
  4.  
  5. Set cnn = New ADODB.Connection
  6. cnn.ConnectionString = _
  7.  "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  8.           "Data Source=" & App.Path & "\dbTimeScheduling2.mdb;"
  9. cnn.Open
  10. For x = 0 To 3
  11.  
  12. On Error Resume Next
  13. rsShowRec.Open "Select * from RECORDS , cnn, adOpenStatic, adLockOptimistic"
  14.  
  15. If rsShowRec.EOF <> True Then
  16. If Combo1(x).Text = rsShowRec.Fields("TIME START").Value And Combo4(x).Text = rsShowRec.Fields("TIME END").Value And Combo2(x).Text = rsShowRec.Fields("ROOM").Value And Combo3(x).Text = rsShowRec.Fields("DAYS").Value Then
  17.     rsShowRec.Close
  18.     cnn.Close
  19.     cmdSave.Visible = False
  20.  
  21. Else
  22.     cmdSave.Visible = True
  23. End If
  24. End If
  25. Next
  26.     MsgBox ("Records already existed"), vbCritical
  27.  
  28.  
  29. End Sub
  30.  
the scenario is this eventhough my table doesnt have any records on it, still my system goes popping a mesage that the records i want to verify or save is already existed, i know theres something with my code. I assme that i doesnt actually loking in my database like i want,, kidly take a look at my code and i hope somebody can figure out whats wrong with it!!! Thank you very much!
May 25 '07 #1
15 1667
Killer42
8,435 Expert 8TB
rsShowRec.Open "Select * from RECORDS , cnn, adOpenStatic, adLockOptimistic"
You've got the quotes in the wrong place. You're including all the parameters (cnn, etc) in the SQL string.
May 25 '07 #2
darrel
72
Thank you for entertaining mga thread, but i dont how to arrange it! can you give me ideas or hint on how my code will function as i want...
May 25 '07 #3
Killer42
8,435 Expert 8TB
Thank you for entertaining mga thread, but i dont how to arrange it! can you give me ideas or hint on how my code will function as i want...
I haven't looked at the code in much detail as yet, but the line that I mentioned probably should be corrected like this...
Expand|Select|Wrap|Line Numbers
  1. rsShowRec.Open "Select * from RECORDS" , cnn, adOpenStatic, adLockOptimistic
May 25 '07 #4
darrel
72
Thank very Mr. i will try to debug my program and keep you posted...thank you!
May 26 '07 #5
darrel
72
Mr.Killer ive debug my code but still doesnt change, still it doesnt look for records in my fields:
here's my code:

Expand|Select|Wrap|Line Numbers
  1. Dim rsShowRec As New ADODB.Recordset
  2. Dim cnn As New ADODB.Connection
  3.  
  4. Set cnn = New ADODB.Connection
  5. cnn.ConnectionString = _
  6.  "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  7.           "Data Source=" & App.Path & "\dbTimeScheduling2.mdb;"
  8. cnn.Open
  9.  
  10. For x = 0 To 15
  11. On Error Resume Next
  12. rsShowRec.Open "Select * from RECORDS", cnn, adOpenStatic, adLockOptimistic, adText
  13. Next
  14.  
  15.     If Combo1(0).Text = rsShowRec.Fields("TIME START") And Combo2(0).Text = rsShowRec.Fields("ROOM") And Combo3(0).Text = rsShowRec.Fields("DAYS") And Combo4(0).Text = rsShowRec.Fields("TIME END") Then
  16.         MsgBox ("Record already existed"), vbCritical
  17.         rsShowRec.Close
  18.         cnn.Close
  19.         Exit Sub
  20.  
  21.  
  22.     Else
  23.         MsgBox ("There's no conflict with the previous records, you may save it now!"), vbOKOnly
  24.         cmdSave.Enabled = True
  25.     End If
  26.  
i think there's something with this code of mine:
Expand|Select|Wrap|Line Numbers
  1.  
  2. If Combo1(0).Text = rsShowRec.Fields("TIME START") And Combo2(0).Text = rsShowRec.Fields("ROOM") And Combo3(0).Text = rsShowRec.Fields("DAYS") And Combo4(0).Text = rsShowRec.Fields("TIME END") Then
  3.         MsgBox ("Record already existed"), vbCritical
  4.         rsShowRec.Close
  5.         cnn.Close
  6.         Exit Sub
  7.  
I think it doesnt do anything at all, what my code must do is to test all the records in my fields and see if there's going to be a duplication with the data that will be save later on... Mr.Killer i hope you can figure out what ive been doing wrong or missing, am getting frustrated,,, i cant go forward to the other tasks of my system without finishing this test of records.. I hope you can help me.. Thank you very much and God bless you all for helping us newbies in vb programming, hail to you all!!
May 26 '07 #6
Killer42
8,435 Expert 8TB
I don't understand the purpose of the For loop around the Open. You are trying to open rsShowRec 15 times? Why?

I think what you need to do is either open rsShowRec as now (but only once) then use MoveNext in a loop to go through the records and check them, or use a WHERE clause in your SQL to find any record matching your values.
May 26 '07 #7
darrel
72
thank you for that information: can u teach how to use WHERE clause in my code??? Thank you very much.

i have modified again my code, but still nothing happen... it always prompt me that the records i have inputted inputted though in my records table it doesnt match anything i know theres somthing wrong with my comparison method, and i'am not sure with the connection of my database, is it the right way to code it,,

here my code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdVerify_Click()
  2. Dim rsShowRec As New ADODB.Recordset
  3. Dim cnn As New ADODB.Connection
  4.  
  5. Set cnn = New ADODB.Connection
  6. cnn.ConnectionString = _
  7.  "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  8.           "Data Source=" & App.Path & "\dbTimeScheduling2.mdb;"
  9. cnn.Open
  10.  
  11. For y = 0 To 15
  12. If Combo1(y).Text = "Select" Or Combo2(y).Text = "Select" Or Combo3(y).Text = "Select" Or Combo4(y).Text = "Select" Then
  13.         On Error Resume Next
  14.         MsgBox ("Cannot Save blank entries"), vbCritical
  15.         On Error Resume Next
  16.         Exit Sub
  17. End If
  18. Next
  19.  
  20. 'AVOIDING DUPLICATION OF RECORDS
  21.         rsShowRec.Open "Select * from RECORDS", cnn, adOpenStatic, adLockOptimistic
  22.  
  23.     If rsShowRec.RecordCount > 0 Then
  24.         For x = 0 To 15
  25.             If rsShowRec.Fields("TIME START") = UCase(Combo1(x).Text) And rsShowRec.Fields("TIME END") = UCase(Combo4(x).Text) And rsShowRec.Fields("ROOM") = UCase(Combo2(x).Text) And rsShowRec.Fields("DAYS") = UCase(Combo3(x).Text) Then
  26.             rs.MoveNext
  27.             End If
  28.  
  29.         Next
  30.  
  31.         MsgBox ("Record already existed"), vbCritical
  32.         rsShowRec.Close
  33.         cnn.Close
  34.         End If
  35.  
  36.  
  37. End Sub
  38.  
thank you for patience mr. i hope we can slice whats wrong with this code... i just have to compare the data that i will be saving to the records that are been save in my RECORDS table... please...
May 26 '07 #8
Killer42
8,435 Expert 8TB
Ok, this is still going to require some work on your part. But the basic syntax to include a WHERE clause in your SQL string would be something like this...

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2. strSQL = "Select * from RECORDS" _
  3.        & " WHERE [TIME START] = '" & Ucase(Combo1(x).Text) & "'" _
  4.        & " AND [TIME END] = '" & UCase(Combo4(x).Text) & "'" _
  5.        & " AND ROOM = '" & UCase(Combo2(x).Text) & "'" _
  6.        & " AND DAYS = '" & UCase(Combo3(x).Text) & "'"
May 26 '07 #9
darrel
72
Thank you very much... i will try to work it out now... ill keep you posted with the development of my program.
May 28 '07 #10
darrel
72
Hello, i have a news.. it still doesnt work,, there no error with the conection of the database and to the code it self but it doesnt meet up my needs... now the problem is every time i will verify the schedule that has been made, the program always prompt me the that theirs no error on the schedule though it has errors... i think there something wrong my with code concerning the testing on the records in my table and the schedule that is being created by the user...

heres my code now

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub cmdVerify_Click()
  3. Dim rs As New ADODB.Recordset
  4. Dim cnn As New ADODB.Connection
  5. Dim x As Integer
  6. Dim strSQL As String
  7.  
  8. Set cnn = New ADODB.Connection
  9. cnn.ConnectionString = _
  10.  "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  11.           "Data Source=" & App.Path & "\dbTimeScheduling2.mdb;"
  12. cnn.Open
  13.  
  14. For x = 0 To 15
  15. On Error Resume Next
  16. strSQL = "Select * from RECORDS" _
  17. & "WHERE [TimeStart] = ' " & UCase(Combo1(x).Text) & " ' " _
  18. & " AND [TimeEnd] = ' " & UCase(Combo4(x).Text) & " ' " _
  19. & " AND [ROOM] = ' " & UCase(Combo2(x).Text) & " ' " _
  20. & " AND [DAYS] = ' " & UCase(Combo3(x).Text) & " ' "
  21.  
  22.  
  23. If Combo1(x).Text = "Select" Or Combo2(x).Text = "Select" Or Combo3(x).Text = "Select" Or Combo4(x).Text = "Select" Then
  24.         MsgBox ("Cannot Save blank entries"), vbCritical
  25.         rsShowRec.Close
  26.         cnn.Close
  27.         Exit Sub
  28. End If
  29. Next x
  30.  
  31.        If rs.EOF <> True Then
  32.        Else
  33.     For z = 0 To 15
  34.        If UCase(Combo1(z).Text) = rs.Fields("TimeStart") And UCase(Combo4(z).Text) = rs.Fields("TimeEnd") And UCase(Combo2(z).Text) = rs.Fields("ROOM") And UCase(Combo3(z).Text) = rs.Fields("DAYS") Then
  35.  
  36.         rs.Close
  37.         cnn.Close
  38.        End If
  39.     Next z
  40.         MsgBox ("Theres error on the schedule!"), vbCritical
  41.     End If
  42.  
  43.     For y = 0 To 15
  44.        If Not UCase(Combo1(y).Text) = rs.Fields("TimeStart") And UCase(Combo4(y).Text) = rs.Fields("TimeEnd") And UCase(Combo2(y).Text) = rs.Fields("ROOM") And UCase(Combo3(y).Text) = rs.Fields("DAYS") Then
  45.      End If
  46.     Next y
  47.         MsgBox ("Theres no error on the schedule!"), vbOKOnly
  48.         cmdSave.Enabled = True
  49. End Sub
  50.  
  51.  
I think theres something wrong in this area

Expand|Select|Wrap|Line Numbers
  1.  
  2. If rs.EOF <> True Then
  3.        Else
  4.     For z = 0 To 15
  5.        If UCase(Combo1(z).Text) = rs.Fields("TimeStart") And UCase(Combo4(z).Text) = rs.Fields("TimeEnd") And UCase(Combo2(z).Text) = rs.Fields("ROOM") And UCase(Combo3(z).Text) = rs.Fields("DAYS") Then
  6.  
  7.         rs.Close
  8.         cnn.Close
  9.        End If
  10.  
i think this code doesnt do anything at all... I hope you can tell me whats wrong with this,,, I pressume that it doesnt actually looking in my fields.Thank you so much for patience... Hoping to hear from you soon...
May 28 '07 #11
Killer42
8,435 Expert 8TB
I don't have time right now to go over this in detail. But I have a couple of observations...
  • You probably don't actually know whether there are any errors, because once you execute On Error Resume Next any error will simply cause you to continue with the next statement.
  • Your indentation is a bit messed up, which makes it much harder to follow the logic.
  • Why do you have an If test which does nothing...
    If rs.EOF <> True Then
    Else
  • I think your logic is needlessly complex. It probably should be following a fairly simple path something like...
    Expand|Select|Wrap|Line Numbers
    1. For x = 0 to 15
    2.   If anything not filled in Then
    3.     Cannot Save blank entries
    4.     Exit For
    5.   End If
    6.   Build SQL string to find any match for values(x)
    7.   Execute query
    8.   If anything found (check RecordCount, I suppose) Then
    9.     Theres error on the schedule!
    10.     Exit For
    11.   End If
    12. Next
May 28 '07 #12
darrel
72
thank you for the advise, one more question i hope its ok! after i build a SQL string, how can i execute it, am having a hard to understand what you have posted "Execute query"..?? thank you
May 28 '07 #13
Killer42
8,435 Expert 8TB
thank you for the advise, one more question i hope its ok! after i build a SQL string, how can i execute it, am having a hard to understand what you have posted "Execute query"..?? thank you
That's the Open that you were using before. It opens, or executes, the query.
May 28 '07 #14
darrel
72
Thank you for that information Mr.Killer, but sad to say still my program doesnt work as i want, Mr. can you give me some example on how to compare database records or on how to prevent records using sql statement in visual just like wat i have been doing,even a simple example or code, for able for me to understand the concept and to understand the codes and scenarion needed.. thank you very very much... i hope you can site me some example... am really getting frustrated ive been working it out for about a week now, but still no changes...


Hope you can help me with this... Thank you!
May 28 '07 #15
Dököll
2,364 Expert 2GB
Thank you for that information Mr.Killer, but sad to say still my program doesnt work as i want, Mr. can you give me some example on how to compare database records or on how to prevent records using sql statement in visual just like wat i have been doing,even a simple example or code, for able for me to understand the concept and to understand the codes and scenarion needed.. thank you very very much... i hope you can site me some example... am really getting frustrated ive been working it out for about a week now, but still no changes...


Hope you can help me with this... Thank you!
Response to this is here:

http://www.thescripts.com/forum/thread654517.html
Jun 1 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: simon.alexandre | last post by:
Hi all, I use csv module included in python 2.3. I use the writer and encouter the following problem: in my output file (.csv) there is a duplication of the end of line character, so when I open...
1
by: David Hane | last post by:
Hi all, I would like give users the ability to experiment with complex queries but I'm worried about them creating queries that will bog down the server. Does anyone have any ideas for...
20
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a...
3
by: Tcc | last post by:
Hi All, Assume there are some data in "a.txt": e.g. ABC
10
by: David Hirschfield | last post by:
Here's a strange concept that I don't really know how to implement, but I suspect can be implemented via descriptors or metaclasses somehow: I want a class that, when instantiated, only defines...
7
by: =?Utf-8?B?S2F2aXRh?= | last post by:
We have one web application developed in .Net framework 1.1. This site is live on production server since one year and used across world by many users. But only one of the users is facing...
1
by: dbarmer | last post by:
I have a unix database system that hold three tables, 254, 255, 256, and all are linked together by WO, CUST NO, LOC NO. This is for Unix Program Purposes Only. Duplication here is by design. So...
61
by: arnuld | last post by:
I have created a program which creates and renames files. I have described everything in comments. All I have is the cod-duplication. function like fopen, sprint and fwrite are being called again...
4
by: henry | last post by:
Folks: Using Dreamweaver CS3... Consider a home page, "index.php" which conditionally REQUIREs one of 'N' HTML files of pure content. All site styles are specified in a master CSS file,...
5
by: kate s | last post by:
Im creating an employee database for TAFE. I have an employee table with employee_id and place fields. The trigger is activated by the Before Insert event. When there's a duplication of...
0
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,...
0
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...
0
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...
0
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
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.