473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Syntax Error in INSERT INTO statement and missing operator in query for UPDATE

Richard Alvarez
Expand|Select|Wrap|Line Numbers
  1. Dim Request As String, rsData As Recordset
  2.  
  3.     If Me.cmdUpdate.Caption = "&Save" Then
  4.  
  5.         Request = "INSERT INTO tblMasterData VALUES '" & Replace(Me.txtID.Text, " '", "''") & "'," & _
  6.             "'" & Replace(Me.txtFName.Text, "'", "''") & "'," & _
  7.             "'" & Replace(Me.cboPosition.Text, "'", "''") & "'," & _
  8.             "'" & Replace(Me.txtStatus.Text, "'", "''") & "'," & _
  9.             "'" & Replace(Me.txtRate.Text, "'", "''") & "'," & _
  10.             "'" & Replace(Me.dtpStart.value, "'", "''") & "'," & _
  11.             "'" & Replace(Me.dtpEnd.value, "'", "''") & "'," & _
  12.             "'" & Replace(Me.txtAge.Text, "'", "''") & "'," & _
  13.             "'" & Replace(Me.txtAddress.Text, "'", "''") & "'"
  14.  
  15.     ElseIf Me.cmdUpdate.Caption = "&Update" Then
  16.  
  17.         Request = "UPDATE tblMasterData SET EmpName ='" & Replace(Me.txtFName.Text, "'", "''") & "'," & _
  18.             "EmpPosition = '" & Replace(Me.cboPosition.Text, "'", "''") & "'," & _
  19.             "EmpStatus = '" & Replace(Me.txtStatus.Text, "'", "''") & "'," & _
  20.             "EmpRate = '" & Replace(Me.txtRate.Text, "'", "''") & "'," & _
  21.             "DateHired = '" & Replace(Me.dtpStart.value, "'", "''") & "'," & _
  22.             "Endo = '" & Replace(Me.dtpEnd.value, "'", "''") & "'," & _
  23.             "EmpAge = '" & Replace(Me.txtAge.Text, "'", "''") & "'," & _
  24.             "EmpAddress = '" & Replace(Me.txtAddress.Text, "'", "''") & "'"
  25.  
  26.         Request = Request & " WHERE EmpID = " & Me.txtID.Text
  27.  
  28.         cmdAdd.Caption = "&Add"
  29.     End If
  30.  
  31.     DBConn.Execute Request
Apr 17 '11 #1

✓ answered by VijaySofist

Hi,

I don't think there is any Syntax Error in the Query, But you may try to Add Open & Close Parenthesis in the Insert Query and Use the Format function for Date in Both the Queries

So the resulting query will look like
Expand|Select|Wrap|Line Numbers
  1. Dim Request As String, rsData As Recordset
  2.  
  3.     If Me.cmdUpdate.Caption = "&Save" Then
  4.  
  5.         Request = "INSERT INTO tblMasterData VALUES ('" & Replace(Me.txtID.Text, " '", "''") & "'," & _
  6.             "'" & Replace(Me.txtFName.Text, "'", "''") & "'," & _
  7.             "'" & Replace(Me.cboPosition.Text, "'", "''") & "'," & _
  8.             "'" & Replace(Me.txtStatus.Text, "'", "''") & "'," & _
  9.             "'" & Replace(Me.txtRate.Text, "'", "''") & "'," & _
  10.             "'" & Format(Me.dtpStart.value, "MM/dd/yyyy") & "'," & _
  11.             "'" & Format(Me.dtpEnd.value, "MM/dd/yyyy") & "'," & _
  12.             "'" & Replace(Me.txtAge.Text, "'", "''") & "'," & _
  13.             "'" & Replace(Me.txtAddress.Text, "'", "''") & "')"
  14.  
  15.     ElseIf Me.cmdUpdate.Caption = "&Update" Then
  16.         Request=""
  17.         Request = "UPDATE tblMasterData SET EmpName ='" & Replace(Me.txtFName.Text, "'", "''") & "'," & _
  18.             "EmpPosition = '" & Replace(Me.cboPosition.Text, "'", "''") & "'," & _
  19.             "EmpStatus = '" & Replace(Me.txtStatus.Text, "'", "''") & "'," & _
  20.             "EmpRate = '" & Replace(Me.txtRate.Text, "'", "''") & "'," & _
  21.             "DateHired = '" & Format(Me.dtpStart.value, "MM/dd/yyyy") & "'," & _
  22.             "Endo = '" & Format(Me.dtpEnd.value, "MM/dd/yyyy") & "'," & _
  23.             "EmpAge = '" & Replace(Me.txtAge.Text, "'", "''") & "'," & _
  24.             "EmpAddress = '" & Replace(Me.txtAddress.Text, "'", "''") & "'"
  25.  
  26.         Request = Request & " WHERE EmpID = " & Me.txtID.Text
  27.  
  28.         cmdAdd.Caption = "&Add"
  29.     End If
  30.  
  31.     DBConn.Execute Request
  32.  
  33.  
