Deleting a record from table in MS ACCESS 2010 - sql

I am trying to delete records from the subreport that created with multiple tables data.
Below is the code I write for this
CurrentDb.Execute "DELETE FROM StateBudget " & " WHERE S_ID=" & ("SLELECT ID FROM States " & " WHERE State=" & Me.subformStateBudget.Form.Recordset.Fields("State"))
And error appear like this
syntax error (missing operator) in query expression 'S_ID=SELECT ID
FROM States WHERE State=????'.

Check this out
CurrentDb.Execute "DELETE FROM StateBudget WHERE S_ID IN (SELECT ID FROM States WHERE State=" & Me.subformStateBudget.Form.Recordset.Fields("State") & ")"
And by the way You have got misspelling in select, You add that string wrongly. That Bracket before Select should be inside the string and many more.

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.

Syntax Error (Missing Operator) in Update SQL statement for VBA Access

I have a form that references a linked table (called "Customer Information") to auto-fill information (such as street address, phone number, etc.) about a customer once the user has entered the customer's name into the form (called "Quote Form"). Once the information has been auto-filled, the user still has the opportunity to change those fields. However, if they change those fields such that they no longer match the information on record for that particular customer, a message box asks them if they'd like to update the customer's information. If they click yes, I want a SQL statement to update the information in the "Customer Information" table to match the information on the "Quote Form".
Unfortunately, I am receiving the following error: "Run-time error '3075': Syntax error (missing operator) in query expression 'insert result of "Form.Controls(Box)" -- see code below'.
I am using a public function because I plan to repeat this procedure for several different fields. Here is my code:
Private Sub StreetAddressEntry_LostFocus()
'Check for match with existing customer information and update if necessary
Call UpdateCustomerInfo("StreetAddressEntry", "[Street Address (Billing)]", "[Customer Information]", "[Customer Name]", "CustomerEntry", "street address")
End Sub
Public Sub UpdateCustomerInfo(Box As String, Fld As String, Tbl As String, lookupFld As String, lookupBox As String, topic As String)
'Underlying function for updating customer information
Dim MsgBoxAnswer As Variant
If Form.Controls(Box).Value <> DLookup(Fld, Tbl, lookupFld & " = " & lookupBox & ".Value") Then
'Request Permission to update customer information based on conflicting entries
MsgBoxAnswer = MsgBox("The " & topic & " does not match what is currently listed for this customer. Would you like to update the customer's information to match?", vbYesNo, "Edit Customer Information?")
If MsgBoxAnswer = vbNo Then 'No permission granted to add a new customer to the list.
Exit Sub
Else 'Permission granted to add new customer to the list.
DoCmd.RunSQL "UPDATE " & Tbl & " SET " & Fld & " = " & Form.Controls(Box) & " WHERE " & lookupFld & " = " & Form.Controls(lookupBox)
End If
End If
End Sub
I would appreciate any suggestions or tips on how to improve this code and resolve the error message.
To debug: you can your SQL text into a variable before running and then print using MsgBox to see what is the SQL text generated. That usually helps catching the problem. If still not clear, you can run that SQL inside a Query in Access and see if it works there.
My guess is that you're missing single quotes if the columns you're trying to update are string columns. Try this
mySQL = "UPDATE " & Tbl & " SET " & Fld & " = '" & Form.Controls(Box) & "' WHERE " & lookupFld & " = '" & Form.Controls(lookupBox) & "'"
MsgBox (mySQL)
DoCmd.RunSQL
Also if table name or column names includes non-alphanumeric characters (like space etc.), you need to use brackets around the table/column names but I think this is not the case with yours as I see square bracketed columns in your code.
Again, the best is to print the SQL text generated and run it inside your database client (in this case Access Query tool)

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)

Excel ODBC SQL Statement Throwing Error '[ODBC EXCEL Driver] Data Type Mismatch in criteria

The SQL statement below inserts values into a new table, in a new sheet (TempPoints). If I remove the last part of the statement (in bold) the query runs OK however when I add it back in to the statement I receive the following error message; '[ODBC EXCEL Driver] Data Type Mismatch in criteria'.
I have replicated the tables and query in SQLServer and it runs OK and as expected which proves the full statement is OK. Is this a limitation of the Excel ODBC driver??
Thank you in advance.
strSQL = "INSERT INTO [TempPoints$] (REFP, PointItemRef, LoadItemRef, PointDES, Iotype, Subtype, Notes) " & _
"SELECT " & tempref & ", [PointsDB$].ItemRef, [LoadstoPointsDB$].LoadRef, [PointsDB$].PointDES, [PointsDB$].Iotype, [PointsDB$].Subtype, [PointsDB$].Notes " & _
"FROM [LoadsToPointsDB$] INNER JOIN [PointsDB$] ON [LoadsToPointsDB$].PointRef = [PointsDB$].ItemRef " & _
"WHERE [LoadsToPointsDB$].LoadRef = " & moditemref & " AND [PointsDB$].SystemComp = 'Y' " & _
**"AND NOT EXISTS (SELECT * FROM [TempPoints$] WHERE [TempPoints$].PointItemRef = [PointsDB$].ItemRef)"**
Thank you for those that viewed this, but I seem to have found the answer.
There is a previous insert into statement that loads data in the table, once the data was being loaded into it, the table was storing the numbers as text and not integers. After removing the rows and ensuring the format of the columns was set to integer the SQL query began to function as expected.

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 #