Making "if" statements that return multiple results.

  • Thread starter Thread starter StacyM
  • Start date Start date
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.
 
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
 
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!
 
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
 
Back
Top