Looking For Advice!!

N

NEWER USER

Which is the CORRECT way to write Code?

Dim strDoc as String
strDoc = "qryTEST
DoCmd.OpenQuery strDoc, acNormal, acEdit

OR

DoCmd.OpenQuery "qryTEST", acNormal, acEdit

Does one run faster than the other? Are both ways acceptable or could
problems arise down the road? If several queries are run back to back,
should I define (Dim) or use the second method above. Any thought
appreciated.
 
S

Stuart McCall

NEWER USER said:
Which is the CORRECT way to write Code?

Dim strDoc as String
strDoc = "qryTEST
DoCmd.OpenQuery strDoc, acNormal, acEdit

OR

DoCmd.OpenQuery "qryTEST", acNormal, acEdit

Does one run faster than the other? Are both ways acceptable or could
problems arise down the road? If several queries are run back to back,
should I define (Dim) or use the second method above. Any thought
appreciated.

The (admittedly awkward) answer is that they're both correct. A general rule
of thumb is if you expect to use the same string of characters more than
once, then your 1st method is the most efficient. If the string is used just
the once, you might as well specify it as a string literal (your 2nd
method).

As far as performance goes, I doubt you could measure the difference between
the two methods, it would be so miniscule.
 
D

Dirk Goldgar

NEWER USER said:
Which is the CORRECT way to write Code?

Dim strDoc as String
strDoc = "qryTEST
DoCmd.OpenQuery strDoc, acNormal, acEdit

OR

DoCmd.OpenQuery "qryTEST", acNormal, acEdit

Does one run faster than the other? Are both ways acceptable or could
problems arise down the road? If several queries are run back to back,
should I define (Dim) or use the second method above. Any thought
appreciated.


There's no significant difference between them. Technically, the second one
is *marginally* more efficient, and that's what I would do if I didn't
anticipate adding any code to choose a different query to be opened. But
the difference in efficiency will never be perceptible to *anyone*, unless
you were running the code an enormous number of times in a tight loop --
which, in the case of this particular code, doesn't make any sense.

If I had a choice of queries to be opened, then I might use a variable to
hold my choice, as in this example:

Dim strQueryName As String

Select Case Me!QueryOption
Case 1 : strQueryName = "qryA"
Case 2 : strQueryName = "qryB"
Case 3 : strQueryName = "qryC"
Case Else: strQueryName = "qryD"
End Select

DoCmd.OpenQuery strQueryName

But for a simple case where I just want to open a foreknown query, I
wouldn't bother using a variable for the query name.
 
T

Tony Toews [MVP]

NEWER USER said:
Which is the CORRECT way to write Code?

Dim strDoc as String
strDoc = "qryTEST
DoCmd.OpenQuery strDoc, acNormal, acEdit

OR

DoCmd.OpenQuery "qryTEST", acNormal, acEdit

Go for the second one. Stuart and Dirk have mentioned some factors.
But, all other factors being negligible, I look at this from next
years viewpoint. The second case will take you a slightly smaller
time to figure out a year from now. Especially if it's part of a
larger module and you have stDoc set lines and lines above the docmd
statement.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Seeking Advice 7
Cleanup Code on Form ?? 3
Resume Next versus GoTo 5
Form Code HELP ?? 4
1st VBA sequence 6
RecordSource Property 2
Use of RecordSet 4
Using multi-select list box data in a Query 1

Top