And Also use Val Function for Numeric values without using single quotes whereever necessary in the Query

Regards
Vijay.R

2 1648
VijaySofist
107 100+
Hi,

I don't think there is any Syntax Error in the Query, But you may try to Add Open & Close Parenthesis in the Insert Query and Use the Format function for Date in Both the Queries

So the resulting query will look like
Expand|Select|Wrap|Line Numbers
  1. Dim Request As String, rsData As Recordset
  2.  
  3.     If Me.cmdUpdate.Caption = "&Save" Then
  4.  
  5.         Request = "INSERT INTO tblMasterData VALUES ('" & Replace(Me.txtID.Text, " '", "''") & "'," & _
  6.             "'" & Replace(Me.txtFName.Text, "'", "''") & "'," & _
  7.             "'" & Replace(Me.cboPosition.Text, "'", "''") & "'," & _
  8.             "'" & Replace(Me.txtStatus.Text, "'", "''") & "'," & _
  9.             "'" & Replace(Me.txtRate.Text, "'", "''") & "'," & _
  10.             "'" & Format(Me.dtpStart.value, "MM/dd/yyyy") & "'," & _
  11.             "'" & Format(Me.dtpEnd.value, "MM/dd/yyyy") & "'," & _
  12.             "'" & Replace(Me.txtAge.Text, "'", "''") & "'," & _
  13.             "'" & Replace(Me.txtAddress.Text, "'", "''") & "')"
  14.  
  15.     ElseIf Me.cmdUpdate.Caption = "&Update" Then
  16.         Request=""
  17.         Request = "UPDATE tblMasterData SET EmpName ='" & Replace(Me.txtFName.Text, "'", "''") & "'," & _
  18.             "EmpPosition = '" & Replace(Me.cboPosition.Text, "'", "''") & "'," & _
  19.             "EmpStatus = '" & Replace(Me.txtStatus.Text, "'", "''") & "'," & _
  20.             "EmpRate = '" & Replace(Me.txtRate.Text, "'", "''") & "'," & _
  21.             "DateHired = '" & Format(Me.dtpStart.value, "MM/dd/yyyy") & "'," & _
  22.             "Endo = '" & Format(Me.dtpEnd.value, "MM/dd/yyyy") & "'," & _
  23.             "EmpAge = '" & Replace(Me.txtAge.Text, "'", "''") & "'," & _
  24.             "EmpAddress = '" & Replace(Me.txtAddress.Text, "'", "''") & "'"
  25.  
  26.         Request = Request & " WHERE EmpID = " & Me.txtID.Text
  27.  
  28.         cmdAdd.Caption = "&Add"
  29.     End If
  30.  
  31.     DBConn.Execute Request
  32.  
  33.  
And Also use Val Function for Numeric values without using single quotes whereever necessary in the Query

Regards
Vijay.R
Apr 18 '11 #2
IT DID THE JOB! Sir thank you so much for that. one more thing, could you please explain to me what's the difference between the first one that i use with the date and the one you give using that format function? idid some experimenting and you're right, it does work with the mm/dd/yyy format. Thankkk you so much again for your reply.
Apr 19 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: alexz | last post by:
valuA = (request.form("toadd")) If valuA = "" then SQL = "UPDATE CourseReg SET attended='Active' WHERE ID IN("&request.form("toadd")&")" Set RS = MyConn.Execute(SQL) End If MyConn.Close Set...
2
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error from the code below! Error: INSERT statement conflicted with COLUMN FOREIGN KEY constraint...
3
by: jinhy82 | last post by:
Hi! I am currently creating a Registration form which contained: UserID Password, FirstName and LastName. These details would be inserted into Ms Access when I click submi button. But I...
7
by: John Øllgård Jensen | last post by:
Hi Using MS Asccess 2000: In a query I'm trying to create a new field with following expression: FilmDate: Left(,4) The field "FilmNo" is another text field in the query. This is...
0
by: M | last post by:
TableAdapter Configuration Wizard do not create Insert Statement(It Creates SELECT, UPDATE, DELETE statements) So if I try using Myadapter.update(MydataTable) to add a new row. I get Error...
26
by: irisbu | last post by:
Hello everybody! I have created a form in word to automatically create mail merges from an access database. I have the following code: Private Sub CommandButton1_Click() Documents.Open...
7
by: Yesurbius | last post by:
I am receiving the following error when attempting to run my query. In my mind - this error should not be happening - its a straight-forward query with a subquery. I am using Access 2003 with all...
9
by: Ed Pisa | last post by:
Hello, I have been working with this problem now for several days. I can delete and Update my data but I can not get it to insert a new record. I receive the syntax error insert into. I am not...
0
by: Ed Pisa | last post by:
Hello, I have been working with this problem now for several days. I can delete and Update my data but I can not get it to insert a new record. I receive the syntax error insert into. I am not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.