Solved

How to store a query result in a variable?

Posted on 2008-10-12
16
1,172 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
  • 6
  • 5
  • 2
  • +1
16 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 84 total points
Comment Utility
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 - Access MVP)
DatabaseMX (Joe Anderson - Access MVP) earned 83 total points
Comment Utility
Another approach:

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

'comparison code here

mx

0
 
LVL 44

Expert Comment

by:GRayL
Comment Utility
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
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
Well, how about running a test in a loop of 100K times to see ?

mx

0
 
LVL 44

Expert Comment

by:GRayL
Comment Utility
Did that already.  You lose by a factor of 2:1 ;-)
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:parmarparveen
Comment Utility
Thanks
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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
Comment Utility
Thanks
0
 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
thank you.

mx
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now