473,399 Members | 2,159 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,399 software developers and data experts.

SQL syntax errors and problems

69
I am a complete noob with Access, and have been struggling along for a while and now reached a point where I am just stuck. I had made a custom form where some of it populates through other SQL queries and some of the information gets entered in by hand/keyboard. Now I am at the point where I want to save the data in the form to a table when the user clicks the button and I am having some syntax errors.

The first way I tried is like this...
Expand|Select|Wrap|Line Numbers
  1. Private Sub savequote_Click()
  2.  
  3. SQLTEXTINFO = "INSERT INTO Quote_Info (QuoteNumber, thisdate, CustomerPO, SalesPersonName, Discount, DealerNumber, Dealer, DealerAddress, DealerCity, DealerState, DealerZip, ContactName, ContactPhone, ContactFax, ContactEmail, TotalSkids, TotalWeight, Tozip, QuoteTotal, FreightCost, YFClass, YFZip, YFweight) VALUES Me.Quote_number, Me.Quote_date, Me.Dealer_po, Me.Quote.salesperson, Me.Dealer_pricing, Me.Dealer_id, Me.Dealer_name, Me.Dealer_address, Me.Dealer_city, Me.Dealer_state, Me.Dealer_zip, Me.Dealer_contactname, Me.Dealer_contactphone, Me.Dealer_contactfax, Me.Dealer_contactemail, Me.Total_skids, Me.Total_weight, Me.Zip_ship, Me.Total_cartcost, Me.Freight_cost, Me.YF_class, Me.YF_zip, Me.YF_weight;"
  4.  
  5. DoCmd.RunSQL SQLTEXTINFO
  6.  
  7. End Sub
  8.  
And attempting to run the above code gives a syntax error. I made a slight change to the code, by putting ( ) around the VALUES like the following and a little pop up box will begin to ask me for all the values for every item listed in the VALUES (...) part of the code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub savequote_Click()
  2.  
  3. SQLTEXTINFO = "INSERT INTO Quote_Info (QuoteNumber, thisdate, CustomerPO, SalesPersonName, Discount, DealerNumber, Dealer, DealerAddress, DealerCity, DealerState, DealerZip, ContactName, ContactPhone, ContactFax, ContactEmail, TotalSkids, TotalWeight, Tozip, QuoteTotal, FreightCost, YFClass, YFZip, YFweight) VALUES (Me.Quote_number, Me.Quote_date, Me.Dealer_po, Me.Quote.salesperson, Me.Dealer_pricing, Me.Dealer_id, Me.Dealer_name, Me.Dealer_address, Me.Dealer_city, Me.Dealer_state, Me.Dealer_zip, Me.Dealer_contactname, Me.Dealer_contactphone, Me.Dealer_contactfax, Me.Dealer_contactemail, Me.Total_skids, Me.Total_weight, Me.Zip_ship, Me.Total_cartcost, Me.Freight_cost, Me.YF_class, Me.YF_zip, Me.YF_weight);"
  4.  
  5. DoCmd.RunSQL SQLTEXTINFO
  6.  
  7. End Sub
  8.  
So I am at a little lost on how to handle this situation. I don't even know if this is the best method to insert new data into the table, or if I am able to use Me.name variables in this manner even though all the Me.name variables all get assigned a value earlier in the form. Any help or direction would be greatly appreciated.

Thank You.
Oct 22 '07 #1

✓ answered by Jim Doherty

I am a complete noob with Access, and have been struggling along for a while and now reached a point where I am just stuck. I had made a custom form where some of it populates through other SQL queries and some of the information gets entered in by hand/keyboard. Now I am at the point where I want to save the data in the form to a table when the user clicks the button and I am having some syntax errors.

The first way I tried is like this...
Expand|Select|Wrap|Line Numbers
  1. Private Sub savequote_Click()
  2.  
  3. SQLTEXTINFO = "INSERT INTO Quote_Info (QuoteNumber, thisdate, CustomerPO, SalesPersonName, Discount, DealerNumber, Dealer, DealerAddress, DealerCity, DealerState, DealerZip, ContactName, ContactPhone, ContactFax, ContactEmail, TotalSkids, TotalWeight, Tozip, QuoteTotal, FreightCost, YFClass, YFZip, YFweight) VALUES Me.Quote_number, Me.Quote_date, Me.Dealer_po, Me.Quote.salesperson, Me.Dealer_pricing, Me.Dealer_id, Me.Dealer_name, Me.Dealer_address, Me.Dealer_city, Me.Dealer_state, Me.Dealer_zip, Me.Dealer_contactname, Me.Dealer_contactphone, Me.Dealer_contactfax, Me.Dealer_contactemail, Me.Total_skids, Me.Total_weight, Me.Zip_ship, Me.Total_cartcost, Me.Freight_cost, Me.YF_class, Me.YF_zip, Me.YF_weight;"
  4.  
  5. DoCmd.RunSQL SQLTEXTINFO
  6.  
  7. End Sub
  8.  
