473,785 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Syntax Error in SQL Statement

Hi. I am trying to run the following code when the user clicks a button,
but I am getting a syntax error in the SQL. I have a feeling it has to
do with brackets. Can anyone help?
here is the SQL(watch for word wrap:
StrSql = "insert into[Products](ProductName)" 'SQL string which executes
with the RunSQL statement
16980 StrSql = StrSql & "Values ('" & TxtProductName. Text &
"');"

thanks guys

Colin

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #1
12 2889
Hi Colin

I like to assign a variable that represents the value contained by a form
control.
Then I totally seperate any variables from the textual portion of the SQL
string when doing this kind of thing.

Assigning a variable to a form's control also allows you to inspect it's
contents while in break mode.
Set a breakpoint in your code, at say the "StrSql = " line . Then run the
procedure, and you'll see what I mean.
"Hover" your mouse cursor over the "MyProductN ame" variable, and it's
contents will displayed in a "tooltip"

I'd also suggest using the .Execute method ( including the dbFailOnError)
rather than RunSQL.
The problem with using RunSQL is that some (but not all) records will be
changed by the action query, until such time that an error occurs.
If an error occurs with RunSQL, you have to look through the table and
determine which records have (or have not) been changed.
The dbFailOnError is an "all-or-nothing" approach to updating records. If an
error should occur, all changes are "rolled back".

All of the above is *my understanding* of how things work.
That being said, try it like this:

*************** *************** *************** ****
....
Dim MyDB as DAO.Database
Set MyDB = CurrentDb

Dim MyProductName As String
MyProductName = Me![TxtProductName]

StrSql = ""
SrtSql = StrSql & "INSERT INTO [Products] (ProductName) " 'You were missing
a space between [Products] and (ProductName)
SrtSql = StrSql & "VALUES ("
SrtSql = StrSql & MyProductName 'No quotes here, JUST the variable name.
SrtSql = StrSql & ");"

Debug.Print StrSql
'Open the Debug (Immediate) Window
'Copy the resulting SQL string
'Create a new query based on the Products Table
'Switch to SQL View, and paste in the SQL string (copied from Debug Window)
'Attempt to switch to Datasheet view.
' If there are any errors, Access will tell you and will even highlight the
offending portion

MyDB.Execute StrSql, dbFailOnEror

Set MyDB = Nothing
....
*************** *************** *************** ****

--
HTH,
Don
=============== ==============
Use My*****@Telus.N et for e-mail
Disclaimer:
Professional PartsPerson
Amateur Database Programmer {:o)

I'm an Access97 user, so all posted code
samples are also Access97- based
unless otherwise noted.

Do Until SinksIn = True
File/Save, <slam fingers in desk drawer>
Loop

=============== =============== ==
"ColinWard" <je*********@ho tmail.com> wrote in message
news:40******** **************@ news.newsgroups .ws...
Hi. I am trying to run the following code when the user clicks a button,
but I am getting a syntax error in the SQL. I have a feeling it has to
do with brackets. Can anyone help?
here is the SQL(watch for word wrap:
StrSql = "insert into[Products](ProductName)" 'SQL string which executes
with the RunSQL statement
16980 StrSql = StrSql & "Values ('" & TxtProductName. Text &
"');"

thanks guys

Colin

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 13 '05 #2
Thanks for your help don. I will try this on Monday. Of course you know
that this means that I will have to change all my runSQL statements
arrgh!!! LOL

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #3

Hi Don,

This is what I currently have but I get a 'Mising Operator' error with
the last part of the SQL statement.

*************** *************** *************** ***********
Dim MyDB As DAO.Database
Set MyDB = CurrentDb
Dim strSql As String
Dim MyProductName As String
MyProductName = Me![TxtProductName]
strSql = ""
strSql = strSql & "INSERT INTO [Products] (ProductName) "
strSql = strSql & "VALUES("
strSql = strSql & MyProductName 'No quotes here, JUST the variable name.
strSql = strSql & ");"

Debug.Print strSql
'Open the Debug (Immediate) Window
'Copy the resulting SQL string
'Create a new query based on the Products Table
'Switch to SQL View, and paste in the SQL string (copied from Debug
Window)
'Attempt to switch to Datasheet view.
' If there are any errors, Access will tell you and will even highlight
the
'offending portion

MyDB.Execute strSql, dbFailOnError
Set MyDB = Nothing

*************** *************** ***************

thanks Don

Colin
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #4
Hi Colin,

Sorry, I forgot to "wrap" the "MyProductN ame" variable in quotation marks
....
(notice that I did that using the Chr$(34) substitute for the ["] character
in the revised code below.)

I was getting:
INSERT INTO [Products] (ProductName) VALUES(Sunlight Dishwash Soap);

where it should have been:
INSERT INTO [Products] (ProductName) VALUES("Sunligh t Dishwash Soap");
*************** ****** Revised Code *************** **************
Private Sub cmdAddProduct_C lick()

Dim MyDB As DAO.Database
Set MyDB = CurrentDb

Dim strSql As String
Dim MyProductName As String
MyProductName = Me![TxtProductName]

strSql = ""
strSql = strSql & "INSERT INTO [Products] (ProductName) "
strSql = strSql & "VALUES("
strSql = strSql & Chr$(34) & MyProductName & Chr$(34)
strSql = strSql & ");"

'Debug.Print strSql

MyDB.Execute strSql, dbFailOnError
Set MyDB = Nothing

End Sub
*************** ****** End Revised Code *************** ********

Regards,
Don
_______________ _______________ _______________ _______________ _

"ColinWard" <je*********@ho tmail.com> wrote in message
news:40******** **************@ news.newsgroups .ws...

Hi Don,

This is what I currently have but I get a 'Mising Operator' error with
the last part of the SQL statement.

*************** *************** *************** ***********
Dim MyDB As DAO.Database
Set MyDB = CurrentDb
Dim strSql As String
Dim MyProductName As String
MyProductName = Me![TxtProductName]
strSql = ""
strSql = strSql & "INSERT INTO [Products] (ProductName) "
strSql = strSql & "VALUES("
strSql = strSql & MyProductName 'No quotes here, JUST the variable name.
strSql = strSql & ");"

Debug.Print strSql
'Open the Debug (Immediate) Window
'Copy the resulting SQL string
'Create a new query based on the Products Table
'Switch to SQL View, and paste in the SQL string (copied from Debug
Window)
'Attempt to switch to Datasheet view.
' If there are any errors, Access will tell you and will even highlight
the
'offending portion

MyDB.Execute strSql, dbFailOnError
Set MyDB = Nothing

*************** *************** ***************

thanks Don

Colin
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 13 '05 #5
On 05 Jul 2004 17:36:41 GMT, ColinWard <je*********@ho tmail.com>
wrote:

Hi Don,

This is what I currently have but I get a 'Mising Operator' error with
the last part of the SQL statement.

************** *************** *************** ************
Dim MyDB As DAO.Database
Set MyDB = CurrentDb
Dim strSql As String
Dim MyProductName As String
MyProductNam e = Me![TxtProductName]
strSql = ""
strSql = strSql & "INSERT INTO [Products] (ProductName) "
strSql = strSql & "VALUES("
strSql = strSql & MyProductName 'No quotes here, JUST the variable name.
strSql = strSql & ");"

Debug.Print strSql
'Open the Debug (Immediate) Window
'Copy the resulting SQL string
'Create a new query based on the Products Table
'Switch to SQL View, and paste in the SQL string (copied from Debug
Window)
'Attempt to switch to Datasheet view.
' If there are any errors, Access will tell you and will even highlight
the
'offending portion

