Finding/Marking Duplicate Values using SQL - sql

I am trying to update a column in my table to specify if the record is a duplicate or not. To do this I added a field called 'DuplicateRecord'.
I can't use the query wizard in access as the duplicate option in this only allows for 10 fields to be checked as a duplicate and my table has more than 10 fields.
The below code works for me:
Call Module1.RunSQL("UPDATE myTable " & _
"SET myTable.DuplicateRecord = TRUE " & _
"WHERE myTable.[CompanyID] IN (" & _
"SELECT * FROM " & _
"(SELECT myTable.[CompanyID] " & _
"FROM myTable " & _
"GROUP BY myTable.[CompanyID] " & _
"HAVING COUNT(*) > 1 ) T1 ) ")
However this code just runs off one field and I need it to run off all fields in the table (there are about 15 fields).
As a test I tried using two fields to see if I could get this working using the following:
Call Module1.RunSQL("UPDATE myTable " & _
"SET myTable.DuplicateRecord = TRUE " & _
"WHERE myTable.[CompanyID] AND myTable.[Product] IN (" & _
"SELECT * FROM " & _
"(SELECT myTable.[CompanyID], myTable.[Product], COUNT(*) " & _
"FROM myTable " & _
"GROUP BY myTable.[CompanyID], myTable.[Product] " & _
"HAVING COUNT(*) > 1 ) T1 ) ")
However I get an error message saying "Run-time error '3306' You have written a subquery that can return more than one field without using the EXISTS reserved word in the main query's FROM clause. Revise the SELECT statement of the subquery to request only one field."
I've tried googling the error but I can't seem to solve it as I don't fully understand it.
Does anyone know how I can apply the logic of testing for duplicates? Is there an easier way to do this then how I am currently trying? I will need to do this for the full record (15 fields), so I am a bit conscious that the way I am currently attempting this might not be the best fit.

Related

Run time error 2107 after running a "DELETE .." SQL statement - VBA/MS Access

I am relatively new to Access VBA and I keep getting the run-time error 2107 on every even numbered run of the following code, and every odd numbered run runs very fine.
I have a summary form that is built to find unique course names and perform a mathematical count function according to filter criteria given by the user. The results are temporarily stored in a table Tbl_EnqSummary.
The code goes like this:
'Empty all previous records in the EnqSummary table:
DoCmd.RunSQL "DELETE * FROM Tbl_EnqSummary"
'find out unique entries in the main data table (Enquiries) and populate the Courseidentifier field in the EnqSummary table with these distinct names:
courses = "INSERT INTO Tbl_EnqSummary ( CourseIdentifier ) " _
& "SELECT DISTINCT Tbl_Enquiries.CourseEnquiryIdentifier FROM Tbl_Enquiries " _
& "WHERE ((Tbl_Enquiries.EnqDate >= #" & Format(Me.txtDateFrom.Value, "yyyy-mm-dd") & "#) " _
& "AND (Tbl_Enquiries.EnqDate <= #" & Format(Me.txtDateTo.Value, "yyyy-mm-dd") & "#)) " _
& "ORDER BY Tbl_Enquiries.CourseEnquiryIdentifier"
'Requery the form
DoCmd.RunSQL courses
summarytable = "SELECT * FROM Tbl_EnqSummary ORDER BY Tbl_EnqSummary.CourseIdentifier"
Me.RecordSource = summarytable
Me.AllowAdditions = False
Me.Requery
Here I get the run-time error 2107 stating there is some validation rule being violated. There is no validation rule anywhere - I have checked and checked.

SQL query works slower than the Access query wizard

I am working on automation of elimination of unmatched rows in two tables in MS Access. As known, Access has a query wizard for this process which named Find unmatched records. This method works fast even for 500k data rows.
But when I execute a query in MS Access VBA it works so slowly. Is there a faster SQL implementation to eliminate data, or does MS Access use a different method? How can I make it fast?
Below is my query in VBA. Table1 and Table2 each have more than 100k rows.
strQuery = SELECT gsmno INTO newtablename FROM table1 WHERE gsmno NOT IN (SELECT gsmno FROM table2)
CurrentDb.Execute strQuery
Use a LEFT OUTER JOIN and check for NULL on the Table2 gsmno. Those are your Non-Matches.
strQuery = & _
"SELECT " & _
" t1.gsmno " & _
"INTO " & _
" newtablename " & _
"FROM " & _
" table1 as t1 " & _
"LEFT OUTER JOIN " & _
" table2 as t2 on " & _
" t1.gsmno = t2.gsmno " & _
"WHERE " & _
" isnull(t2.gsmno) = true;"
CurrentDb.Execute strQuery

