Making "if" statements that return multiple results.

S

StacyM

I am using Access to create this tool for a company and I am having a little
trouble with the expressions. I just started using the program last week.
What I want is a statement that will give the user multiple choices depending
on their choice in another text box.For example,if they chose Corn, the
months would be March, May July, September and December, but if they choose
Soybeans, the choices should be January, March, May, July, August, September,
and November. I have tried some different things but it doesn't work quite
right. I think the "IIf" would be able to do this, but if there is an easier
expression, that would be great too.
 
S

Scott Whetsell, A.S. - WVSP

Try this:

If Me.cboBoxName = "Corn" Then
With Me.ListBoxName
.RowSource = ""
.AddItem "March"
.AddItem "May"
.AddItem "July"
.AddItem "August"
.AddItem "September"
.AddItem "November"
End With
ElseIf Me.cboBoxName = "Soybeans" Then
With Me.ListBoxName
.RowSource = ""
.AddItem "January"
.AddItem "March"
.AddItem "May"
.AddItem "July"
.AddItem "September"
.AddItem "December"
End With
End If
 
J

John W. Vinson

I am using Access to create this tool for a company and I am having a little
trouble with the expressions. I just started using the program last week.
What I want is a statement that will give the user multiple choices depending
on their choice in another text box.For example,if they chose Corn, the
months would be March, May July, September and December, but if they choose
Soybeans, the choices should be January, March, May, July, August, September,
and November. I have tried some different things but it doesn't work quite
right. I think the "IIf" would be able to do this, but if there is an easier
expression, that would be great too.

What's the context, Stacy? How does the program (or how do you!!) make the
connection between a crop and a date?

I'm GUESSING that you'll want a Table reflecting the connection (a field for
crop and a field for the month), and that you would use a combo box on a form
to select a crop; a second combo box would be based on a Query referencing the
first combo box to select only the right months. But I'm not sure if that's
what you're getting at!
 
A

Azonei

I am using Access to create this tool for a company and I am having a little
trouble with the expressions.  I just started using the program last week.
What I want is a statement that will give the user multiple choices depending
on their choice in another text box.For example,if they chose Corn, the
months would be March, May July, September and December, but if they choose
Soybeans, the choices should be January, March, May, July, August, September,
and November.  I have tried some different things but it doesn't work quite
right.  I think the "IIf" would be able to do this, but if there is an easier
expression, that would be great too.

If you have a number of options to choose from (like 3 or more), I
would use a Select Case statement:

Select Case CropName
Case "Corn"
With Me.ListBox1
.RowSource = ""
.AddItem "March"
.AddItem "May"
.AddItem "July"
.AddItem "August"
.AddItem "September"
.AddItem "November"
End With
Case "SoyBeans"
With Me.ListBoxName
.RowSource = ""
.AddItem "January"
.AddItem "March"
.AddItem "May"
.AddItem "July"
.AddItem "September"
.AddItem "December"
End With
Case "Wheat"
With Me.ListBoxName
.RowSource = ""
.AddItem "March"
.AddItem "May"
.AddItem "July"
.AddItem "September"
.AddItem "December"
End With
Case Else
....
End Select
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top