473,386 Members | 1,819 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.

VBA value into SQL query

Afternoon all
is it possiable to put a value from my VBA code into a SQL statement? my VBA code is as follows
Expand|Select|Wrap|Line Numbers
  1. strLowestRack = "SQLtoFindOldestStockfrmAssignOrders"
  2.  
  3. Stop
  4.  
  5. Set rs = Me.[sqlOrderDetails subform].Form.RecordsetClone
  6.  
  7. Do While Not rs.EOF
  8.  
  9. prodnumber = rs!ProdID
  10. q = rs!Quantity
  11.  
  12.  
  13.     For i = 1 To q
  14.  
  15.         l = DLookup("RackID", strLowestRack) 'change this sql
  16.  
  17.         DoCmd.RunSQL "UPDATE [Location] SET [Location].ProdID = 1 WHERE [Location].RackID =" & l
  18.  
  19.     Next i
  20.  
  21. rs.MoveNext
  22.  
  23. Loop
The value from within this code that is called prodnumber i would like to use in this sql query
Expand|Select|Wrap|Line Numbers
  1. SELECT Location.RackID
  2. FROM Location
  3. WHERE (((Location.ProdID)="prodnumber") 
  4.     AND ((Location.StockAge)=(SELECT MIN(StockAge) 
  5.     FROM Location 
  6.         WHERE Location.ProdID = "prodnumber")));
Where ive put "prodnumber" is actually where i need that value. i have tried [rs]![prodnumber] and [rs]![prodID] with no luck. If someone could point me in the right direction
Thanks very much and for all the previous help i have recieved.
Aug 22 '12 #1

✓ answered by twinnyfo

315Hughes,

Your code would look someting like this:

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL as String
  2.  
  3. strSQL = "SELECT Location.RackID" & _
  4.     " FROM Location" & _
  5.     " WHERE Location.ProdID = " & prodnumber & _
  6.     " AND Location.StockAge = " & _
  7.         " (SELECT MIN(StockAge) FROM Location " & _
  8.         " WHERE Location.ProdID = " & prodnumber & ");" 
  9.  
I've stepped out the code to make it more readable, but it could be on one line. This would have to be within the same sub as above, so that prodnumber is a variable the DB recognizes.

4 1730
TheSmileyCoder
2,322 Expert Mod 2GB
Dlookup takes 3 parameters, where the last one is optional. You can specify a Where clause as the third parameter, allthough I am not sure it would work in your case.

You cannot use [rs] directly in your query since that is a value only available to the VBA environment.

I could offer up some code that would set the querydefinition to a parsed string which would solve your immediate problem, however, I would like if you could explain WHAT you are trying to achieve, as I feel there may be more efficient ways to approach your problem.
Aug 22 '12 #2
twinnyfo
3,653 Expert Mod 2GB
315Hughes,

Your code would look someting like this:

Expand|Select|Wrap|Line Numbers
  1. Dim strSQL as String
  2.  
  3. strSQL = "SELECT Location.RackID" & _
  4.     " FROM Location" & _
  5.     " WHERE Location.ProdID = " & prodnumber & _
  6.     " AND Location.StockAge = " & _
  7.         " (SELECT MIN(StockAge) FROM Location " & _
  8.         " WHERE Location.ProdID = " & prodnumber & ");" 
  9.  
I've stepped out the code to make it more readable, but it could be on one line. This would have to be within the same sub as above, so that prodnumber is a variable the DB recognizes.
Aug 22 '12 #3
@TheSmileyCoder
Hi what i am trying to do is a stock system i use the value from prodnumber to search through a location table to find the oldest stock location number based on that prodnumber value.
The SQL query that does it is the one i posted (also called "strLowestRack" in the vba code). The value that is returned is held in the value "l"(rack number with the oldest stock) which comes from the Dlookup search. The value "l" is then used to update work sheets.
I hope this explains it better
Aug 22 '12 #4
zmbd
5,501 Expert Mod 4TB
315Hughes,
It appears that your query would return the newest stock age not the oldest. (line 7 of TwinnyFo's code)
-z
Aug 22 '12 #5

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

Similar topics

2
by: Mountain Man | last post by:
Hi, I'm having trouble with the foreach function. I'm using it twice inside a user defined function with two different arrays, and in the second instance it's returning the value of the first...
6
by: ubccis | last post by:
Hi. I'm wondering if you can dynamically query a database with javascript and php. For example, can I select something on the select menu and dynamically query the database and populate another...
1
by: gudia | last post by:
Two tables: T1 (c1 int, TestVal numeric(18,2), ResultFactor numeric(18,2))--c1 is the primary key. T2 (x1 int, FromVal numeric(18,2), ToVal numeric(18,2), Factor numeric(18,2))--x1 is the...
10
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty...
3
by: thomas goodwin | last post by:
I have a query which asks for a parameter value to execute it. To see the results I have to: a) click on the query -- the "Enter Parameter Value" window pops up. b) enter the parameter value c)...
1
by: Ed Murphy | last post by:
Query #1: select <list of fields> from C join B on C.b_key = B.b_key join A on B.a_key = A.a_key where A.o_key = <some value> Query #2:
3
by: vstolmech513 | last post by:
Here goes nothing... I'm trying to make a report that shows what guys work on what crew, ie. day crew or night crew. For this selection I've got a form with a combo box with either a "D" or "N"...
4
natalie99
by: natalie99 | last post by:
Hi All :) I am trying to stipulate rules for the return of a pricing value in Access. I know this should be very easy, I simply cannot seem to make my expressions work! The two tables have...
2
by: de_ja | last post by:
Hi, If, for example an access database that concerns insurance claim investigations. The database comprises only one table containing data - investigation reference number, referral date,...
1
by: Michel Walsh | last post by:
In the same spirit, but more LINQ related, you can also use ExecuteQuery: var query = dataContext.ExecuteQuery<className>( @"SELECT ... WHERE ... AND... OR... "); where className is...
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:
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
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: 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
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,...

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.