I am making the form to insert the records in table. I am unable to run this query as it is showing me the syntax error. I tried lot but not able to find the error. My query is as follow.
CurrentDb.Execute "
INSERT into Database2Sample(
nyu_id, location1, location2,
first_name, middle_name, last_name,
full_name, instructor_sis_id, instructor_email,
intstructor_net_id, personal_email, CV_link,
employee_nyu_global_site, highest_degree, degree_subject_area,
degree_status, degree_granting_institution, accomplishments,
Other_employee, WSQ_home_campus, teaching_language_course)
VALUES
('" & Me.nyu_id & "', '"
& Me.location1 & "', '"
& Me.location2 & "', '"
& Me.first_name & "', '"
& Me.middle_name & "', '"
& Me.last_name & "', '"
& Me.full_name & "', '"
& Me.instructor_sis_id & "', '"
& Me.instructor_email & "', '"
& Me.intstructor_net_id & "', '"
& Me.personal_email & "', '"
& Me.CV_link & "', '"
& Me.employee_nyu_global_site & "', '"
& Me.highest_degree & "', '"
& Me.degree_subject_area & "', '"
& Me.degree_status & "', '"
& Me.degree_granting_institution & "', '"
& Me.accomplishments & "', , '"
& Me.Other_employee & "', '"
& Me.WSQ_home_campus & "', '"
& Me.teaching_language_course & "')"
You have two commas in your value between accomplishments and employee field.
'" & Me.accomplishments & "', , '" & Me.Other_employee & "'
Delete any one of them will make it work.
Related
Why when I write code like this the result doesn't match the table?
Code:
=DLookUp("Quiz","Grade_Fall_2022","Program_ID= '" & DLookUp("ID","Program","Program= '" & [Text_Program1] & "'") & "'" And "TIS_ID= " & [Text_ID])
Result: https://i.stack.imgur.com/c84Xh.png
Source Table: https://i.stack.imgur.com/aywZE.png
Try with:
=DLookUp("Quiz","Grade_Fall_2022","Program_ID = '" & DLookUp("ID","Program","Program = '" & [Text_Program1] & "'") & "' And TIS_ID = " & [Text_ID])
Here's my code:
strSQL = "UPDATE tblTransactions SET Verifier = '" & strUserName & "' WHERE PatientAccountNumber = '" & Me.PatientAccountNumber & "'"
strSQL = "UPDATE tblTransactions SET VerifierAssgnDate = #" & Date & "# WHERE PatientAccountNumber = '" & Me.PatientAccountNumber & "'"
"Verifier" updates fine, but "VerifierAssignDate" throws a "field not updateable" error. It's a DateTime field.
Thanks!
You can and should combine these (and VerifierAssgnDate was probably misspelled):
strSQL = "UPDATE tblTransactions " & _
"SET Verifier = '" & strUserName & "', VerifierAssignDate = Date() " & _
"WHERE PatientAccountNumber = '" & Me.PatientAccountNumber & "'"
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Data Source=DESKTOP-3H4F5GB\MURATS;Initial Catalog=CRM;Integrated Security=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "UPDATE Firma SET (Sektor = '" & ComboBox1.Text & "', Tip = '" & ComboBox2.Text & "', Telefon = '" & MaskedTextBox1.Text & "', Web = '" & TextBox2.Text & "', Adres = '" & RichTextBox2.Text & "', OSB = '" & ComboBox3.Text & "', PR = '" & ComboBox4.Text & "', Sehir = '" & ComboBox5.Text & "', Ulke = '" & ComboBox6.Text & "', Note = '" & RichTextBox1.Text & "', Durum = '" & ComboBox7.Text & "', Email ='" & TextBox3.Text & "') WHERE Firma = '" & ComboBox8.Text & "'"**
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
con.Close()
End Try
End Sub
Your error is clearly on this line
cmd.CommandText = "UPDATE Firma SET (Sektor = '" & ComboBox1.Text & "', Tip = '" & ComboBox2.Text & "', Telefon = '" & MaskedTextBox1.Text & "', Web = '" & TextBox2.Text & "', Adres = '" & RichTextBox2.Text & "', OSB = '" & ComboBox3.Text & "', PR = '" & ComboBox4.Text & "', Sehir = '" & ComboBox5.Text & "', Ulke = '" & ComboBox6.Text & "', Note = '" & RichTextBox1.Text & "', Durum = '" & ComboBox7.Text & "', Email ='" & TextBox3.Text & "') WHERE Firma = '" & ComboBox8.Text & "'"**
You have to remove the () from the Update statement to be like the below
cmd.CommandText = "UPDATE Firma SET Sektor = '" & ComboBox1.Text & "', Tip = '" & ComboBox2.Text & "', Telefon = '" & MaskedTextBox1.Text & "', Web = '" & TextBox2.Text & "', Adres = '" & RichTextBox2.Text & "', OSB = '" & ComboBox3.Text & "', PR = '" & ComboBox4.Text & "', Sehir = '" & ComboBox5.Text & "', Ulke = '" & ComboBox6.Text & "', Note = '" & RichTextBox1.Text & "', Durum = '" & ComboBox7.Text & "', Email ='" & TextBox3.Text & "' WHERE Firma = '" & ComboBox8.Text & "'"
remove your open and close parenthesis on:
cmd.CommandText = "UPDATE Firma SET (Sektor = '" & ComboBox1.Text & "', Tip = '" & ComboBox2.Text & "', Telefon = '" & MaskedTextBox1.Text & "', Web = '" & TextBox2.Text & "', Adres = '" & RichTextBox2.Text & "', OSB = '" & ComboBox3.Text & "', PR = '" & ComboBox4.Text & "', Sehir = '" & ComboBox5.Text & "', Ulke = '" & ComboBox6.Text & "', Note = '" & RichTextBox1.Text & "', Durum = '" & ComboBox7.Text & "', Email ='" & TextBox3.Text & "')
answer
cmd.CommandText = "UPDATE Firma SET Sektor = '" & ComboBox1.Text & "', Tip = '" & ComboBox2.Text & "', Telefon = '" & MaskedTextBox1.Text & "', Web = '" & TextBox2.Text & "', Adres = '" & RichTextBox2.Text & "', OSB = '" & ComboBox3.Text & "', PR = '" & ComboBox4.Text & "', Sehir = '" & ComboBox5.Text & "', Ulke = '" & ComboBox6.Text & "', Note = '" & RichTextBox1.Text & "', Durum = '" & ComboBox7.Text & "', Email ='" & TextBox3.Text & "'
and some addition try to learn to use parametized queries comment if you want to learn.
can anyone help me where am I doing wrong.. im building an update statement with multiple columns to update MSQL with VBA and values from Excel. heres my simple code, Im parsing variable in function to execute SQL command (I have a table name "People" and I have define all the column) below:
Function GetUpdateTextSQL(PIC As String,
Customer As String,
DOB As Date,
Rank As String,
Organization As String,
Status As String,
Gender As String,
Religion As String,
Hobby As String,
CreatedBy1 As String,
CreatedOn1 As Date,
ChangedBy1 As String,
ChangedOn1 As Date,
PeopleID As Integer) As String
SQLStr = _
"UPDATE People" & _
"SET PIC = " & _
"'" & PIC & "', Customer = '" & Customer & "'," & _
"DOB = '" & Format(DOB, "yyyy-mm-dd") & "', Rank = '" & Rank & "'," & _
"Organization = '" & Organization & "',Status = '" & Status & "', Gender = '" & Gender & "'," & _
"Religion = '" & Religion & "', Hobby = '" & Hobby & "'," & _
"CreatedBy = '" & CreatedBy1 & "', CreatedOn = '" & CreatedOn1 & "'," & _
"ChangedBy = '" & ChangedBy1 & "', ChangedOn = '" & ChangedOn1 & "'" & _
"WHERE PeopleID = & PeopleID &;"
GetUpdateTextSQL = SQLStr
//And here Im inserting and executing command below to get values from excel:
For Each r In Range("A45", Range("A45").End(xlDown))
CmdForSave.CommandText = _
GetUpdateTextSQL( _
r.Offset(0, 1).Value, r.Offset(0, 2).Value, _
r.Offset(0, 3).Value, _
r.Offset(0, 4).Value, r.Offset(0, 5).Value, _
r.Offset(0, 6).Value, r.Offset(0, 7).Value, _
r.Offset(0, 8).Value, r.Offset(0, 9).Value, _
r.Offset(0, 10).Value, r.Offset(0, 11).Value, _
r.Offset(0, 12).Value, r.Offset(0, 13).Value, _
r.Offset(0, 0).Value)
CmdForSave.Execute
Next r
The error I get is 'Incorrect Syntax near 'PIC'
What is wrong with my code? Iam using MS SQL Express (SQL 2012)
Thanks All,
I have figured it out why the problem is with space on the update SQL Statement.
The right statement using VBA (space before SET, space before WHERE):
SQLStr = _
"UPDATE People" & _
" SET PIC = '" & PIC & "', Customer = '" & Customer & "'" & _
" WHERE PeopleID = '" & PeopleID & "';"
The above error is being thrown when I run the below command:
CurrentDb.Execute "INSERT INTO Table2(FirstName, LastName)" & "VALUES('" & Me.frst_Name_txt & ", '" & Me.lst_Name_txt & "','" & "')"
Can anyone see where I am going wrong?
Thanks
The error:
You have extra value '" & "' and also Text data types require delimiters around the value. At its simplest an apostrophe.
Try this
CurrentDb.Execute "INSERT INTO Table2(FirstName, LastName)" & "VALUES('" & Me.frst_Name_txt & "','" & Me.lst_Name_txt & "')"
You're missing an apostrophe: CurrentDb.Execute "INSERT INTO Table2(FirstName, LastName) VALUES('" & Me.frst_Name_txt & "', '" & Me.lst_Name_txt & "')"