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

if statement with combo boxes on a form

100+
P: 107
Can I write an if statement:
if me.comboOne = "Blue" Then
comboTwo = "small", "medium", and "large"

I know that is not a correct code- but I use if statements to fill textboxes from combo boxes- I am just wondering if I can write an if statement and have a second combo box show the choices I want based off of what the user selects in the first combo box?

thank you
Dec 6 '07 #1

✓ answered by Thembelani Zwan

@missinglinq

this code works perfectly except that one line is missing. Before you can actually us Me.Combo2.RowSource, You need to first specify the Row Source Type (Me.Combo2.RowSourceType) which is Value list since you're manually entering information. The following is the updated code.

Expand|Select|Wrap|Line Numbers
  1. If Combo1 = "blue" Then
  2.     Me.Combo2.RowSourceType = "Value List"
  3.     Me.Combo2.RowSource = "small" & ";" & "medium" & ";" & "large"
  4. Else
  5.     Me.Combo2.RowSource = ""
  6. End If

Share this Question
Share on Google+
8 Replies


lee123
100+
P: 556
you could try this:

Expand|Select|Wrap|Line Numbers
  1. if combo1 = blue then
  2.     combo2.text=small", 
  3.     combo2.text="medium", 
  4.     combo2.text= "large"
  5. else
  6.       combo2.text=""
  7.      combo2.text="" 
  8.      combo2.text= ""
  9. end if
lee123
Dec 6 '07 #2

100+
P: 107
Thank You,

** the first part of If statement works good but
for some reason I am getting errors with second part after the else **
Dec 6 '07 #3

missinglinq
Expert 2.5K+
P: 3,532
It's always a good idea to give the version of Access you're running, when asking a question, as it can make a difference! This will do the job in any version of Access, I think.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Combo1_AfterUpdate()
  2. If Combo1 = "blue" Then
  3.  Me.Combo2.RowSource = "small" & ";" & "medium" & ";" & "large"
  4. Else
  5.  Me.Combo2.RowSource = ""
  6. End If
  7. End Sub
  8.  
In the Properties Box - Data for the second Combo2 you have to set Row Source Type to Value List.

Welcome to TheScripts!

Linq ;0)>
Dec 6 '07 #4

100+
P: 107
Thanks for explanation and solution
Dec 6 '07 #5

missinglinq
Expert 2.5K+
P: 3,532
Expand|Select|Wrap|Line Numbers
  1. if combo1 = blue then
  2.      combo2.text=small", 
  3.      combo2.text="medium", 
  4.      combo2.text= "large"
  5.  else
  6.        combo2.text=""
  7.       combo2.text="" 
  8.       combo2.text= ""
  9.  end if
There is no way this code could ever work as written.

if combo1 = blue then

blue
is text and would have to be enclosed in quotation marks

combo2.text=small",
combo2.text="medium",

Aside from the fact that small only has quotes on one end, the commas between the assignment statements have no place in Access syntax

Also, you can only use the .Text property if the control in question has focus. Otherwise you have to use the .Value ; Since .Value is the Default Property for a textbox/combobox, you can omit it, i.e.

combo2.Value = "small"

can be simply written

combo2 = "small"


And lastly, if you correct all of the above problems, since you're assigning three values to the combobox value property, only the last value ("large") will be present!

Linq ;0)>
Dec 6 '07 #6

lee123
100+
P: 556
yea your right when i wrote the code i was in a hurray didn't look at it until now sorry for the bad coding: Missinglinq but thanx for clearing that up.

lee123
Dec 7 '07 #7

P: 1
@missinglinq

this code works perfectly except that one line is missing. Before you can actually us Me.Combo2.RowSource, You need to first specify the Row Source Type (Me.Combo2.RowSourceType) which is Value list since you're manually entering information. The following is the updated code.

Expand|Select|Wrap|Line Numbers
  1. If Combo1 = "blue" Then
  2.     Me.Combo2.RowSourceType = "Value List"
  3.     Me.Combo2.RowSource = "small" & ";" & "medium" & ";" & "large"
  4. Else
  5.     Me.Combo2.RowSource = ""
  6. End If
Jan 23 '19 #8

twinnyfo
Expert Mod 2.5K+
P: 3,032
Also, keep in mind that because this is simply a string your re assigning to the value of the RowSource property, there is no need to concatenate. Thus, line 3 above could simply be:

Expand|Select|Wrap|Line Numbers
  1. Me.Combo2.RowSource = "small;medium;large"
However, if one were building this string by other means, using variables, that does demonstrate the ability to concatenate and build an appropriate string.

Thanks for the clarification n this post, Thembelani Zwan!
Jan 23 '19 #9

Post your reply

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