MyDB.Execute strSql, dbFailOnError
Set MyDB = Nothing

************** *************** *************** *

thanks Don

Colin


Colin, this should be all you need.
=============== =
Dim strSql As String

strSql = "INSERT INTO [Products] (ProductName) VALUES('" _
& Me![TxtProductName] & "');"
'Still need to wrap a string with single quotes.

CurrentDb.Execu te strSql, dbFailOnError
=============== =

Notes:
You still need to wrap the string in single quotes, and if the Product
name has any quotes single or other, they will have to be managed.
BTW, the product usually has an ID number that is much easier to deal
with.

You don't really need an object variable to use the execute method -
just do it directly from CurrentDb - unless you need the object for
something else. Which you don't here.

For small strings like this I find that line continuation character is
much easier to read than continuous concatenating - and less typing.
My 0.02

- Jim
Nov 13 '05 #6
Hi Jim,

That "_" line-continuation character issue is definitely a matter of
personal preference then ...
I for one loathe and despise that stupid thing! <grin>

I admit that I must just be a very methodical thinker, and can only deal
with "sequential steps" while writing code.
For me, the way that the SQL strings "line up" with each other make it
easier for me to follow.
It also allows me to quicky and easily "inspect" the value of a variable (in
break mode), right inside of the SQL string.
It also all but eliminates those annoying "double-quote, single-quote,
double-quote" and the "Forms!MainForm .SubForm.Form!C ontrols.Control Name"
syntax nightmares.

