Solved

How to store a query result in a variable?

Posted on 2008-10-12
16
1,176 Views
Last Modified: 2013-11-27
Hi,

I have created a query which retuarns a single string value in result. I want to store this resulted string value in a variable for further comparison with other variables. Please help me with syntax.

Thanks
Praveen Parmar
0
Comment
Question by:parmarparveen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +1
16 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 84 total points
ID: 22697151
Hello parmarparveen,

Dim rs As DAO.Recordset
Dim SomeString As String

Set rs = CurrentDb.OpenRecordset("NameOfQuery")
SomeString = rs![NameOfField]  'or SomeString = rs.Fields(0).Value, where the column indexing starts at 0
rs.Close
Set rs = Nothing

Regards,

Patrick
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 83 total points
ID: 22698078
Another approach:

Dim sStr As String
sStr = DLookup("[YourQueryFieldName]","[YourQueryName]")

'comparison code here

mx

0
 
LVL 44

Expert Comment

by:GRayL
ID: 22699039
Just an observation, Patrick, Joe.  Would this use less resources?

SomeString = CurrentDb.OpenRecordSet("qryName")!fldName

and is it as faster or slower than the DLookup()
0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 75
ID: 22699058
Well, how about running a test in a loop of 100K times to see ?

mx

0
 
LVL 44

Expert Comment

by:GRayL
ID: 22699091
Did that already.  You lose by a factor of 2:1 ;-)
0
 
LVL 75
ID: 22699262
Cool.  That's a good trick!

What about MP's scheme, similar to yours but with the extra line of code.  Run that.

mx
0
 
LVL 44

Assisted Solution

by:GRayL
GRayL earned 83 total points
ID: 22700160
Here was my test.  Draw your own conclusions.

ts=timer: for n = 1 to 1000: id = dlookup("drid","doctors"): next n:? timer-ts
 4.766968
ts=timer: for n = 1 to 1000: id = currentdb.OpenRecordset("doctors")!drid: next n:? timer-ts
 2.432983

0
 

Author Comment

by:parmarparveen
ID: 22700272
Thanks
0
 
LVL 75
ID: 22700595
Public Function mCheckTime()
    Dim x As Long, n As Long, ID
    Dim ts
    x = 50000
    ts = Timer: For n = 1 To x: ID = DLookup("FIELD1", "query36"): Next n
    'ts = Timer: For n = 1 To x: ID = CurrentDb.OpenRecordset("query36")!FIELD1: Next n
    Debug.Print Timer - ts
End Function

DLookup:               71.59399
Open Recordset:   64.45398

mx
0
 
LVL 44

Expert Comment

by:GRayL
ID: 22706346
parmarparveen:  What didn't you like about the answer such that you rewarded a B with absolutely no feedback?  A  share would have been in order as both mx and mathewspatrick provided two ways of getting the variable, and I provided a variation on the theme, along with some test methodology and test results.  Mx went the extra mile and gave you his test methodology and results.  I am requesting you ask Community Support to re-open the question so it may be closed properly.  You should never close a question in that manner without giving the participants to correct any shortcoming you have observed.  
0
 
LVL 44

Expert Comment

by:GRayL
ID: 22723186
0
 
LVL 75
ID: 22738341
oops ... parmarparveen ... gRay meant that you should split the points between the 3 of us, and really gRay had the most efficient solution.  Can you once again Request Attention and fix this?

thx.mx
0
 

Author Closing Comment

by:parmarparveen
ID: 31505430
Thanks
0
 
LVL 75
ID: 22738361
thank you.

mx
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question