Solved

VBA between statement

Posted on 2008-10-03
9
2,079 Views
Last Modified: 2013-11-28
In VBA, how do I write an expression that limits BETWEEN to values.  For example:

If [Lookup]= 1 to 10 Then msgbox "found it"

The above doesn't work in VBA but this is what I want to do.  I want it to look for the values 1 to 10.

How can I make this work?

Thanks.
 
0
Comment
Question by:dddw
  • 4
  • 3
  • 2
9 Comments
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 22633783
If [Lookup] >= 1  AND [Lookup] <= 10 Then msgbox "found it"
0
 

Author Comment

by:dddw
ID: 22633883
This helps.  I should have elaborated a little more.  The field [Lookup] has 1 to 30 options.  I want it to find only the option from 10 to 20.  Would code If [Lookup] >= 10  AND [Lookup] <= 20 Then msgbox "found it" still work?  
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 22634020
Select Case [Lookup]
   Case 1 to 10
      'do 1-10 stuff
   Case 11 to 20
      'do 11-20 stuff
   Case Else
       'do anything else stuff
End Select

>I should have elaborated a little more.
One of the more annoying asker issues is the continuous stream of follow-on questions, so yes please try to state all your requirements up front in the original question.  Thanks in advance.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 22634208
>>  Would code If [Lookup] >= 10  AND [Lookup] <= 20 Then msgbox "found it" still work?

For that specific case -- yes (no harm in trying it)

That said, Jim's approach is far more flexible if there are any other missing details :-)

0
 

Author Comment

by:dddw
ID: 22634337
So would the following work.  Thanks for your patience.

Select Case [Lookup]
   Case 1 to 10
      If [Lookup] >= 1 AND [Lookup] <= 10 Then msgbox "Found 1-10"
   Case 11 to 20
      If [Lookup] >= 11 AND [Lookup] <= 20 Then msgbox "Found 11-20"
   Case Else
       msgbox "not found"
End Select
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 250 total points
ID: 22634379
You're combining independant solutions, and creating redundant code.

That will work, but is unnecessarily complicated.

You have two solutions here:

- An If-Then statement
- A Select Case block

They both work fine independantly of one another.

Try coding them up -- and testing them...
0
 

Author Closing Comment

by:dddw
ID: 31502746
Thank you.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 22634418
A Select-Case block is like a very readable mega if-then-else block...
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 22634471
Thanks for the split.  Good luck with your project.  -Jim
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

789 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