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

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.

Related

Finding/Marking Duplicate Values using 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.

Searching a table where the field doesnt exist - Access VBA

I am a data consultant who migrates data I am sent into our system. I have written code that compares the contents of my table against what has been put into oracle, as an extra test. The tables are a little convoluted due to how they relate to each other. But essentially here is my question:
When I look to match two field values and the field doesnt exist I get a parameter pop up box. I want to only run the code if the field exists.
I have tried many things, wrapping an if statement around it but I always get the parameter box, can anyone help there must be an easier way to do this!
If Not DoCmd.OpenQuery("SELECT TOP 1" & MatchValues!FieldName & " FROM " &
MatchValues!ORACLE_TABLE_NAME) Then
MsgBox "moomins"
' strSQL = "INSERT INTO 002_TableValueErrors(ORACLE_TABLE_NAME,TRANSFORM_TABLE_NAME,FIELD_NAME,ORACLE_FIELD_VALUE,TRANSFORM_FIELD_VALUE) "
' strSQL = strSQL & " VALUES (" & MatchValues!ORACLE_TABLE_NAME & "," & MatchValues!TRANSFORM_TABLE_NAME & "," & MatchValues!FieldName & ",'ORACLE: NOT FOUND','ORACLE: NOT FOUND')"
End If
If you deal with Oracle: Have you tried to check if the field exists by querying ALL_TAB_COLUMNS in Oracle?
"Select count(*) from ALL_TAB_COLUMNS where table_name = " & MatchValues!ORACLE_TABLE_NAME & " and COLUMN_NAME = " & MatchValues!FieldName
(Untestet cause currently I have no Oracle Instance available)

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.

MS Access SQL query within VBA code

I need a second set of eyes on this SQL query embedded in the VBA code. I'm making an MS Access app that returns a dataset based on the to & from date criteria set by the user in the specific date picker boxes. The sql query that you see actually has been tested statically within MS Access query design view. I tested it with actual dates where you see the Me.from_filter and Me.to_filter. It worked perfectly! If you chose something like from 1/1/2015 to 5/1/2015 it returns columns of all the months you need. Perfect. Now when I embed it in the VBA code and assign it to a control variable, I get the "Run-time error '2342': A RunSQL action requires an argument consisting of an SQL statement." Can someone please eyeball this and tell me what might be wrong?
Option Compare Database
Option Explicit
Dim strSQL As String
Private Sub Command0_Click()
If IsNull(Me.from_filter) Or IsNull(Me.to_filter) Then
MsgBox "You have not entered a start date or end date"
Else
strSQL = "TRANSFORM Sum(dbo_ASSET_HISTORY.MARKET_VALUE) AS SumOfMARKET_VALUE " _
& "SELECT [dbo_FIRM]![NAME] AS [FIRM NAME], dbo_FUND.CUSIP, dbo_FUND.FUND_NAME, dbo_FUND.PRODUCT_NAME " _
& "FROM (dbo_ASSET_HISTORY INNER JOIN dbo_FIRM ON dbo_ASSET_HISTORY.FIRM_ID = dbo_FIRM.FIRM_ID) INNER JOIN dbo_FUND ON dbo_ASSET_HISTORY.FUND = dbo_FUND.FUND " _
& "WHERE (((dbo_FIRM.Name) Like 'Voya F*') And ((dbo_ASSET_HISTORY.PROCESS_DATE) >= #" & Me.from_filter & "# And (dbo_ASSET_HISTORY.PROCESS_DATE) <= #" & Me.to_filter & "#)) " _
& "GROUP BY [dbo_FIRM]![NAME], dbo_FUND.CUSIP, dbo_FUND.FUND_NAME, dbo_FUND.PRODUCT_NAME " _
& "PIVOT [dbo_ASSET_HISTORY]![ASSET_YEAR] & '-' & [dbo_ASSET_HISTORY]![ASSET_MONTH];"
DoCmd.RunSQL (strSQL)
End If
End Sub
RunSQL is for running action queries like update, insert, select into, delete, etc as per Microsoft definition https://msdn.microsoft.com/en-us/library/office/ff194626.aspx , you should probably use OpenQuery or similar to run your query.

MS Access : How do delete one record with multiple criteria?

I am trying to delete one record in a table. I have two unbounds, one with a number and one with a date and then a command button to execute the code. So in my table I assign these values and they go in the table in their seperate column and contain the "Name, Number, Date". So say I want to delete this record, all I do is enter the number into one unbound and the date into the other unbound and then click the button and it should delete. The problem I am getting is "Error 13(Type mismatch) in procedure..." Here is my code below:
CurrentDb.Execute "DELETE FROM CrewTable WHERE KitNumber = " & Me.txtClearKitEntry And ActionDate = " & Me.txtClearDateEntry"
CurrentDb.Execute dbFailOnError
Me.Crew.Requery
Again, the KitNumber is a number and ActionDate is a date. I thought for dates you need to use '#' but I got errors with that. So I feel I just have some quotes and that kind of stuff in the wrong order. Any help would be appreciated. Thanks
Change this:
CurrentDb.Execute "DELETE FROM CrewTable WHERE KitNumber = " & Me.txtClearKitEntry And ActionDate = " & Me.txtClearDateEntry"
To this
CurrentDb.Execute "DELETE FROM CrewTable WHERE KitNumber = " & Me.txtClearKitEntry & " And ActionDate = #" & Me.txtClearDateEntry & "#;"
part of you string was not in quotes and also when using dates in sql queries in Access they have to be wrapped in #