By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,097 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,097 IT Pros & Developers. It's quick & easy.

VB in MS Access - Using < and > ...Please help

agroover
P: 8
I know the following code is probably way off, but I really need some help with this. I keep trying different things, but nothing seems to work. I'm just getting really frustrated. Below is what I have so far.

If Grade.text = 1006, then I don't want the user to be able to put certain values into the C textbox. The value needs to be >= 0.04 and <= 0.06. If they enter any other value, I want a message box to appear saying that the value is not valid.

Expand|Select|Wrap|Line Numbers
  1. Private Sub C_Enter()
  2.  
  3. GRADE.SetFocus
  4. C.Locked = False
  5.  
  6. If Trim(GRADE.Text) = "1006" Then
  7.     ElseIf C.Value >= 0.04 Then
  8.         MsgBox "Value must be between 0.04 and 0.06"
  9.     ElseIf C.Value <= 0.06 Then
  10.         MsgBox "Value must be between 0.04 and 0.06"
  11.     Else
  12.  
  13. End If
Any help at all would be greatly appreciated. I've searched everywhere trying to find out what I need to do and what is wrong. I'm not sure if I should use the DMin and DMax functions or not.

I'd really appreciate any sugguestions someone might have.

Thanks
Apr 4 '07 #1
Share this Question
Share on Google+
6 Replies


Tig201
100+
P: 103
Try
code
Expand|Select|Wrap|Line Numbers
  1. If Val(C.Text) >= 0.4 And Val(C.Text) <= 0.6 Then
  2. MsgBox ("enter a value between 0.4 and 0.6")
you may also want to add error checking to validate C.Text isnumeric
Apr 4 '07 #2

Expert 5K+
P: 8,434
Actually agroover, your code looks as though it's very close to doing what you want. You've just got the syntax of your nested IF statements a little jumbled.

Try this slight modification...

Expand|Select|Wrap|Line Numbers
  1. Private Sub C_Enter()
  2.  
  3. GRADE.SetFocus
  4. C.Locked = False
  5.  
  6. If Trim(GRADE.Text) = "1006" Then
  7.     If C.Value >= 0.04 Then
  8.         MsgBox "Value must be between 0.04 and 0.06"
  9.     ElseIf C.Value <= 0.06 Then
  10.         MsgBox "Value must be between 0.04 and 0.06"
  11.     End If
  12. End If
Oh, and you probably should just use "<" and ">" rather than ">=" and "<=". After all, if you are telling the user the value must be 0.04 to 0.06, it seems a bit unfriendly to complain when they enter 0.04 or 0.06, doesn't it.
Apr 5 '07 #3

agroover
P: 8
Thank you both for your help. I'm going to try it out and let you know if they work out. Thanks again for the suggestions and the help. It's greatly appreciated.
Apr 5 '07 #4

Expert 5K+
P: 8,434
Thank you both for your help. I'm going to try it out and let you know if they work out. Thanks again for the suggestions and the help. It's greatly appreciated.
I just realised, we had our "less than" and "greater than" backwards. You'll need to swap them around.
Apr 6 '07 #5

agroover
P: 8
Thank you Killer42 andTig201 for all of your help. I was able to get the code to work.

Below is the code I ended up using.

Expand|Select|Wrap|Line Numbers
  1. Private Sub C_LostFocus()
  2. GRADE.SetFocus
  3. C.Locked = False
  4.  
  5. If Trim(GRADE.Text) = "1006" Then
  6.     If C.Value < 0.04 Or C.Value > 0.06 Then
  7.         MsgBox "Value must be between 0.04 and 0.06"
  8.     End If
  9. End If
  10.  
  11. End Sub
Thanks again.
Apr 9 '07 #6

Expert 5K+
P: 8,434
Excellent! Glad we could help. :)
Apr 9 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.