And attempting to run the above code gives a syntax error. I made a slight change to the code, by putting ( ) around the VALUES like the following and a little pop up box will begin to ask me for all the values for every item listed in the VALUES (...) part of the code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub savequote_Click()
  2.  
  3. SQLTEXTINFO = "INSERT INTO Quote_Info (QuoteNumber, thisdate, CustomerPO, SalesPersonName, Discount, DealerNumber, Dealer, DealerAddress, DealerCity, DealerState, DealerZip, ContactName, ContactPhone, ContactFax, ContactEmail, TotalSkids, TotalWeight, Tozip, QuoteTotal, FreightCost, YFClass, YFZip, YFweight) VALUES (Me.Quote_number, Me.Quote_date, Me.Dealer_po, Me.Quote.salesperson, Me.Dealer_pricing, Me.Dealer_id, Me.Dealer_name, Me.Dealer_address, Me.Dealer_city, Me.Dealer_state, Me.Dealer_zip, Me.Dealer_contactname, Me.Dealer_contactphone, Me.Dealer_contactfax, Me.Dealer_contactemail, Me.Total_skids, Me.Total_weight, Me.Zip_ship, Me.Total_cartcost, Me.Freight_cost, Me.YF_class, Me.YF_zip, Me.YF_weight);"
  4.  
  5. DoCmd.RunSQL SQLTEXTINFO
  6.  
  7. End Sub
  8.  
So I am at a little lost on how to handle this situation. I don't even know if this is the best method to insert new data into the table, or if I am able to use Me.name variables in this manner even though all the Me.name variables all get assigned a value earlier in the form. Any help or direction would be greatly appreciated.

Thank You.
Hi Lumpy,

Given you are a newbie you have at least discovered the VBA window then :)

You are on the right track just needs tweaking thats all.

There are two methods for entering data into the table one is via the .AddNew method of an Recordset object there are lots or references on how to do that using that way on here so I won't reinvent the wheel on that one you can quite easily research that on here.

Your post uses the DoCmd.RunSQL command to do what that command is designed to do within Access, namely to run an 'Action' query using the SQL language provided by you and taken as part of its argument. In so providing that SQL you provide a 'string' of characters on which the command bases itself.

So what is the string? the string is a continuous sequence of characters taken together to form the argument. Somewhat obvious you might think, but not to some of the people some of the time.

In your case you need to build that string from a combination of literal characters and values provided by the contents of screen controls. The way you have it at the moment is NOT doing that it is saying this:

"I want to insert the string description 'Me.Quote_number' into the quote number field of the table" understand the difference? you are NOT saying take the value from this control and pass that value to the string.

So in building the string for the insert SQL as you have it there, you need to amend and build it along these lines (I break it into parts for reasons of clarity and demonstrating how you string build hopefully rather than anything else)

Expand|Select|Wrap|Line Numbers
  1.  Dim MyInsert AS String 
  2. Dim MyValues as String
  3.  
  4. MyInsert ="INSERT INTO Quote_Info (QuoteNumber, thisdate, CustomerPO, SalesPersonName, Discount, DealerNumber, Dealer, DealerAddress, DealerCity, DealerState, DealerZip, ContactName, ContactPhone, ContactFax, ContactEmail, TotalSkids, TotalWeight, Tozip, QuoteTotal, FreightCost, YFClass, YFZip, YFweight) "
  5.  

Expand|Select|Wrap|Line Numbers
  1.  MyValues= "VALUES (" 
  2.  
  3. MyValues = MyValues & " ' " & Me!QuoteNumber &" ', "
  4. MyValues = MyValues & " # " & Me!QuoteDate &" #, " 
  5. MyValues = MyValues & " ' " & Me!Dealer_po &" ', "
  6.  
You keep on doing this till you exhaust your values list as it applies to your list of insert fields. What are these bits either side of my control names? I hear you ask. They are delimiting characters that are telling the SQL what the data type is that is being passed # denotes a date value for instance.

Now the next effect of this is that the string variable MyValues is increasing in length each time you append unto itself so that when you get to the DoCmd.RunSQL command the actual string you are passing to the command is a joined combination of the variables MyInsert and MyValues.......... like this:

Expand|Select|Wrap|Line Numbers
  1.  DoCmd.RunSQL MyInsert & MyValues
That is the fundamental idea basically! If you want to see the value of these variables 'from your screen' instead of running the command then place and unbound textbox on your screen and call it txtMySQL then place this command

