Syntax error while creating query in code - mysql

comm = "INSERT INTO CUST_DETAILS (cust_ID,cust_name,Address,Email-id,Phone_no)"
+ "values ('" & txtID.Text & "','" & txtName.Text & "','"
& txtAdd.Text & "','" & txtEmail.Text & "','" & txtPhone.Text & "')"
What is the error?

VB6 does not automatically know that a line continues after a line break. If you do not use the line continuation character VB6 sees the end of the line as the end of the statement, and the next line as a new statement. So the second and third lines are syntactically incorrect. To fix it add a single space followed by an underscore after the first and second lines to tell the VB6 that the line continues on the next line.
comm = "INSERT INTO CUST_DETAILS (cust_ID,cust_name,Address,Email-id,Phone_no)" _
& "values ('" & txtID.Text & "','" & txtName.Text & "','" _
& txtAdd.Text & "','" & txtEmail.Text & "','" & txtPhone.Text & "')"
MSDN reference here
Also please note I replaced your "+" which will concatenate a string, but is bad practice with an ampersand.

Related

MS Access using UPDATE statement keeps entering new data

I'm using an UPDATE Statement but whenever I click the Edit button then Update, it's entering a new line but with the same data.
My code:
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. for insert
'2. for update
If Me.txtNumber.Tag & "" = "" Then
'this is for insert new
'add data to table
CurrentDb.Execute "INSERT INTO tblcompany (companyname, companyaddress, contactnumber, contactperson, emailaddress, website, plantlocation, projectinfo, consultant) " & _
" VALUES('" & Me.txtCompanyName & "','" & _
Me.txtCompanyAddress & "','" & Me.txtContactNumber & "','" & _
Me.txtContactPerson & "','" & Me.txtEmailAddress & "','" & _
Me.txtWebsite & "','" & Me.txtPlantLocation & "','" & _
Me.txtProjectInfo & "','" & Me.txtConsultant & "')"
Else
'otherwise (tag of txtNumber store the number of company to be modified)
CurrentDb.Execute "UPDATE tblcompany " & _
" SET companyname='" & Me.txtCompanyName & "''" & _
", companyaddress='" & Me.txtCompanyAddress & "''" & _
", contactnumber='" & Me.txtContactNumber & "'" & _
", contactperson='" & Me.txtContactPerson & "''" & _
", emailaddress='" & Me.txtEmailAddress & "'" & _
", website='" & Me.txtWebsite & "'" & _
", plantlocation='" & Me.txtPlantLocation & "''" & _
", projectinfo='" & Me.txtProjectInfo & "''" & _
", consultant='" & Me.txtConsultant & "''" & _
" WHERE number=" & Me.txtNumber.Tag
End If
'clear form
cmdClear_Click
'refresh data in list on form
frmCompanySub.Form.Requery
End Sub
Isn't the Tag property empty by default? if you're saving a new record you will have to set a tag property equal to the number. So that when you come to update a record the Where number = & me.txt.number.tag is true. Otherwise all record tags of "" will equal "".
Also, tab in on your currentDb.execute line (after then).

Inssert Statment, Values not Coded correctly

Complete VBA newb...trying to enter some simple data into a simple table from an access form.
I used a youtube video and attempted to modify the code for my use, however I can get seem to get passed errors.
Here is the code mapped to the button that I am using to enter in the data
Private Sub Command0_Click()
'add data to table
CurrentDb.Execute "INSERT INTO EmpLunchTBL (FullName, ManName, TuesOrder, ThursOrder, PayType)" & _
"VALUES (" ' & Me.txtName & ",'" & Me.txtMS & "','" & Me.cboTues & "','" & Me.cboThurs & "','" & Me.cboPaytype & "')"
End Sub
Pretty Straight forward, I know the issue lies somewhere within my values area...but again total newb...
Thank you!
I think the "VALUES (" ' is the problem. Try this:
Private Sub Command0_Click()
'add data to table
Debug.Print "INSERT INTO EmpLunchTBL (FullName, ManName, TuesOrder, ThursOrder, PayType)" & _
"VALUES ('" & Me.txtName & "','" & Me.txtMS & "','" & Me.cboTues & "','" & Me.cboThurs & "','" & Me.cboPaytype & "')"
End Sub
Edit: Another single quote was missing.

