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

select case not working

P: 35
Hi everyone,

could you please help me. i have a combo box that have the values "1st month", "2nd month" etc, a text box to put details such as weight, length and ccirc. what i want to do is when a user inputs the details and click a button, the text box wres, lres and ccircres will show the results such as "high", "low" or "normal". i tried the following code in the on click event of the button but it wont show anything...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command24_Click()
  2. Select Case Me.age
  3. Case "1st month"
  4.  
  5. ' declares variables
  6.  
  7. If weight <= 2.96 Then
  8. wres = "low"
  9. ElseIf weight >= 4.93 Then
  10. wres = "high"
  11. Else
  12. wres = "Normal"
  13. End If
  14.  
  15. If length <= 49.1 Then
  16. lres = "short"
  17. ElseIf length >= 57 Then
  18. wres = "long"
  19. Else
  20. lres = "Normal"
  21. End If
  22.  
  23. If hcircres <= 34.1 Then
  24. wres = "small"
  25. ElseIf hcircres >= 38.4 Then
  26. wres = "big"
  27. Else
  28. hcircres = "Normal"
  29. End If
  30. End Select
  31.  
  32. Select Case Me.age
  33. Case "2nd month"
  34.  
  35. ' declares variables
  36.  
  37. If Me.[weight] <= 3.57 Then
  38. wres = "low"
  39. ElseIf Me.[weight] >= 5.84 Then
  40. wres = "high"
  41. Else
  42. wres = "Normal"
  43. End If
  44.  
  45. If Me.[length] <= 52.1 Then
  46. lres = "short"
  47. ElseIf Me.[length] >= 60.3 Then
  48. wres = "long"
  49. Else
  50. lres = "Normal"
  51. End If
  52.  
  53. If Me.[hcircres] <= 35.7 Then
  54. wres = "small"
  55. ElseIf Me.[hcircres] >= 39.8 Then
  56. wres = "big"
  57. Else
  58. hcircres = "Normal"
  59. End If
  60. End Select
*its up to 12th months but the code is the same
Apr 23 '11 #1

✓ answered by RuralGuy

Dumb question but you are certain the ComboBox name is age right?

Share this Question
Share on Google+
43 Replies


RuralGuy
Expert 100+
P: 375
I would start by only having *one* Select Case...End Select in the procedure.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command24_Click()
  2.    Select Case Me.age
  3.       Case "1st month"
  4.  
  5.          ' declares variables
  6.  
  7.          If Weight <= 2.96 Then
  8.             wres = "low"
  9.          ElseIf Weight >= 4.93 Then
  10.             wres = "high"
  11.          Else
  12.             wres = "Normal"
  13.          End If
  14.  
  15.          If length <= 49.1 Then
  16.             lres = "short"
  17.          ElseIf length >= 57 Then
  18.             wres = "long"
  19.          Else
  20.             lres = "Normal"
  21.          End If
  22.  
  23.          If hcircres <= 34.1 Then
  24.             wres = "small"
  25.          ElseIf hcircres >= 38.4 Then
  26.             wres = "big"
  27.          Else
  28.             hcircres = "Normal"
  29.          End If
  30.  
  31. '----- Remove   End Select
  32.  
  33. '----- Remove   Select Case Me.age
  34.  
  35.       Case "2nd month"
  36.  
  37.          ' declares variables
  38.  
  39.          If Me.[Weight] <= 3.57 Then
  40.             wres = "low"
  41.          ElseIf Me.[Weight] >= 5.84 Then
  42.             wres = "high"
  43.          Else
  44.             wres = "Normal"
  45.          End If
  46.  
  47.          If Me.[length] <= 52.1 Then
  48.             lres = "short"
  49.          ElseIf Me.[length] >= 60.3 Then
  50.             wres = "long"
  51.          Else
  52.             lres = "Normal"
  53.          End If
  54.  
  55.          If Me.[hcircres] <= 35.7 Then
  56.             wres = "small"
  57.          ElseIf Me.[hcircres] >= 39.8 Then
  58.             wres = "big"
  59.          Else
  60.             hcircres = "Normal"
  61.          End If
  62.  
  63.    End Select
  64.  
  65. End Sub