Updating Record in a table VBA Access

I am trying to update the record in an Access table using the below code and get an error that the data type is mismatch in criteria expression :
If Not (Me.frmdamagesub.Form.Recordset.EOF And Me.frmdamagesub.Form.Recordset.BOF) Then
With Me.frmdamagesub.Form.Recordset
Me.txtquantity = .Fields("Quantity")
Me.txtquantity.Tag = .Fields("Quantity")
Me.cmdedit.Caption = " Update"
CurrentDb.Execute " UPDATE damaged_card " & _
" SET Quantity='" & Me.txtquantity & "'" & _
" WHERE Quantity=" & Me.txtquantity.Tag
End With
End If
Since you are comparing to texts, try it like this:
CurrentDb.Execute " UPDATE damaged_card " & _
" SET Quantity='" & Me.txtquantity & "'" & _
" WHERE Quantity='" & Me.txtquantity.Tag & "'"
Notice the added '-s.
Debugging
If it still fails, check the values in runtime and try running the SQL manually and see if it works that way. To extract the statement in runtime, have VBA output it into the Immediate window (ctrl+g) like this:
debug.print " UPDATE damaged_card " & _
" SET Quantity='" & Me.txtquantity & "'" & _
" WHERE Quantity='" & Me.txtquantity.Tag & "'"
Do this before you try to execute it.
Then you can go to Access, create a new query, change the view to SQL view, and paste the produced SQL statement. Before executing it with Run, it is worth taking a look at the affected rows by clicking the View button and selecting Datasheet View.
This displays all the rows from the target table that will be affected by the change. If no rows are displayed, that means no row fits your criteria, and nothing will be changed.
To be honest, your query doesn't make much sense, as it says: "update the Quantity to 5 where the Quantity is 5". You might want to rethink you where clause.
Refine your query in Access, and once it works, paste it back to the VBA code.

SQL ORDER BY on Update- update last record with condition

I am trying to update the last record of table Log (the field with the latest TimeAccessed) given that TimeExited is null, and the computername is the same as the "cm" parameter. I have this but get error, "missing semicolon at end of sql statement"
what is wrong??
dbs.Execute "UPDATE Log " _
& "SET TimeExited = " & Format(CloseTime, "\#hh:mm:ss AMPM\#") _
& " WHERE TimeExited is NULL AND ComputerName = '" & cm & "'" _
& " ORDER BY TimeAccessed DESC" _
& " LIMIT 1; "
nothing wrong with first 2 lines, work perfectly fine, it's the last two that give problems
Access SQL doesn't use LIMIT n it uses TOP n, and as mentioned in the other question cited in the comments to your question, you aren't allowed to use TOP in the way you've described. Instead, you'll need to do something along these lines:
UPDATE Log
SET TimeExited = CloseTime
WHERE TimeExited IS NULL
AND ComputerName='r2d2'
AND TimeAccessed IN
(
SELECT TOP 1 TimeAccessed
FROM Log
WHERE TimeExited IS NULL
AND ComputerName='r2d2'
ORDER BY TimeAccessed DESC
)

Syntax error with select in select in vb to access SQL

I got a SQL statement where am selecting from an access database in vb but I get this error; "syntax error in query expression select sum(brought_qtty)" when I run my program. I imagine am doing the right thing but seems am not. How can I adjust this select? The code is below:
"select distinct(brought_price) as [Price], select sum(brought_qtty) as [Ordinary] from brought_coffee where " & _
"coffee_grade=O, select sum(brought_qtty) as [Premium] from brought_coffee where" & _
"coffee_grade=P, sum(brought_qtty) as [Total Qtty]" & _
", sum(brought_paid) as [paid], " & _
"sum(brought_bal) as [Balance]" & _
"from brought_coffee, farmer where brought_date=#" & dtc.Text.Trim & "# and farmer_centre='" & cc.Text.Trim & _
"' and farmer.farmer_num=brought_coffee.farmer_num"
The second and third select in the query produce a syntax error... Replace select sum(brought_qtty) with just sum(brought_qtty).