473,414 Members | 1,947 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,414 software developers and data experts.

Run-Time Error with SQL Update in VBA

*Edit: Split from thread http://bytes.com/topic/access/answer...query-into-vba*

Thank you very much as well TheSmileyCoder i have followed you instructions to keep it all to best practice.
IOne more question if its ok, I have updated the value of x to be an Integer, this works ok but i have put a update sql into the vba to update the table based on the value of x. All the data in the table is a number/integer but i get a error saying. "Run-time error '3464' Data type mismatch in criteria expression" the sql statement is
Expand|Select|Wrap|Line Numbers
  1. db.Execute "UPDATE Location SET Location.ID = 0 WHERE Location.ID =' & x & '"
If i change the value of x to 1 it works fine so why wont it work with the value from the vba/sql result?
Thanks
Jul 6 '12 #1
9 2611
zmbd
5,501 Expert Mod 4TB
If X is type integer now then you should remove the quotes from around the X... otherwise SQL thinks it's a string.

Personal choice... I tend to build my SQL string first then call the execute. This way I can do a debug.print sSQL incase I need to check how it's formed.
Expand|Select|Wrap|Line Numbers
  1. Dim iX as integer
  2. Dim sSQL as string
  3. sSQL= "UPDATE Location SET Location.ID = 0 WHERE Location.ID = " & iX
  4. db.Execute sSQL
-z
Jul 6 '12 #2
I presume the " should go at the end of the SQL update statement? and not like
Expand|Select|Wrap|Line Numbers
  1. Location.ID = " & x
because that would mean the x is not part of the SQL. Also even after removing the quotes it still wont work i have tried it both ways if i do it this way
Expand|Select|Wrap|Line Numbers
  1. db.Execute "UPDATE Location SET Location.ID = 0 WHERE Location.ID = & x"
i get a error saying "syntax error (missing operator) in query expression 'Location.ID = & x'.
if i do it like
Expand|Select|Wrap|Line Numbers
  1. db.Execute "UPDATE Location SET Location.ID = 0 WHERE Location.ID =" & x
it wont give an error but wont work because it looks like the value of x is not part of the SQL
Thanks for any reply in advance
Jul 6 '12 #3
zmbd
5,501 Expert Mod 4TB
As you haven't started your new post yet:

No, the example is as is... your iX has the integer value assigned to it from earlier in your code.

The following is an example taken from part of a code I use to update a particular record in an actual database that I use in production to track 100's of containers:

The code is in a form (frm_qaqc_updatestate) which is opened from another form that deals with the actual container information (note that is is only a few lines out of many as there is validation and signiture and on and on and on involved with updating a container's status).

I've placed the subparts of the string on seperate lines so you can see how they relate:
Expand|Select|Wrap|Line Numbers
  1. zsqlstring = 
  2. "UPDATE
  3. tbl_tmp_qaqchold
  4. SET
  5. qaqcstat = '" & z_str_cbobx_qaqcstat & "' 
  6. WHERE 
  7. ContainerRecord_id_pk = " & z_int_addtolist
  8. zdb.Execute zsqlstring, dbFailOnError
now normally zsqlstring would be all on one line.

On line 5: "qaqcstat" is text field in the tbl_tmp_qaqchold, that will be set to a string value taken from a form control - notice the single quotes " ' " - if it were a date then the " ' " would be replaced by a pound sign "#" an integer would be as on....

.... On line 7: ContainerRecord_id_pk is a field in tbl_tmp_qaqchold, it is a long integer (actually an autonumber field used to track the containers but that's another story). Notice how z_int_addtolist just hangs there? z_int_addtolist was set earlier in the form to be the integer id for the container.

SO let's say the lab is updateing the qaqcstatus for container 23 to hold:
z_int_addtolist = 23
z_str_cbobx_qaqcstat = hold

so if you were to insert DEBUG.PRINT zsqlstring between lines 7 and 8 then in the immedate window you would get:
Expand|Select|Wrap|Line Numbers
  1. UPDATE tbl_tmp_qaqchold SET qaqcstat = 'Hold' WHERE ContainerRecord_id_pk = 23
There you have it.
-z
Jul 7 '12 #4
ZMBD I must say a very big thank you for all of your help. I never did post the new post as it was a school boy error by myself by using the wrong values in the SQL statement. The last comment is very detailed and very informative thanks. But i do have an issue how to i update two values in a SQL update the code is below
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "UPDATE [TableToUpdate]SET [ProdID]=800 , [TimeStamp]=#" & Format(Date, "yyyy-mm-dd") & "# WHERE [ProdID] = 0 AND [ID]=(SELECT MIN(ID)AS low FROM [TableToUpdate] WHERE [ProdID] = 0);"
I took notice of you above post and the date works fine but what i am trying to change is where the value is 800 for the ProdID update this needs to be the Int value of L which is declared further up in my code. I have tried
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunSQL "UPDATE [TableToUpdate]SET [ProdID]=" & L , [TimeStamp]=#" & Format(Date, "yyyy-mm-dd") & "# WHERE [ProdID] = 0 AND [ID]=(SELECT MIN(ID)AS low FROM [TableToUpdate] WHERE [ProdID] = 0);"
at it gives my an error which highlights the first # tag from the code and says compile error expected expresion. No doubt this is one character im missing any help would be much apprcieated.
Jul 9 '12 #5
zmbd
5,501 Expert Mod 4TB
say.. just to let you know I did see this; however, I'm on my way "out-the-door" so to speak... I may not get a chance to take a careful look until a little later tonight or tomorrow.

If someone else can help before I can take a look, please feel free to jump-in the water... it's nice and cool! (it's like 95°F outside... looking forward to the pool at home!)

With a VERY quick look at your second code; try the following:
Expand|Select|Wrap|Line Numbers
  1. sSQL ="UPDATE [TableToUpdate]SET [ProdID]=" & L , [TimeStamp]= "#" & Format(Date, "yyyy-mm-dd") & "#" WHERE [ProdID]  = 0 AND [ID]=(SELECT MIN(ID) AS low FROM [TableToUpdate] WHERE [ProdID] = 0);" 
  2. DoCmd.RunSQL sSQL
Looks like there were a few missing quote marks and a missing space or two... SQL engines are very touchy about such things in the parsing.




-z
Jul 9 '12 #6
i sure wish i was where ever you where as always wet and windy in the wonderful UK. i copied and pasted your code and i get the error compile error, Expected, end of statement by the , after the L
the code is as follows
Expand|Select|Wrap|Line Numbers
  1. sSQL = "UPDATE [TableToUpdate]SET [ProdID]=" & L , [TimeStamp]= "#" & Format(Date, "yyyy-mm-dd") & "#" WHERE [ProdID]  = 0 AND [ID]=(SELECT MIN(ID) AS low FROM [TableToUpdate] WHERE [ProdID] = 0);"
Jul 9 '12 #7
zmbd
5,501 Expert Mod 4TB
Looks like there is a space between "L" and the comma and a missing "(" As I said, those SQL engines are very much the stickler for their p's and q's!

Sorry, feeling well under the weather today and somewhat yesterday too... please try:

Expand|Select|Wrap|Line Numbers
  1. sSQL = "UPDATE [TableToUpdate] SET [ProdID]=" & L & ", [TimeStamp]=#" & Format(Date, "yyyy-mm-dd") & "# WHERE [ProdID] = 0 AND [ID]= (SELECT MIN(ID) AS low FROM [TableToUpdate] WHERE [ProdID]= 0);"
Jul 10 '12 #8
:D Thank you every so much for the help, and you really souldnt appologise your helping me out. That worked a treat you deserve a well earned rest. Thanks again
R
Jul 10 '12 #9
zmbd
5,501 Expert Mod 4TB
I'm glad I could help...
-z

[EDIT] 2012-07-11:2125CST - asked mods to split this thread starting at post 5 per OP's post in #8 - Z[/EDIT]
Jul 10 '12 #10

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

Similar topics

4
by: Sheryl Reed | last post by:
Hi, I am using this browser that came with an sbcglobal account and when I click on the built in "shopping" button (and others), sometimes i get a jscript runtime error. It happens often but not...
3
by: colm | last post by:
i get the above runtime error on the following line of code when i try to update a reord in my form when it gets to the line Set rs = DBEngine(0)(0).OpenRecordset(strSql) the entire code...
3
by: bill_hounslow | last post by:
I'm trying to transfer data from one Sql Server into a table on another, using a simple INSERT query in an Access database with links to tables on both servers (the reasons for this are complicated...
1
by: c_del29 | last post by:
I am getting this message every time that I open an e-mail in Outlook or hotmail. Microsoft Visual C++ Runtime Error! Program: c:\Program Files\Internet Explorer\IEXPLORE.EXE This application...
6
by: mark | last post by:
I have an asp.net ecommerce web application on a remote web server. I'm using an Access database on the back end. I've notice a few strange things. When I mimic an multiple user environment by...
3
by: Alphonse Giambrone | last post by:
I have been using the IE Treeview for several months and it has been working fine until recently. Now on my development machine I get a runtime error when the page loads or the mouse moves over...
8
by: g_man | last post by:
I am trying trap Runtime error 3022 (duplicates) in the click event of a command button that closes the form. I have code in the Form_Error event that does a good job of providing a more meaningful...
5
by: shivaniem | last post by:
Please help me on this. I want ti add detatils in my database through VB6. when i rumn the program i say add enter the details and say save. when i say save an error occurs saying Runtime...
3
by: Peachstone | last post by:
I'm using Windows XP and MS Access 2002. I am trying to write code in VB which is attached to a text control on a form called "Scripture1". I have another text control on the same form called...
1
by: Lauren Dobson | last post by:
This database was working days ago, outputting a vocabulary list into a word document. Any idea why I'm getting runtime error 3010? I just switched from Windows XP to Windows 7 but I'm still using...
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
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.