Apr 23 '11 #2

P: 35
hi,
Thanks for the quick reply. i tried it but still it's not showing any values.
Apr 23 '11 #3

RuralGuy
Expert 100+
P: 375
Put the code in the AfterUpdate event of the ComboBox for more automatic operation. Make sure what is being returned with Me.Age By starting with a MsgBox that displays the value for you as a diagnostic.
Apr 23 '11 #4

P: 35
I put the code in the AfterUpdate event but still won't show any values. Sorry, i'm new to this but how do i make the MsgBox diagnostic?
Apr 23 '11 #5

RuralGuy
Expert 100+
P: 375
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command24_Click()
  2.    MsgBox "[" & Me.age & "]"
  3.    Select Case Me.age 
  4.  
Apr 23 '11 #6

P: 35
it returns a message box that says "[]". that's it.
Apr 23 '11 #7

RuralGuy
Expert 100+
P: 375
That means your Case structure will not work, which is what you are seeing. What is the Bound Column of your ComboBox and what do you have and the RowSource and RowSourceType?
Apr 23 '11 #8

P: 35
RowSource = "1st month";"2nd month";"3rd month";"4th month";"5th month";"6th month";"7th month";"8th month";"9th month";"10th month";"11th month";"12th month";"15th month";"18th month";"24th month"

RowSourcceType = Value List

BoundColumn = 1
Apr 23 '11 #9

P: 35
If it is of any help, i made the combo box by choosing the 2nd option in the wizard something like "i will type the values that i want". then typed it in the form view.
Apr 23 '11 #10

RuralGuy
Expert 100+
P: 375
Did you move the code to the AfterUpdate event of the ComboBox?
Apr 23 '11 #11

P: 35
yup, still no values and the message box displays "[]"
Apr 23 '11 #12

RuralGuy
Expert 100+
P: 375
Dumb question but you are certain the ComboBox name is age right?
Apr 23 '11 #13

P: 35
This is embarrassing...
actually it might be the answer! it was unbound and nothing is written in the control source. it's working now. thank you very much!
Apr 23 '11 #14

RuralGuy
Expert 100+
P: 375
That's Great! Glad we got it working finally.
Apr 23 '11 #15

P: 35
Yes! A really big thanks to you!!! You have been very helpful!
Apr 23 '11 #16

P: 35
hi again, i think this may be a related question since its still the combo box, because when i tested it, the results where all over the place like it would say normal when it shouldn't and sometimes one text box won't display any value at all. i checked the code but it's ok the values are the way they should be. please help.
Apr 23 '11 #17

RuralGuy
Expert 100+
P: 375
I'm not sure what else I can do with what we have so far. Do you know how to single step the code to see what values are returned and what branches of the code are taken?
Apr 23 '11 #18

P: 35
Hi!
What we have done before solves the problem that i have with the combo box. My problem now is that its not returning the correct values that i set. maybe it have something to do with the code being long and almost the same? about the single step, i don't know how to do that either.
Apr 24 '11 #19

RuralGuy
Expert 100+
P: 375
While looking at the code you click outside the left margin and a dot appears. That is a BreakPoint. When you run the form and execute the code, it will stop at the BreakPoint. You can then F8 a line at a time and watch the path the code takes. Hovering over variables will pop up their current value.
Apr 24 '11 #20

P: 35
How will i know that its the wrong code? most of them turns yellow when i press F8.
Apr 24 '11 #21

100+
P: 131
Mac, pressing F8 helps runs the code step-by-steps, you just check the results to the end of code! Just Continue and you may know where the code is doing wrong
Apr 24 '11 #22

P: 35
where do i see the results? after running the code step by steps up to the End sub nothing happens.
Apr 24 '11 #23