Expand|Select|Wrap|Line Numbers
  1. Me!txtMySQL=MyInsert & MyValues
immediately before the DoCmd.RunSQL command line like so and comment out the DoCmd command:

Expand|Select|Wrap|Line Numbers
  1.  Me!txtMySQL=MyInsert & MyValues 
  2. 'DoCmd.RunSQL MyInsert & MyValues
  3.  

Incidentally it would be useful for you to research the difference on here between the BANG and DOT operators on here ( ie: Me! and Me. )for your own information and to understand the difference

Hope this helps you

Regards

Jim :)

4 1572
Jim Doherty
897 Expert 512MB
I am a complete noob with Access, and have been struggling along for a while and now reached a point where I am just stuck. I had made a custom form where some of it populates through other SQL queries and some of the information gets entered in by hand/keyboard. Now I am at the point where I want to save the data in the form to a table when the user clicks the button and I am having some syntax errors.

The first way I tried is like this...
Expand|Select|Wrap|Line Numbers
  1. Private Sub savequote_Click()
  2.  
  3. SQLTEXTINFO = "INSERT INTO Quote_Info (QuoteNumber, thisdate, CustomerPO, SalesPersonName, Discount, DealerNumber, Dealer, DealerAddress, DealerCity, DealerState, DealerZip, ContactName, ContactPhone, ContactFax, ContactEmail, TotalSkids, TotalWeight, Tozip, QuoteTotal, FreightCost, YFClass, YFZip, YFweight) VALUES Me.Quote_number, Me.Quote_date, Me.Dealer_po, Me.Quote.salesperson, Me.Dealer_pricing, Me.Dealer_id, Me.Dealer_name, Me.Dealer_address, Me.Dealer_city, Me.Dealer_state, Me.Dealer_zip, Me.Dealer_contactname, Me.Dealer_contactphone, Me.Dealer_contactfax, Me.Dealer_contactemail, Me.Total_skids, Me.Total_weight, Me.Zip_ship, Me.Total_cartcost, Me.Freight_cost, Me.YF_class, Me.YF_zip, Me.YF_weight;"
  4.  
  5. DoCmd.RunSQL SQLTEXTINFO
  6.  
  7. End Sub
  8.  
And attempting to run the above code gives a syntax error. I made a slight change to the code, by putting ( ) around the VALUES like the following and a little pop up box will begin to ask me for all the values for every item listed in the VALUES (...) part of the code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub savequote_Click()
  2.  
  3. SQLTEXTINFO = "INSERT INTO Quote_Info (QuoteNumber, thisdate, CustomerPO, SalesPersonName, Discount, DealerNumber, Dealer, DealerAddress, DealerCity, DealerState, DealerZip, ContactName, ContactPhone, ContactFax, ContactEmail, TotalSkids, TotalWeight, Tozip, QuoteTotal, FreightCost, YFClass, YFZip, YFweight) VALUES (Me.Quote_number, Me.Quote_date, Me.Dealer_po, Me.Quote.salesperson, Me.Dealer_pricing, Me.Dealer_id, Me.Dealer_name, Me.Dealer_address, Me.Dealer_city, Me.Dealer_state, Me.Dealer_zip, Me.Dealer_contactname, Me.Dealer_contactphone, Me.Dealer_contactfax, Me.Dealer_contactemail, Me.Total_skids, Me.Total_weight, Me.Zip_ship, Me.Total_cartcost, Me.Freight_cost, Me.YF_class, Me.YF_zip, Me.YF_weight);"
  4.  
  5. DoCmd.RunSQL SQLTEXTINFO
  6.  
  7. End Sub
  8.  
So I am at a little lost on how to handle this situation. I don't even know if this is the best method to insert new data into the table, or if I am able to use Me.name variables in this manner even though all the Me.name variables all get assigned a value earlier in the form. Any help or direction would be greatly appreciated.

Thank You.
Hi Lumpy,

Given you are a newbie you have at least discovered the VBA window then :)

You are on the right track just needs tweaking thats all.

There are two methods for entering data into the table one is via the .AddNew method of an Recordset object there are lots or references on how to do that using that way on here so I won't reinvent the wheel on that one you can quite easily research that on here.

Your post uses the DoCmd.RunSQL command to do what that command is designed to do within Access, namely to run an 'Action' query using the SQL language provided by you and taken as part of its argument. In so providing that SQL you provide a 'string' of characters on which the command bases itself.

So what is the string? the string is a continuous sequence of characters taken together to form the argument. Somewhat obvious you might think, but not to some of the people some of the time.

In your case you need to build that string from a combination of literal characters and values provided by the contents of screen controls. The way you have it at the moment is NOT doing that it is saying this:

