View Full Version : Selecting range of values in VB


kodiak385
12-17-2007, 11:33 AM
Sorry if this has been posted somewhere already, but I searched all over and couldn't find it. Anyway, here goes:

I'm trying to use VB to test to see if a control's value meets given conditions, and then assign the control's BackColor a new RBG value based on what it returns. Essentially, I'm using manual coding in lieu of Access' built in conditional formatting.

What I'm currently working with is something like this:

Private Sub txt_FinalColor_lowscores_Click()
Me!txt_FinalColor_lowscores.BackColor = Switch(txt_FinalColor_lowscores > 5, RGB(0, 255, 0), txt_FinalColor_lowscores = 5, RGB(255, 255, 0))
End Sub

So far, this works fine. Single values and simple >/< values don't give me any trouble. The problem I'm running into is that with subsequent conditions, I need to find whether the control's value is between two given values, and I'm not sure how structure that request. I know if it were SQL, I'd use Between x And y, but unfortunately, it's not. I know this has got to be simple, but I'm drawing blanks on how to actually do it. How do I tell VB to test whether a control is between two given values?

Sorry for such a super-noob question, but I'm extremely new to VB, and I've pretty much avoided it like the plague up until this point, hehe. :o I know you can do some really powerful stuff with it, so I want to learn, but I'm just barely getting my feet wet so far. Thanks for the help.

pbaldy
12-17-2007, 12:09 PM
You can use the fact that Switch evaluates left to right. If your first test is <5 and your second test is <10, the second test is basically "between 5 and 9", because anything below 5 would have been handled by the first test.

kodiak385
12-17-2007, 12:18 PM
Oh, hey! That would work, wouldn't it? :) Thanks a ton.

Just out of curiosity, is there any way to call data between values, or would it always be evaluated step by step like that?

pbaldy
12-17-2007, 12:28 PM
Not sure what you mean by calling data between values. What are you trying to achieve?

kodiak385
12-17-2007, 12:37 PM
Sorry, I know my terminology's all screwed up. :o

Basically, is there a VB equivalent of Between x And y, or is the only way to find that through sequential tests?

pbaldy
12-17-2007, 12:46 PM
Well, in a Select/Case structure, you can have

Case 1 To 5

You can't use Between in an If/Then, but you can use:

If Whatever >= 1 And Whatever <=5 Then

More info about what you're trying to achieve will help determine the best method.

kodiak385
12-17-2007, 02:18 PM
Ah, that'll do for now. You've already answered my question anyway. Thank ya much! :D