RuralGuy
Expert 100+
P: 375
Is the code that sets the other values executing?
Apr 24 '11 #24

P: 35
Yes, but showing other values. like if it should be "normal" it would show "big" or "short" vice versa. And the weight textbox at first it may show for example "low" but if i again click the button it shows "big". and with some values like "60.3" it just won't show anything.
Apr 24 '11 #25

RuralGuy
Expert 100+
P: 375
Is the right code executing (getting highlighted)? When you hover over weight for example does it display the correct value?
Apr 24 '11 #26

P: 35
Nope. It displays the value that i see in the form which is wrong.
Apr 24 '11 #27

RuralGuy
Expert 100+
P: 375
You lost me on that one. You enter a value in the weight control and when your code executes and you hover over the weight entry you get the "wrong" value?
Apr 24 '11 #28

RuralGuy
Expert 100+
P: 375
Maybe you should try and attach you db at this time. Compact and Repair and then ZIP it up first.
Apr 24 '11 #29

P: 35
Yup! From the form view, the value that pops out when i clicked the results button is already wrong, that's the same value that comes when i hover in the code view. Here's my DB, I really appreciate this man.
Attached Files
File Type: zip DBmac.zip (22.8 KB, 84 views)
Apr 24 '11 #30

RuralGuy
Expert 100+
P: 375
The Name of your control is Combo22. "age" is the field to which that control is bound. The field is not actually updated until you either move to another record or close the form.
Apr 24 '11 #31

P: 35
Yup, when it is in the afetupdate event. but the code is in the button. and if ever it updates it returns the wrong values. Different to those that i wrote on the code.
Apr 24 '11 #32

RuralGuy
Expert 100+
P: 375
All of the fields in the Development table save two, are text fields. You are doing numerical testing against "text". Your table fields should be probably the Single DataType fields.
Apr 24 '11 #33

P: 35
Actually they were numbers before general number and double. Then i tried text, the results were the same.
Apr 24 '11 #34

RuralGuy
Expert 100+
P: 375
Try changing them to Single or Double again and see what happens.
Apr 24 '11 #35

P: 35
Still it doesn't show the correct values. have you seen the code? i think i did it correctly? the problem is that for example in the code <= 6.99 should be "short" but after i pressed the button it shows "Normal" or "big". and the thing is "big" is in the other "if" statement.
Apr 24 '11 #36

RuralGuy
Expert 100+
P: 375
You should name your Controls differently that the fields to which they are bound. Access does not know whether you are referring to the field or the control.
Apr 24 '11 #37

RuralGuy
Expert 100+
P: 375
See if this "1st Month" works as you would expect.
Attached Files
File Type: zip DBmac1.zip (34.6 KB, 73 views)
Apr 24 '11 #38

P: 35
Actually to all the data i placed there already, the 1st month returns the best answer. Can you try the 2nd month? and also where do i change the control name?
Apr 24 '11 #39

P: 35
the 1st month returns the values correctly now
Apr 24 '11 #40

P: 35
i see the control now i did that also but i didn't change the code. that's why.
Apr 24 '11 #41

RuralGuy
Expert 100+
P: 375
The name of a control is on the "Other" tab of the property sheet of the control. If you use the Me. preface in the code then Intelisense will show you the possible legal answers.
Apr 24 '11 #42

P: 35
I found it!!! Thanks to you rural guy! I tried and change the 2nd month as well and found that i messed a lot since i only copy, paste and edit it so, it ended up that some of them have the same name. I should be more careful when coding. aside from the fact that i'm using the field name. Thanks a lot again!!!!!
Apr 24 '11 #43

NeoPa
Expert Mod 15k+
P: 31,758
A good point to note here is that if you were in the (good) habit of using capital letters within your object names then the name "age", being all lower-case, would have jumped out as an obvious problem right from the start. It makes sense to use the facilities available to help with your work.
Apr 25 '11 #44

Post your reply

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