"I want to insert the string description 'Me.Quote_number' into the quote number field of the table" understand the difference? you are NOT saying take the value from this control and pass that value to the string.

So in building the string for the insert SQL as you have it there, you need to amend and build it along these lines (I break it into parts for reasons of clarity and demonstrating how you string build hopefully rather than anything else)

Expand|Select|Wrap|Line Numbers
  1.  Dim MyInsert AS String 
  2. Dim MyValues as String
  3.  
  4. MyInsert ="INSERT INTO Quote_Info (QuoteNumber, thisdate, CustomerPO, SalesPersonName, Discount, DealerNumber, Dealer, DealerAddress, DealerCity, DealerState, DealerZip, ContactName, ContactPhone, ContactFax, ContactEmail, TotalSkids, TotalWeight, Tozip, QuoteTotal, FreightCost, YFClass, YFZip, YFweight) "
  5.  

Expand|Select|Wrap|Line Numbers
  1.  MyValues= "VALUES (" 
  2.  
  3. MyValues = MyValues & " ' " & Me!QuoteNumber &" ', "
  4. MyValues = MyValues & " # " & Me!QuoteDate &" #, " 
  5. MyValues = MyValues & " ' " & Me!Dealer_po &" ', "
  6.  
You keep on doing this till you exhaust your values list as it applies to your list of insert fields. What are these bits either side of my control names? I hear you ask. They are delimiting characters that are telling the SQL what the data type is that is being passed # denotes a date value for instance.

Now the next effect of this is that the string variable MyValues is increasing in length each time you append unto itself so that when you get to the DoCmd.RunSQL command the actual string you are passing to the command is a joined combination of the variables MyInsert and MyValues.......... like this:

Expand|Select|Wrap|Line Numbers
  1.  DoCmd.RunSQL MyInsert & MyValues
That is the fundamental idea basically! If you want to see the value of these variables 'from your screen' instead of running the command then place and unbound textbox on your screen and call it txtMySQL then place this command

Expand|Select|Wrap|Line Numbers
  1. Me!txtMySQL=MyInsert & MyValues
immediately before the DoCmd.RunSQL command line like so and comment out the DoCmd command:

Expand|Select|Wrap|Line Numbers
  1.  Me!txtMySQL=MyInsert & MyValues 
  2. 'DoCmd.RunSQL MyInsert & MyValues
  3.  

Incidentally it would be useful for you to research the difference on here between the BANG and DOT operators on here ( ie: Me! and Me. )for your own information and to understand the difference

Hope this helps you

Regards

Jim :)
Oct 22 '07 #2
Lumpy
69
Thank You Jim for all the information. That was exactly the direction I was looking for. It helped me to not only get my INSERT statement working but also provided me with additional insight on how VBA works. Thanks again!
Oct 23 '07 #3
Jim Doherty
897 Expert 512MB
Thank You Jim for all the information. That was exactly the direction I was looking for. It helped me to not only get my INSERT statement working but also provided me with additional insight on how VBA works. Thanks again!
You're very welcome I'm glad it helped you

Jim :)
Oct 23 '07 #4
NeoPa
32,556 Expert Mod 16PB
This is all good learning Lumpy, but the way Access is designed to allow you to add data into tables from a form is via a bound form with bound controls. It is possible (as you've found out) to add data from a form programatically and using SQL, but this simply requires a lot of programming to replace what Access is already designed to do for you.
Oct 23 '09 #5

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

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
7
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...
15
by: chirs | last post by:
I am trying to understand a piece of code. In a javascrpit file, there is a function: function ItemStyle(){ var names=; addProps(this,arguments,names,true); }; In the html file, it calls...
0
by: Berthold Höllmann | last post by:
I have a default coding header # -*- coding: iso-8859-15 -*- in my python files. I now have Problems with this settings. I swithched to Python 2.4.1 under Windows. When I import files with the...
2
by: Daniel | last post by:
how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo"
9
by: Tin Gherdanarra | last post by:
Hallo, I'm trying to install pypgsql. However, I get syntax errors while compiling the C sources. The following excerpt from pgconnection.h looks a little funny to me: typedef struct {...
1
by: Hari Sekhon | last post by:
I've written an except hook into a script as shown below which works well for the most part and catches exceptions. import sys def myexcepthook(type,value,tb): do something ...
3
by: Maciek | last post by:
All, I'm having a problem building a library (VMime) with Visual .NET 2003. It's including, among others, gnutls.h from the GNU TLS library and I'm getting syntax errors in that file. For...
7
by: Josh | last post by:
I have a lot of except Exception, e statements in my code, which poses some problems. One of the biggest is whenever I refactor even the triviallest thing in my code. I would like python to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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,...

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.