Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to store a query result in a variable?

Posted on 2008-10-12
16
Medium Priority
?
1,181 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 93

Accepted Solution

by:
Patrick Matthews earned 336 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 332 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 332 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

927 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