showing run time error 3134 in access from

it is giving me an run time error 3134 syntax error in insert into statement while clicking on the control.
Private Sub CmdAddNew_Click()
'add data to table
CurrentDb.Execute "INSERT INTO tblemployee(,firstname,lastname,Address,city)" & _
"VALUES('" & Me.txtfirstname & "','" & Me.txtlastname & "','" & Me.txtaddress & "','" & Me.txtcity & "')"
tblemployee(,firstname
Extra comma here?

Access VBA - run time error '3075'

For starters, I have practically no knowledge of VBA Code. What I'm trying to do here is take information from a form and subform and enter it as a new record into a table that is set as the record source of the subform.
The error code reads: run time error '3075':
Syntax Error (Missing Operator) in query expression 'GENERAL METAL (CUBEX)'.
Also I apologize for how messy it is. I honestly just tried to copy what I saw in a YouTube video that kind of represented what I was trying to do.
CurrentDb.Execute "INSERT INTO workingorders(customer, partname, partnumber, metal, grade, unitweight, Process, subcontract, MoldDescription, moldlocation, specialconcerns, shippinginst, datereq, orderdate, qtyordered, qtycast) " & _
" VALUES(" & Me.customer & ", '" & Me.partname & "','" & Me.partnumber & "','" & Me.metal & "','" & Me.grade & "','" & Me.unitweight & "','" & Me.Process & "','" & Me.subcontract & "','" & Me.MoldDescription & "','" & Me.moldlocation & _
Me.specialconcerns & "','" & Me.shippinginst & "','" & Me.datereq & "','" & Me.orderdate & "','" & Me.qtyordered & "','" & Me.qtycast & "')"
This part concerns me:
'" & Me.moldlocation & _ Me.specialconcerns & "'
It looks like you're missing the closing quote before the line suppression. Any time you see "& _" it's telling the code that you're moving to a new line, but to supress that line break when the code is run. You generally need to close up your quotes before you do that, just like was done in the other line suppressor:
qtycast) " & _ " VALUES(
So, in short, give this a shot:
CurrentDb.Execute "INSERT INTO workingorders(customer, partname, partnumber, metal, grade, unitweight, Process, subcontract, MoldDescription, moldlocation, specialconcerns, shippinginst, datereq, orderdate, qtyordered, qtycast) " & _
" VALUES(" & Me.customer & ", '" & Me.partname & "','" & Me.partnumber & "','" & Me.metal & "','" & Me.grade & "','" & Me.unitweight & "','" & Me.Process & "','" & Me.subcontract & "','" & Me.MoldDescription & "','" & Me.moldlocation "'," & _
"'" & Me.specialconcerns & "','" & Me.shippinginst & "','" & Me.datereq & "','" & Me.orderdate & "','" & Me.qtyordered & "','" & Me.qtycast & "')"
Since I don't know your data, I'll just remind you that anything that's TEXT needs to be enclosed with single quotes (i.e. '" & Me.grade & "',) and anything that's an INT does not need the single quote (i.e. " & Me.customer & ",). Just make sure all your variables are enclosed accordingly or that will cause an error as well.
If this answers your question, please don't forget to give the answer a checkmark. Thanks!

How to add a new record from unbound fields of two tables in to a subform

Hey there Good Day can anyone help me please?
I got a mainform and a subform and Ive created a query to join the two tables and ive tried to use the query to insert fields which is unbound, my subform fields which im getting from my join query if I insert into my fields it says and click the Add button I get a error it says:
Run-time error '3134':
Syntax error in insert into statement.
Code I used below:
Private Sub Add_Click()
CurrentDb.Execute "INSERT INTO PlantTransactionQuery(TransactionID,Plant Number,Categories,Description,Location,TransactionDate,Opening_Hours,Closing_Hours,Hours Worked,Fuel,Fuel Cons Fuel/Hours,Hour Meter Replaced,Comments)" & _
"VALUES(" & Me.txt13 & ",'" & Me.txt1 & "','" & Me.txt2 & "','" & Me.txt3 & "','" & Me.txt4 & "','" & Me.txt5 & "','" & Me.txt6 & "','" & Me.txt7 & "','" & Me.txt8 & "','" & Me.txt9 & "','" & Me.txt10 & "'," & Me.txt11 & "," & Me.txt12 & ")"
PlantTransactionsubform.Form.Requery
End Sub
I dont know if u can insert into a query and i dont know which name I must put into brackets for reserved name.
Any help will be much appreciated of how to add two tables in a subform in a button onclick on the same page or even maybe i made a mistake in my code for me everything looks good.
Thanks in advance
You need to put square brackets [] around any table or field name that
contains spaces or "funny characters", or
is an Access reserved word.
Try this instead:
CurrentDb.Execute "INSERT INTO PlantTransactionQuery (TransactionID,[Plant Number],Categories,Description,Location,TransactionDate,Opening_Hours,Closing_Hours,[Hours Worked],Fuel,[Fuel Cons Fuel/Hours],[Hour Meter Replaced],Comments) " & _
"VALUES (" & Me.txt13 & ",'" & Me.txt1 & "','" & Me.txt2 & "','" & Me.txt3 & "','" & Me.txt4 & "','" & Me.txt5 & "','" & Me.txt6 & "','" & Me.txt7 & "','" & Me.txt8 & "','" & Me.txt9 & "','" & Me.txt10 & "'," & Me.txt11 & "," & Me.txt12 & ")"
Edit
You can make SQL statements easier to verify visually if you break them up line-by-line, something like this:
CurrentDb.Execute _
"INSERT INTO [PlantTransactionQuery] (" & _
"[TransactionID], " & _
"[Plant Number], " & _
"[Categories], " & _
"[Description], " & _
"[Location], " & _
"[TransactionDate], " & _
"[Opening_Hours], " & _
"[Closing_Hours], " & _
"[Hours Worked], " & _
"[Fuel], " & _
"[Fuel Cons Fuel/Hours], " & _
"[Hour Meter Replaced], " & _
"[Comments] " & _
") VALUES (" & _
Me.txt13 & ", " & _
"'" & Me.txt1 & "', " & _
"'" & Me.txt2 & "', " & _
"'" & Me.txt3 & "', " & _
"'" & Me.txt4 & "', " & _
"#" & Me.txt5 & "#, " & _
"'" & Me.txt6 & "', " & _
"'" & Me.txt7 & "', " & _
"'" & Me.txt8 & "', " & _
"'" & Me.txt9 & "', " & _
"'" & Me.txt10 & "', " & _
Me.txt11 & ", " & _
Me.txt12 & " " & _
")"
The 3061 error message "Too few parameters..." can happen if a field (column) name is mis-spelled, or a field name that happens to be a reserved word is not enclosed in square brackets. I've edited the query above so all table and column names are bracketed, just to be safe.
Also, check that you are quoting strings and are not quoting numbers. A mistake there could also result in the aforementioned error.
As for [TransactionDate], you probably should un-bind the text box for consistency. Notice that the value for that field is enclosed in hash marks (#) instead of single quotes. You can add a "date" input mask to the text box, or you can use something like...
Format(CDate(Me.txt5), "yyyy-mm-dd")
...to format the date for you.
Finally, be aware that building SQL strings like that leaves you open to SQL injection problems, so you might also consider replacing that .Execute with something like
Dim rst as DAO.Recordset
Set rst = CurrentDB.OpenRecordset("[PlantTransactionQuery]", dbOpenTable)
rst.AddNew
rst![TransactionID] = CLng(Me.txt13)
rst![Plant Number] = Me.txt1
rst![Categories] = Me.txt2
...
rst![Comments] = Me.txt12
rst.Update