If/Then useage in a form - what's faster?

M

ManningFan

Is it faster to do something like:

If Forms![formname]![subformname]![control] = "W" or Forms![formname]!
[subformname]![control] = "X" or Forms![formname]![subformname]!
[control] = "Y" or Forms![formname]![subformname]![control] = "Z" Then
... Perform Some Code
Else
Endif

or is it faster do do:

strForm = Forms![formname]![subformname]![control]
If strForm = "W" or strForm = "X" or strForm = "Y" or strForm = "Z"
Then
... Perform Some Code
Else
Endif

I'm trying to figure out which requires less processing, or if they're
both the same. Any help is appreciated!
 
B

Bob Quintal

m:
Is it faster to do something like:

If Forms![formname]![subformname]![control] = "W" or
Forms![formname]! [subformname]![control] = "X" or
Forms![formname]![subformname]! [control] = "Y" or
Forms![formname]![subformname]![control] = "Z" Then
... Perform Some Code
Else
Endif

or is it faster do do:

strForm = Forms![formname]![subformname]![control]
If strForm = "W" or strForm = "X" or strForm = "Y" or strForm =
"Z" Then
... Perform Some Code
Else
Endif

I'm trying to figure out which requires less processing, or if
they're both the same. Any help is appreciated!
The second is definitely faster to debug. It's also probably slightly
faster to execute.
 
J

Jeff Boyce

Are you concerned about absolute processing speed, or perceived response
time? I doubt your users would be able to spot the difference...

By the way, using:
strForm = Forms![formname]![subformname]![control]
might confuse whoever needs to maintain this six months from now.

"strForm" seems to imply a string variable related to a Form, while your
code seems to imply that you want o refer to a control's name ... and as
written, refers to the control object itself, not its name!

Good luck

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.


ManningFan said:
Is it faster to do something like:

If Forms![formname]![subformname]![control] = "W" or Forms![formname]!
[subformname]![control] = "X" or Forms![formname]![subformname]!
[control] = "Y" or Forms![formname]![subformname]![control] = "Z" Then
... Perform Some Code
Else
Endif

or is it faster do do:

strForm = Forms![formname]![subformname]![control]
If strForm = "W" or strForm = "X" or strForm = "Y" or strForm = "Z"
Then
... Perform Some Code
Else
Endif

I'm trying to figure out which requires less processing, or if they're
both the same. Any help is appreciated!
 
M

ManningFan

"strForm" seems to imply a string variable related to a Form, while your
code seems to imply that you want o refer to a control's name ... and as
written, refers to the control object itself, not its name!

Good luck

Regards

Jeff Boyce
Microsoft Access MVP

Jeff - That was really "air code", what I want to do is look at a
subform's text box and determine what its value is. Depending on
what's in it, I'm running an If/Then/Else statement to process
different code. I was just trying to figure out if you get the value
inside the textbox first and then run the If/Then/Else against the
variable, will it be faster than hitting the subform's textbox over
and over. It may be the exact same thing, I just don't know how
Access treats it.
 
J

Jeff Boyce

Didn't realize it was "air" ...

I'll ask again, though, ...

Are you trying to get better (perceived) performance out of your code, or
are to scraping for microseconds ...?

Regards

Jeff Boyce
Microsoft Access MVP

--
Disclaimer: This author may have received products and services mentioned
in this post. Mention and/or description of a product or service herein
does not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.

"strForm" seems to imply a string variable related to a Form, while your
code seems to imply that you want o refer to a control's name ... and as
written, refers to the control object itself, not its name!

Good luck

Regards

Jeff Boyce
Microsoft Access MVP

Jeff - That was really "air code", what I want to do is look at a
subform's text box and determine what its value is. Depending on
what's in it, I'm running an If/Then/Else statement to process
different code. I was just trying to figure out if you get the value
inside the textbox first and then run the If/Then/Else against the
variable, will it be faster than hitting the subform's textbox over
and over. It may be the exact same thing, I just don't know how
Access treats it.
 
D

David W. Fenton

m:
Is it faster to do something like:

If Forms![formname]![subformname]![control] = "W" or
Forms![formname]! [subformname]![control] = "X" or
Forms![formname]![subformname]! [control] = "Y" or
Forms![formname]![subformname]![control] = "Z" Then
... Perform Some Code
Else
Endif

or is it faster do do:

strForm = Forms![formname]![subformname]![control]
If strForm = "W" or strForm = "X" or strForm = "Y" or strForm =
"Z" Then
... Perform Some Code
Else
Endif

I'm trying to figure out which requires less processing, or if
they're both the same.

It's much faster to assign a value in a control to a variable and
then compare a string to the string stored in that variable than it
is to compare a string to the value returned by the control.

So, yes, your second approach is faster.

You might be able to make the code more efficient thus:

SELECT CASE Forms![formname]![subformname]![control]
CASE "W", "X", "Y", "Z"
' do whatever
CASE ELSE
' do something else
END SELECT

But that's an optimization specific to the way you structured your
example, and not necessarily generalizable.

All that said, beware of premature optimization:

http://en.wikipedia.org/wiki/Premature_optimization#When_to_optimize

Generally, optimizations like this are most effective when they are
done on repetitive operations, say, inside a loop. Most of the time
in a database application is spent sitting around waiting for user
input, so unless your process is slowing that down or is causing the
user to wait while a lengthy process is completed, it's not likely
to be a particularly helpful optimization.

In other words, if this change is 10 milliseconds faster, that
doesn't make any difference at all until the point at which you're
performing the operation 100 times in a row. On tne other hand, if
it lops off half a second from execution time, it's worthwhile.
 

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

Top