How to fix syntax error in UPDATE statment in Access VBA - sql

I'm trying to build a database and ran into a problem with the syntax in an UPDATE statement. Does anyone know what's wrong with the statement?
I've tried adjusting the use of single and double quotes but it's still not working.
CurrentDb.Execute "INSERT INTO tbl_SponsorContacts (ContactName, ContactEmail, ContactPhone, SponsorID )" _
& " VALUES ('" & Me.txtContactName & "','" & Me.txtContactEmail & "', '" & Me.txtContactPhone & "' , '" & Me.txtSpnID & "')"
Else
CurrentDb.Execute "UPDATE tbl_SponsorContacts SET ContactName = " _
& " '" & Me.txtContactName & "', ContactEmail = '" & Me.txtContactEmail & "', " _
& " ContactPhone = '" & Me.txtContactPhone & "', SponsorID = '" & Me.txtSpnID & "', WHERE ContactID = '" & Me.txtContactID & "'"
End If
Exit Sub
I keep getting the 3144 syntax error which doesn't provide any hints as to the cause. Any help is appreciated.
UPDATE: Thanks to Jerry M. and forpas for suggesting to remove the comma before where. That helped but I am not getting an error 3061, "Too few parameters. Expected 1".
I will post a new question.

As #forpas mentioned in the comments, it looks like you have an extra ,
Try below:
CurrentDb.Execute "INSERT INTO tbl_SponsorContacts (ContactName, ContactEmail, ContactPhone, SponsorID )" _
& " VALUES ('" & Me.txtContactName & "','" & Me.txtContactEmail & "', '" & Me.txtContactPhone & "' , '" & Me.txtSpnID & "')"
Else
CurrentDb.Execute "UPDATE tbl_SponsorContacts SET ContactName = " _
& " '" & Me.txtContactName & "', ContactEmail = '" & Me.txtContactEmail & "', " _
& " ContactPhone = '" & Me.txtContactPhone & "', SponsorID = '" & Me.txtSpnID & "'WHERE ContactID = '" & Me.txtContactID & "'"
End If
Exit Sub ```

Related

Access Error Run-Time 2447 Insert into table

The Error shows its there is an Invalid of the . (Dot) Or ! operator
If IsNull(Me.m_1) Or IsNull(Me.u_1) Or Me.m_1 = "" Or Me.u_1 = "" Then
MsgBox "Please add all needed info"
Else
sqls = "INSERT INTO User_access (Application_ID, Modul_ID, username_ID, Clock_ID, Assinged_date, Assinged_by, Request_number) values('" & Me.A_1 & "', '" & Me.m_1 & "', '" & Me.u_1 & "', '" & Me.Clock_ID & "', '" & Date & "', '" & Me.Assinged_by & "', '" & Me.Request_number & "');"
DoCmd.RunSQL sqls
End If
I cannot see any error within the code So please Help me
Use Date():
sqls = "INSERT INTO User_access (Application_ID, Modul_ID, username_ID, Clock_ID, Assinged_date, Assinged_by, Request_number) values('" & Me.A_1 & "', '" & Me.m_1 & "', '" & Me.u_1 & "', '" & Me.Clock_ID & "', Date(), '" & Me.Assinged_by & "', '" & Me.Request_number & "');"
And no quoting of numeric values.

How do I solve Access Insert into table error?

I'm doing a project and I just used some code from YouTube where it seemed to work, but it isn't working for me. I am getting this message. :/
Run-time Error: '3134':
Syntax Error in INSERT INTO statement.
Here's the code:
Private Sub Dodaj_Click()
CurrentDb.Execute "INSERT INTO Rezervacije( JMBG, Grad, Broj dana, Broj osoba) " & _
" VALUES('" & Me.JMBG & "', '" & Me.Destinacija & "', '" & Me.Dani & "', '" & Me.Osobe & "')"
CurrentDb.Execute "INSERT INTO Putnici(Broj licne karte, Ime, Prezime, JMBG, Mobilni, Email, Pol) " & _
" VALUES('" & Me.Licna & "', '" & Me.Ime & "', '" & Me.Prezime & "', '" & Me.JMBG & "', '" & Me.Mobilni & "', '" & Me.Email & "', '" & Me.Pol & "')"
End Sub

MA run-time error '3075' Syntax Error

i have this code and it shows me run-time error '3075' Syntax Error and i dont know how to fix it . help me please
CurrentDb.Execute "UPDATE PUNONJESIT set Id_punonjesit= '" & Me.id & "', Emer_punonjesi ='" & Me.emri & "', Mbiemer_punonjesi='" & Me.mbiemri & ", Mosha='" & Me.mosha & "', Profesioni='" & Me.profesioni & "', Rroga='" & Me.rroga & "', WHERE Id_punonjesit ='" & Me.id & "'"
I'm not sure the manual data (e.g. Me.id) is retrieve from text box properties or otherwise? If they are text boxes, then you should change them to this (e.g. Me.id.value) so the access can retrieve the value.
Here is my suggestion below:
(You)
CurrentDb.Execute "UPDATE PUNONJESIT set Id_punonjesit= '" & Me.id & "', Emer_punonjesi ='" & Me.emri & "', Mbiemer_punonjesi='" & Me.mbiemri & ", Mosha='" & Me.mosha & "', Profesioni='" & Me.profesioni & "', Rroga='" & Me.rroga & "', WHERE Id_punonjesit ='" & Me.id & "'"
(Me)
CurrentDb.Execute "UPDATE PUNONJESIT set Id_punonjesit=" & Me.id.value & ", Emer_punonjesi =" & Me.emri.value & ", Mbiemer_punonjesi=" & Me.mbiemri.value & ", Mosha=" & Me.mosha.value & ", Profesioni=" & Me.profesioni.value & ", Rroga=" & Me.rroga.value & ", WHERE Id_punonjesit =" & Me.id.value & ";"
You can try it and see what error does it generate. Do let me know if it helps? (:

VBA invalid .(dot) or ! or parenthesis after splitting database

I have a database with a form that was working just fine, however now after I split the Database I'm getting an error:
There is an invalid use of the . (dot) or ! operator or invalid Parentheses.
This is the code targeted by the Debugger
DoCmd.RunSQL ("Insert Into TestingStatistics (BadgeNumber, TestName, College, Instructor, SigninTime, Special) " & _
"Values ('" & Me.txtBadgeNo.Value & "', '" & strTestName & "', '" & strCollege & "', '" & strInstructor & "', Now(), '" & Me.chkSpecial.Value & "');")
When I remove the Me.txtBadgeNo.Value and Me.chkSpecial.Value
"Values (" & strTestName & "', '" & strCollege & "', '" & strInstructor & "', Now());")
I get the error:
Syntax Error (Missing operator) in query expression 'Medicine', 'Medicine, 'Larsen', Now());'.
Again, this worked before I split the database, I need this insert statement to work, what's going on?
"Values (" & strTestName & "', '" & strCollege & "', '" & strInstructor & "', '" & Now() & "');")
You cannot just delete values; the count must match the count of fields.
Try this:
DoCmd.RunSQL ("Insert Into TestingStatistics (BadgeNumber, TestName, College, Instructor, SigninTime, Special) " & _
"Values (" & Me!txtBadgeNo.Value & ", '" & strTestName & "', '" & strCollege & "', '" & strInstructor & "', Now(), " & Me!chkSpecial.Value & ");")
Edit, to debug:
Dim SQL As String
SQL = _
"Insert Into TestingStatistics (BadgeNumber, TestName, College, Instructor, SigninTime, Special) " & _
"Values (" & Me!txtBadgeNo.Value & ", '" & strTestName & "', '" & strCollege & "', '" & strInstructor & "', Now(), " & Me!chkSpecial.Value & ");"
Debug.Print SQL
DoCmd.RunSQL SQL

Date exception in German windows during insert MS access vb.net

I have written an insert query which inserts datetime along with other columns. It works fine for all locations except when
my German client logs in and runs the application it gives him below error. I have formatted the datevalue to yyyy-mm-dd to make
culture independent.
MS access database is stored in a server in US.
German client is running application from Germany.
strDateSubmit = dtpDateSub.Value.ToString("yyyy-MM-dd")
strSaveOSTR = "INSERT INTO " & strOSTR & " ([OSTR #],[OSTR Type],[# of Samples],[RA#],[Customer],[SKF #],[Test Description]," & _
"[TestLength],[TestUnit],[TestLengthDays],[Requestor],[Date Submitted],[Seals Avail],[Fixtures Available],[Peripherals Avail],[PO Avail]," & _
"[Machine Type],[Hours to Process],[Location],[Current Status],[ErrorsPresent],[ContaminType]" & SampleREcvd1 & ", [Emp_ID],[Industry])" & _
" Values ( '" & strOSTRNum & "', '" & cmbOSTRTypes.Text & "', " & intSamples & ", '" & strRA & "', '" & strCustomer & "', '" & strSKFNum & _
"', '" & strTestDescr & "', " & intTestLength & ", '" & strTestUnits & "', '" & txtTestLDays.Text & "', '" & strRequestor & "', #" & strDateSubmit & "#, '" & strSealAvail & _
"', '" & strFixtAvail & "', '" & strPheriAvail & "', '" & strPOAvail & "', '" & strMachineClass & "', " & intHrstoProc & ",'" & g_objProp.Location & _
"', '" & strStatus & "', '" & ErrorsPresent & "', '" & ContaminationType & "'" & SampleREcvd2 & ", '" & emp_id & "', '" & Industry & "')"
Error: Syntaxfehler in Datum in Abfrageausdruck '#01.02.2016'
Do yourself a favor: Scrap the dynamic SQL and use a parameterized query, e.g.,
sampleSQL = "INSERT INTO TableName ([Date Submitted]) VALUES (?)"
Dim cmd = New OleDbCommand(sampleSQL, conn)
cmd.Parameters.AddWithValue("?", dtpDateSub.Value)
cmd.ExecuteNonQuery()
That way you can use the actual DateTime value returned by the DateTimePicker. You don't have to be concerned with locale settings or string formats or any of those aggravations.
You may need a more strict format of the date expression string:
strDateSubmit = dtpDateSub.Value.ToString("yyyy'/'MM'/'dd")
This will always return a date like "2016/02/01" (no dots or dashes), and it will concatenate correctly here:
"', #" & strDateSubmit & "#, '"
as you intend.