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

Saving the result of a query in a variable

31
I have a query whose result is a single value like sum of cost's column. Can I save the result in a string or a double variable. I tried using recordset. The code I used is as follows:

Expand|Select|Wrap|Line Numbers
  1. Sub CreateRecordset2()
  2.  
  3.     Dim rst As New ADODB.Recordset
  4.     Dim str As String
  5.     Dim str1 As String
  6.     Dim intgr As Double
  7.  
  8.     str1 = "'*' & 'drilling' & '*'"
  9.  
  10. rst.Open "SELECT Sum([QUESTOR Run tbl].[cost]) FROM [Project info Tbl] INNER JOIN [QUESTOR Run tbl] ON [Project info Tbl].[Project ID] = [QUESTOR Run tbl].[Project ID] WHERE ((([Project info Tbl].[Offshore only])=Yes) AND (([QUESTOR Run tbl].[QUE$TOR Version Name])='7.3') AND (([QUESTOR Run tbl].[Cost Tab]) Like " & str1 & "))", CurrentProject.Connection
  11.  
  12.     str = rst.GetString
  13.     'intgr = CDbl(str)
  14.  
  15.     MsgBox str
  16.  
  17.     'Debug.Print rst.GetString
  18.  
  19.     rst.Close
  20.  
  21.     Set rst = Nothing
  22.  
  23. End Sub
The message box is not giving any value. I think the problem is in the "Like" clause. Can somebody please help me with this. I am getting an answer if I am omitting the "Like" clause.

Thank you.
Jul 12 '07 #1
6 7353
Flo100
31
I also tried the following code:

Expand|Select|Wrap|Line Numbers
  1. Sub CreateRecordset2()
  2.  
  3.     Dim rst As New ADODB.Recordset
  4.     Dim str As String
  5.  
  6.     str = "SELECT Sum([QUESTOR Run tbl].[cost]) FROM [Project info Tbl] INNER JOIN [QUESTOR Run tbl] ON [Project info Tbl].[Project ID] = [QUESTOR Run tbl].[Project ID] WHERE ((([Project info Tbl].[Offshore only])=Yes) AND (([QUESTOR Run tbl].[QUE$TOR Version Name])='7.3') AND (([QUESTOR Run tbl].[Cost Tab]) Like '*' & 'drilling' & '*'))"
  7.  
  8.     rst.Open str, CurrentProject.Connection
  9.  
  10.     str = rst.GetString
  11.  
  12.     MsgBox str
  13.  
  14.     rst.Close
  15.  
  16.     Set rst = Nothing
  17.  
  18. End Sub
The message box is empty. When I copy the string in the variable "str" into a new query and try to get the result it gives me result in the datasheet view. Why am i not able to get an answer? The same stratedy works fine for queries like "SELECT * from [Project info tbl]. The message box displys all the records in that table. This strategy is also working for queries that have a "like" clause with exact string to match that is a "like" clause without * at the beginning and end. This is not working only for * in the like Clause. Can somebody please help.

Thank you.
Jul 12 '07 #2
JKing
1,206 Expert 1GB
Here's a little something from microsoft
The Jet Database Engine is used to work with the data in an Access (.mdb) database and supports the ANSI SQL-89 standard. However, data access pages in Access databases connect to Jet by using ActiveX Data Objects (ADO) and the Jet OLEDB provider. ADO and OLEDB always use ANSI SQL-92 syntax. Thus, data access pages must use ANSI SQL-92 syntax. This means that a form and a page bound to the same query can return different result data sets.
Here's the link as well ADODB Wildcards

So I think the solution here is:

Expand|Select|Wrap|Line Numbers
  1. str = "SELECT Sum([QUESTOR Run tbl].[cost]) FROM [Project info Tbl] INNER JOIN [QUESTOR Run tbl] ON [Project info Tbl].[Project ID] = [QUESTOR Run tbl].[Project ID] WHERE ((([Project info Tbl].[Offshore only])=Yes) AND (([QUESTOR Run tbl].[QUE$TOR Version Name])='7.3') AND (([QUESTOR Run tbl].[Cost Tab]) Like '%drilling%'))"
  2.  
  3. rst.Open str, CurrentProject.Connection
  4.  
Jul 12 '07 #3
JKing
1,206 Expert 1GB
I'm making the assumption that you're using 2002,2003 or later.
Jul 12 '07 #4
Flo100
31
Here's a little something from microsoft

Here's the link as well ADODB Wildcards

So I think the solution here is:

Expand|Select|Wrap|Line Numbers
  1. str = "SELECT Sum([QUESTOR Run tbl].[cost]) FROM [Project info Tbl] INNER JOIN [QUESTOR Run tbl] ON [Project info Tbl].[Project ID] = [QUESTOR Run tbl].[Project ID] WHERE ((([Project info Tbl].[Offshore only])=Yes) AND (([QUESTOR Run tbl].[QUE$TOR Version Name])='7.3') AND (([QUESTOR Run tbl].[Cost Tab]) Like '%drilling%'))"
  2.  
  3. rst.Open str, CurrentProject.Connection
  4.  

Thank you so much. Its working........ hehe....I am happy...happy..happy
Jul 12 '07 #5
JKing
1,206 Expert 1GB
You're very welcome! Glad it worked for you.

Jking
Jul 12 '07 #6
NeoPa
32,556 Expert Mod 16PB
I saw this and it reminded me that I had planned to put together an article on the differences between the ANSI-89 & ANSI-92 standards. It also includes instructions on how to use pattern matching to select the data you want (ANSI Standards in String Comparisons).
I hope you find it useful :)
Jul 13 '07 #7

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

Similar topics

3
by: Shawn Wilson | last post by:
Hi, I've created a PHP file called filename.jpg. It outputs a JPG. I've set up a .htaccess file to force the filetype to PHP. The file is displayed in a page on a browser. I cannot get the...
2
by: shank | last post by:
I have a search page with 7 different field options to search upon. The user can use any or all options. On the results page I'd like to put at the top of each column and ASC and DESC link to...
1
by: Randy K | last post by:
I have a table with some 35000 records and I need some help sorting it out. The goal is to get counts of failures modes oraganized by serial number. the table is set up roughly like this. s/n ...
4
by: Jim via DotNetMonster.com | last post by:
Hi, How can I assign the result of a function to a variable. I need to get the result so that I can query the database again. What I'm trying is:...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
6
by: Busbait | last post by:
I need to know how can I store SQL result into variable, I have used the below code but it dose not work. Is there an easier way to do this? ...
4
by: pkj7461 | last post by:
Hi, I was using Docmd.Transferspreadsheet to to populate query data in Excel. My code so far Dim xlApp As Excel.Application Dim xlWb As Excel.workbook Dim xlWs As Excel.Worksheet Set x1App =...
0
by: FLANDERS | last post by:
Hi all, Is it possible to declare a SQL type of result set or similar? I want to do use the IN predicate like you can in a non-procedural SQL like this: UPDATE TABLE1 SET COL1 = 123 WHERE COL2 IN...
0
by: plonk | last post by:
Hi I have the following code which saves the result from a query into an array. I want to be able to do some mathematic functions on the data, but can't seem to figure out how to get it into...
13
by: Janine de Lange | last post by:
Hi guys. I’m not familiar with Ms access VB (or any other for that matter)but learning fast. I have tried various methods to achieve the outcome but all have failed. Background Front End...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
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...

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.