As far as the "less typing" aspect goes, I have a little utility program
called SQL2VAR.EXE that does 99% of that repetitious typing for me!
I just paste in a sample SQL string from the SQL view of a query that I have
designed and tested, then hit the [Generate] button! (For short strings like
this one, though, I generally just "Copy/Paste" MySQL = MySQL & "" 4 or 5
times and then "fill in the blanks".)
After pasting the generated string inside my code window, I simply replace
the "literal" values with variables, and I'm "good to go".

I will agree with what you said about the CurrentDB thing, though. It saves
having to "Set" and "UnSet" the object. I'll likely adopt that method.
That's what I like about this forum ... you get to see a lot of alternative
methods, and learn a lot doing so.

There's my $0.02 worth. <grin>
Have a great day,
Don
"Jim Allensworth" <Ji****@NOTdata centricsolution s.com> wrote in message
news:40******** *******@netnews .comcast.net...
On 05 Jul 2004 17:36:41 GMT, ColinWard <je*********@ho tmail.com>
For small strings like this I find that line continuation character is
much easier to read than continuous concatenating - and less typing.
My 0.02

- Jim

Nov 13 '05 #7

Hi Don. This works great. I am now trying to modify it so that I can add
a second value (ProductDescrip tion) to the same record but when I do it
as below I get an error saying that the number of values and the number
of destination fields are not the same.

Thanks Don

Colin

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #8
Hi Don I forgot to post the code tht i am trying to use for two values:
******CODE START********** *************** *************
Private Sub cmdAddProduct_C lick()

Dim MyDB As DAO.Database
Set MyDB = CurrentDb

Dim strSql As String
Dim MyProductName As String
Dim MyProductDescri ption As String
MyProductName = Me![TxtProductName]
MyProductDescri ption = Me![txtProductDescr iption]

strSql = ""
strSql = strSql & "INSERT INTO [Products]
(ProductName,Pr oductDescriptio n)"
strSql = strSql & "VALUES("
strSql = strSql & Chr$(34) & MyProductName & Chr$(34)
strSql = strSql & Chr$(34) & MyProductDescri ption & Chr$(34)
strSql = strSql & ");"
'Debug.Print strSql
MyDB.Execute strSql, dbFailOnError
Set MyDB = Nothing
End Sub
*******CODE END************ *************** **************

Thanks Don

Colin

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #9
Hi Don,

On Mon, 05 Jul 2004 19:56:15 GMT, "Don Leverton"
<le************ ****@telusplane t.net> wrote:
Hi Jim,

That "_" line-continuation character issue is definitely a matter of
personal preference then ...
I for one loathe and despise that stupid thing! <grin>
Personal preference, absolutely. So much of this stuff is.

