Ms Access SQL Update statement doesn't work - sql

I currently have a split default view
I'm trying to change the field "Email verzonden?" from "Niet verzonden!" to "Verzonden!" when the user presses a button, but only when the checkbox in front of it is checked.
I tried to use an update statement with SQL but I get the following error code:
Run-time error '3464': Data type mismatch in criteria expression.
Does anyone know how to fix this and how to get it to work?
This is the SQL that I'm using:
Private Sub cmdTest_Click()
Dim SQL As String
SQL = "Update artikelfiche SET Verzonden = 'Verzonden!' WHERE Bestellen = -1"
DoCmd.RunSQL SQL
End Sub
Thanks in advance!

I should look better before posting questions.
This is the solution though:
Private Sub cmdTest_Click()
Dim SQL As String
SQL = "Update artikelfiche SET Verzonden = 'Verzonden!' WHERE Bestellen = '-1'"
DoCmd.RunSQL SQL
End Sub
Basically, I forgot the '' at the criteria of Bestellen.

Related

Input variable in Where clause for SQL "Select Statement" - Access, VBA

Need help for the below listed issues.
I am getting "YearN" as user input to the select statement to Run a Query in VBA for Access Database. The statement works when a number directly entered in where clause say "2027". Not sure how to reference a input variable/object. Please help.
Need help to refresh the record as I am getting runtime error whenever the code trying to execute line "A.open strconnection" saying "The database has been placed in a state by user 'Admin' on machine that prevents it from being opened or locked". Please advise
Dim YearNumber As Long
DoCmd.RefreshRecord
YearN = InputBox("Enter the Record Year to delete:")
If YearN = "" Then
MsgBox "Year not entered. Query exit"
Else
Dim A As Object
Dim rs As Object
Dim strSql As String
Dim strConnection As String
Set A = CreateObject("ADODB.Connection")
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\OneDrive - fab\Access_Db\file.Accdb"
strSql = "DELETE FROM APAC_tbl_test WHERE Year= YearN;"
DoCmd.SetWarnings False
A.Open strConnection
Set rs = A.Execute(strSql)
DoCmd.SetWarnings True
Set rs = Nothing
A.Close
Set A = Nothing
End If
End Sub```
If you are trying to have a "pop-up" box where the user inputs a value for the WHERE clause within Access, you don't necessarily need VBA code. Simply write your SQL statement and in the WHERE clause you'll do something like below:
DELETE FROM APAC_tbl_test WHERE Year = [Enter Year:]
Once you run the above code it should prompt user for the year with "pop-up" box that is labelled "Enter Year".
So basically,
WHERE [column name] = [custom prompt message:]
Important to keep the brackets [ ] and the semicolon :.
More information can be found at this link - https://support.microsoft.com/en-us/office/use-parameters-to-ask-for-input-when-running-a-query-c2806d3d-d500-45a8-8507-ec6af351b6ed

MS-Access VBA DoCmd.RunSQL issue

I'm trying to run some SQL from a button however I keep getting runtime error 2342 "A runSQL action requires an argument consisting of a SQL statement." I know the statement works when I run it direct.
As far as I can see I have set it up exactly as per the example in the dev centre documents but it's still failing - code details below, any advice on what I am doing stoopid gratefully received! Thanks
Example in document:
Public Sub DoSQL()
Dim SQL As String
SQL = "UPDATE Employees" & _
"SET Employees.Title = 'Regional Sales Manager'" & _
"WHERE Employees.Title = 'Sales Manager'"
DoCmd.RunSQL SQL
End Sub
My code:
Private Sub btnTestNextSeqNum_Click()
Dim GetSeqNum As String
GetSeqNum = "SELECT dt1.MaxAccSeqNum FROM (SELECT Item.AccessionYear AS AccYear, Max(Item.AccessionSequenceNumber) AS MaxAccSeqNum FROM Item GROUP BY Item.AccessionYear) AS DT1 WHERE dt1.AccYear = 2020;"
msgbox GetSeqNum *this shows that the variable does contain the full SQL statement
DoCmd.RunSQL GetSeqNum
End Sub

How do you subtract a value from the existing value in a field in MS Access?

I thought I had a simple update statement:
Update LotDestination Set Quantity = Quantity - 2 where Lot = '9002ex'
Quantity is a field type of Number.
No matter what value is in Quantity, it always is 0 after running this query.
Is this an MS Access syntax issue, as I thought this was a pretty standard way to do things in other db platforms?
I created the following code and it works perfectly:
Public Sub UpdateQuantity()
Dim SQL As String
SQL = "UPDATE LotDestination SET LotDestination.Quantity = LotDestination.Quantity-2 WHERE LotDestination.Lot='9002ex'"
DoCmd.RunSQL SQL
End Sub

How do take the value from a combobox and use it to run an SQL query

I am trying to take the value from a combo box (in this case 'cboFullName' located on form 'frmMasterNotebook') and cross reference it to table 'tblSearchEngine01' so that an update gets made to column 'query05contactselect' for all records where in column 'contact' the value matches to that selected by the combobox ('cboFullName'). Below is my code but I am getting a syntax error message.
Private Sub cboFullName_AfterUpdate()
st_sql = "UPDATE tblSearchEngine01, SET tblSearchEngine01.Query05ContactSelect = '1' WHERE (((tblSearchEngine01.[contact])=([forms]![frmmasternotebook]![cbofullname]))))"
Application.DoCmd.RunSQL (st_sql)
Your code builds an UPDATE statement which includes a comma after the table name ...
UPDATE tblSearchEngine01, SET
^
Remove that comma and see whether Access complains about anything else.
However I suggest you start by creating and testing a new query in Access' query designer. Paste this statement into SQL View of your new query ...
UPDATE tblSearchEngine01
SET Query05ContactSelect = '1'
WHERE [contact] = [forms]![frmmasternotebook]![cbofullname];
After you revise and test the statement so that Access executes it without complaint, then you can revise your VBA code to produce the exact same statement text which works in the query designer.
Using DAO this is another way to resolve my issue:
Private Sub cboFullName_AfterUpdate()
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblSearchEngine06", dbOpenTable)
rst.AddNew
rst!Contact = Me.cboFullName.Text
rst!ContactID = Me.cboFullName
rst.Update
rst.Close
Set rst = Nothing

error 3075 when running SQL in access 2007 vba

I wonder why I can't execute this SQL query in access 2007 through VBA, which can be executed when i create a query myself:
Private Sub SQL_Click()
Dim curDatabase As Database
Dim RS_REPORT As Recordset
Set curDatabase = CurrentDb
Set RS_REPORT = _
curDatabase.OpenRecordset("REPORT_CONTENT_ARCHIVE", dbOpenDynaset)
strStatement = "SELECT REPORT_CATEGORY1.DESCRIPTION, REPORT_CATEGORY2.DESCRIPTION, REPORT_CATEGORY3.DESCRIPTION, REPORT_RECOMMENDATION.RECOMMENDATION, REPORT_CONTENT_ARCHIVE.REPORT_UID, REPORT_CONTENT_ARCHIVE.CATEGORY1_ID, REPORT_CONTENT_ARCHIVE.CATEGORY2_ID, REPORT_CONTENT_ARCHIVE.CATEGORY3_ID" & _
"FROM (REPORT_CATEGORY1 RIGHT JOIN REPORT_CATEGORY2 ON REPORT_CATEGORY1.CATEGORY1_ID=REPORT_CATEGORY2.CATEGORY1_ID) RIGHT JOIN (REPORT_CATEGORY3 RIGHT JOIN (REPORT_CONTENT_ARCHIVE INNER JOIN REPORT_RECOMMENDATION ON (REPORT_CONTENT_ARCHIVE.CATEGORY3_ID=REPORT_RECOMMENDATION.CATEGORY3_ID) AND (REPORT_CONTENT_ARCHIVE.CATEGORY2_ID=REPORT_RECOMMENDATION.CATEGORY2_ID) AND (REPORT_CONTENT_ARCHIVE.CATEGORY1_ID=REPORT_RECOMMENDATION.CATEGORY1_ID)) ON (REPORT_CATEGORY3.CATEGORY2_ID=REPORT_RECOMMENDATION.CATEGORY2_ID) AND (REPORT_CATEGORY3.CATEGORY1_ID=REPORT_RECOMMENDATION.CATEGORY1_ID) AND (REPORT_CATEGORY3.CATEGORY3_ID=REPORT_RECOMMENDATION.CATEGORY3_ID)) ON (REPORT_CATEGORY2.CATEGORY2_ID=REPORT_CATEGORY3.CATEGORY2_ID) AND (REPORT_CATEGORY2.CATEGORY1_ID=REPORT_CATEGORY3.CATEGORY1_ID)" & _
"WHERE (((REPORT_CONTENT_ARCHIVE.REPORT_UID)=12));"
Set qryMRSA = curDatabase.CreateQueryDef("DATABASE_RECOMMENDATION_1", strStatement)
End Sub
I receive error of 3075, what is it?
Thanks!
This may be something obvious, but if your SQL string is writen in your code exactly as you write it here, you need an aditional space before FROM and WHERE.
Another way to find out what is going on is to print the SQL string, using Debug.Print strStatement, and copying it to a new blank query object, and see if it works.
Hope this is helpful