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

What is the syntax for an update table field to a variable based on another variable

stonward
145 100+
Greetings All.

I am trying to find the syntax for an SQL Update Table instruction. They're quite straightforward it seems until you try to use variables as criteria.

So, I want to update the Diameters field of my Products Table to the Diameter value on my current form, only updating the record of the ProductID from my current form.

Expand|Select|Wrap|Line Numbers
  1. "UPDATE tblProducts SET Diameter =" & intDiameter
will naturally update all the diameters to the same value. How do I add the criteria of my ProductID?

Thanks again,


Stonward
Feb 6 '13 #1

✓ answered by Seth Schrock

None. The variable name means nothing. I just didn't see that you were assigning the SQL code to a variable.

Here is an example of the difference I was trying to make. Instead of writing
Expand|Select|Wrap|Line Numbers
  1. Dim db as DAO.Database
  2. Set db = CurrentDb
  3.  
  4. Db.Execute "UPDATE tblProducts SET Diameter = " & intDiameter & _ 
  5.                         " WHERE ProductID = " & My_Value, dbFailOnError
I would write
Expand|Select|Wrap|Line Numbers
  1. Dim strQuery as string
  2. Dim db as DAO.Database
  3. set db = CurrentDb
  4.  
  5. strQuery = "UPDATE tblProducts SET Diameter = " & intDiameter & _
  6.            " WHERE ProductID = " & My_Value
  7.  
  8. db.Execute strQuery, dbFailOnError
Even if you are using a different method to run your query, the same logic applies. If you were already assigning the SQL code to a variable, then all you have to do is concatenate the WHERE clause onto the end of your string (don't forget to make sure that there is a space before the WHERE).

8 10165
Seth Schrock
2,965 Expert 2GB
What I find easiest to do is to make a string varible to assign the query to and then run the query variable. So something like:

Expand|Select|Wrap|Line Numbers
  1. Dim strQuery as String
  2.  
  3. strQuery = "UPDATE tblProducts SET Diameter = " & intDiameter & _
  4.            " WHERE ProductID = My_Value"
Your WHERE clause can also use a variable like
Expand|Select|Wrap|Line Numbers
  1. " WHERE ProductID = " & My_Value
You can then execute your query using strQuery instead of the actual SQL code.
Feb 6 '13 #2
stonward
145 100+
Hi Seth,

I can understand using the query window to get the SQL and then convert to VBA format (tried it!), but I don't quite get what it is you're doing in your example. What is the difference between strQuery, your variable and, say, strSQL as a string variable for the SQL?

Stonward
Feb 6 '13 #3
Seth Schrock
2,965 Expert 2GB
None. The variable name means nothing. I just didn't see that you were assigning the SQL code to a variable.

Here is an example of the difference I was trying to make. Instead of writing
Expand|Select|Wrap|Line Numbers
  1. Dim db as DAO.Database
  2. Set db = CurrentDb
  3.  
  4. Db.Execute "UPDATE tblProducts SET Diameter = " & intDiameter & _ 
  5.                         " WHERE ProductID = " & My_Value, dbFailOnError
I would write
Expand|Select|Wrap|Line Numbers
  1. Dim strQuery as string
  2. Dim db as DAO.Database
  3. set db = CurrentDb
  4.  
  5. strQuery = "UPDATE tblProducts SET Diameter = " & intDiameter & _
  6.            " WHERE ProductID = " & My_Value
  7.  
  8. db.Execute strQuery, dbFailOnError
Even if you are using a different method to run your query, the same logic applies. If you were already assigning the SQL code to a variable, then all you have to do is concatenate the WHERE clause onto the end of your string (don't forget to make sure that there is a space before the WHERE).
Feb 6 '13 #4
stonward
145 100+
hi Seth,

Now I didn't know about the space! I saw it in an example just before I read it in your answer. And guess what, it makes a difference!

Thanks yet again.

Stonward
Feb 6 '13 #5
Seth Schrock
2,965 Expert 2GB
Something that can help you in the future when you are having issues to SQL strings (or any variable for that matter), you can put a Debug.Print Variable_Name right after you assign the variable a value and then run your code. It will put the completed string (what the Jet SQL engine sees) in the immediate window (Ctrl + G to open it). This often helps to make sure you have your SQL correct. You can even copy and paste it into a querydef and see if it runs. However, I think that in this case you would have noticed that there wasn't the space that was needed.

Just a tip that can help you down the road.
Feb 6 '13 #6
NeoPa
32,556 Expert Mod 16PB
Seth:
I would write
Expand|Select|Wrap|Line Numbers
  1. ...
  2. CurrentDb.Execute strQuery, dbFailOnError
Please don't Seth. There are many problems associated with treating CurrentDb() as a variable instead of the function that it is. Each calling of that function can return a different object. Each object refers to your current database to be sure, but that is not the same as being the same object. You can do a lot worse than get into the habit (as I have now) of using a DAO.Database variable for every reference you have in code to your database.
Feb 7 '13 #7
Seth Schrock
2,965 Expert 2GB
Oops. I declared the variable db, but didn't use it. I have gotten into the habit of using db instead of CurrentDb, but my first thought is still to use CurrentDb. I just usually catch myself.

Thank-you for pointing this out. I will edit my previous post so that future viewers see the correct (and what I meant to put) code.
Feb 7 '13 #8
NeoPa
32,556 Expert Mod 16PB
Good for you. Especially impressive that you edited the original post. I had considered requesting that you do for that very reason but it seems you're getting ahead of the game. I can feel a PM coming on in the next few days. Must rush now, but more details then ;-) (I know. Cruel to leave it hanging like that :-D Look out for a PM by the end of the week.)
Feb 7 '13 #9

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

Similar topics

4
by: Onion | last post by:
This has to be simple, but I'm forced to admit that I'm a novice who can't figure it out. I have a listbox in a form that allows multiple selections. That works fine. The problem: I can't...
3
by: Nmishra | last post by:
Hi, I have a problem in assigning a name to a pointer dynamically from another pointer. e.g. if I have char* name = "myPtr"; and I want to have a int pointer having the name as myPtr...
1
by: name | last post by:
Tricky I'm trying to do the following string sString = "Here is a test string"; string sFieldRef = "sString"; MessageBox.Show(sFieldRef); //How do I get this line to display: Here is a...
1
by: Bo Long | last post by:
I believe the following a valid SQL statement, but MS Access returns with an error "Operation must be an updateable query". Any suggestions would be greatly appreciated! UPDATE FERCPTILoad AS...
2
by: Umoja | last post by:
Hi Guys, This is a follow up question from my previous post which Nico5038 help me solved. I have a tblMain which contains (ID-Autonumber, Acct, Number, User). TblMain currently does not have...
4
by: Matus1976 | last post by:
Hi, I have a form which updates my table just fine except for some fields in the form are expressions, and the results of the expressions do not populate the table, but the form always remembers the...
2
by: DeanL | last post by:
Hi guys, I'm using a subform to display a list of values from a single table. The form is showing the values in "continuous forms" view and there will only ever be two different values for the...
2
by: selvialagar | last post by:
Let me tell my problem.There are list of parameters in a list box. User can select any number of Parameters (Maximum Limit is 10). Now I want to create dynamic arrays for all the parameters....
1
by: arsha123 | last post by:
How can I change the value of empty session variable with another variable value defined in form. I get value of session value of id from a logon form , that is "". I have defined a variable in form...
0
by: Rod Faulk | last post by:
Hi! I have a problem almost exactly like this one: http://bytes.com/topic/access/answers/870079-updating-pivot-table-page-fields-based-another-page-field Nevermind.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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,...

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.