I admit that I must just be a very methodical thinker, and can only deal
with "sequential steps" while writing code.
For me, the way that the SQL strings "line up" with each other make it
easier for me to follow. I use the ampersand to "line up" the continued string. When I see that
at the beginning of a line I immediately know is continued.
It also allows me to quicky and easily "inspect" the value of a variable (in
break mode), right inside of the SQL string. That can also be done with continuation.
It also all but eliminates those annoying "double-quote, single-quote,
double-quote" and the "Forms!MainForm .SubForm.Form!C ontrols.Control Name"
syntax nightmares.
AFAICS, those have to go somewhere. And if the text value has single
or double quotes then you still have to deal with those.
As far as the "less typing" aspect goes, I have a little utility program
called SQL2VAR.EXE that does 99% of that repetitious typing for me!
I just paste in a sample SQL string from the SQL view of a query that I have
designed and tested, then hit the [Generate] button! (For short strings like
this one, though, I generally just "Copy/Paste" MySQL = MySQL & "" 4 or 5
times and then "fill in the blanks".)
After pasting the generated string inside my code window, I simply replace
the "literal" values with variables, and I'm "good to go".

I will agree with what you said about the CurrentDB thing, though. It saves
having to "Set" and "UnSet" the object. I'll likely adopt that method.
That's what I like about this forum ... you get to see a lot of alternative
methods, and learn a lot doing so.
Absolutely! The exchange of ideas is invaluable.

There's my $0.02 worth. <grin>
Have a great day,
Don


I hope your day is good also,
- Jim
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6229
by: Robert Mark Bram | last post by:
Hi All! I have the following two methods in an asp/jscript page - my problem is that without the update statement there is no error, but with the update statement I get the following error: Error Type: Microsoft OLE DB Provider for ODBC Drivers (0x80004005) Syntax error in UPDATE statement. /polyprint/dataEntry.asp, line 158
29
2477
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules" for when and how to use single quotes and double quotes in ASP? thanks! ---------------------- SQL = SQL & "WHERE '" & REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("GenKTitles.
7
248484
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a particular surname in a particular town. I can select the records fine with this syntax (testing in Oracle SQL* Plus) SELECT NAMEINFO.LASTNAME, NAMEINFO.FIRSTNAME, NAMEINFO.MIDDLENAME, NAMEINFO.GENDER, ADDRESSINFO.REGION FROM NAMEINFO, ADDRESSINFO...
7
6672
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double checked, and the insert works fine (tried to use it from access)... im using visual C# express 2k5... what could be wrong? thanks!
3
2500
by: Nathan Sokalski | last post by:
When trying to submit data to an Access database using ASP.NET I recieve the following error: System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) +41 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +174 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +92
1
3094
by: amitbadgi | last post by:
HI i am getting the foll error while conv an asp application to asp.net Exception Details: System.Runtime.InteropServices.COMException: Syntax error in UPDATE statement. Source Error: Line 112: MM_editCmd.ActiveConnection = MM_editConnection Line 113: MM_editCmd.CommandText = MM_editQuery Line 114: MM_editCmd.Execute
5
2145
by: amitbadgi | last post by:
Hi guys, I am getting the following error in teh insert statement , I am converting this asp application to asp.net, here is teh error, Exception Details: System.Runtime.InteropServices.COMException: Syntax error in INSERT INTO statement. Source Error: Line 118: MM_editCmd.ActiveConnection = MM_editConnection
7
1877
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that shows the alert. What is the essential reason? function () { alert('hi mom'); }(); function () { alert('hi dad'); }(8); var x=function () { alert('hi bro'); }();
12
1707
by: Brad Baker | last post by:
I am trying to write a simple ASP.net/C# page which allows users to select some values and produce a report based on a SQL query. I have a self posting dropdown form which allows users to select the type of report to generate: Select the type of report to display: <form runat="server"> <asp:DropDownList AutoPostBack="true" ID="report" runat="server"> <asp:listitem>Report Type 1</asp:listitem>
1
3370
by: ajos | last post by:
hi evrybdy, the problem is:- i had this running before in jsp but when i changed the jsp page using struts tags there occoured a problem... when i enter values in the 2 text boxes and click enter an exception occours which is- type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request.
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.