ShannonCallahan
asked on
"If Date" in MS Access
Good morning experts,
I want to be able to change the background color of a combobox based off its selected value compared to the current calendar quarter. something like:
If combobox.value = "1st Quarter" AND current quarter is not 1 then
combobox.backcolor = vbRed
elseif
combobox.value = "2nd Quarter" AND current quarter is not 2 then
combobox.backcolor........ .... and so on.
Please let me know if you need further information.
Thank you.
V/R,
Shannon
I want to be able to change the background color of a combobox based off its selected value compared to the current calendar quarter. something like:
If combobox.value = "1st Quarter" AND current quarter is not 1 then
combobox.backcolor = vbRed
elseif
combobox.value = "2nd Quarter" AND current quarter is not 2 then
combobox.backcolor........
Please let me know if you need further information.
Thank you.
V/R,
Shannon
Btw, you can also use conditional formatting:
- Right click the combo box in design view and select conditional formatting.
- Use ExpressionIS formatting to build your conditions
If you are using Access 2010, you can have more than three conditions, whose expressions you can specify in this format, picking different background colors for each condition:
[combobox] = "First Quarter" AND (int(Month(Date()))/4 + 1) <> 1
[combobox] = "Second Quarter" AND (int(Month(Date()))/4 + 1) <> 2
etc
- Right click the combo box in design view and select conditional formatting.
- Use ExpressionIS formatting to build your conditions
If you are using Access 2010, you can have more than three conditions, whose expressions you can specify in this format, picking different background colors for each condition:
[combobox] = "First Quarter" AND (int(Month(Date()))/4 + 1) <> 1
[combobox] = "Second Quarter" AND (int(Month(Date()))/4 + 1) <> 2
etc
Here's another way if you only have the 4 choices in your combo box.
If Me.ComboBox.ListIndex + 1 <> Format(DATE, "q") Then
Me.ComboBox.backColor = vbRed
Else
Me.ComboBox.backColor = vbWhite
End If
...I am just curious?
<combobox.value = "2nd Quarter" AND current quarter is not 2 then>
Why would selecting a value NOT produce the correct "quarter"?
If your combobox row source is a table with both values:
tblQuarters
qID
qName
ex:
qID qName
1 First Quarter
2 Second Quarter
3 Third Quarter
4 Fourth Quarter
...Selecting "Second Quarter" would always produce a 2 as the value
Then you can use this (2) for your "Current Quarter" Value
me.CurrentQuarter=YourComb obox.Colum n(1)
...Thus bypassing the need for this system...
JeffCoachman
<combobox.value = "2nd Quarter" AND current quarter is not 2 then>
Why would selecting a value NOT produce the correct "quarter"?
If your combobox row source is a table with both values:
tblQuarters
qID
qName
ex:
qID qName
1 First Quarter
2 Second Quarter
3 Third Quarter
4 Fourth Quarter
...Selecting "Second Quarter" would always produce a 2 as the value
Then you can use this (2) for your "Current Quarter" Value
me.CurrentQuarter=YourComb
...Thus bypassing the need for this system...
JeffCoachman
Jeff,
I initially wondered the same thing, but I think existing records probably hold older data whose quarter as displayed in the combo needs to be compared to the quarter corresponding to today's date.
I initially wondered the same thing, but I think existing records probably hold older data whose quarter as displayed in the combo needs to be compared to the quarter corresponding to today's date.
...or if the (2) comes from an external source, you can "Lookup" the correct text without needing a combobox.
YourQuatretText=Dlookup("q Name","tbl Quarters", "qID=" & me.QuarterValue)
This says:
Find the qID in tblQuarters where the QID is whatever values you have in the "QuarterValue" field/textbox...
YourQuatretText=Dlookup("q
This says:
Find the qID in tblQuarters where the QID is whatever values you have in the "QuarterValue" field/textbox...
<I think existing records probably hold older data whose quarter as displayed in the combo needs to be compared to the quarter corresponding to today's date.>
good point...
;-)
Jeff
good point...
;-)
Jeff
ASKER
First, thank you all for your comments. I am trying to take the advice given to me by Jeff a couple days ago about simplifying this project. With this in mind I would like to use the conditional formating and stay away from VBA. Let me try to explain this better.
I have a combobox that I typed in the values I wanted:
1st
2nd
3rd
4th
These values represent the quarter a specific training was completed. I want the back color of the combobox to change red if the selected quarter is not the current quarter per date(month()). Example: This month is Sept (falls under 3rd calendar quarter) and if I have any value other than "3rd" is selected, then it will automatically turn red.
Thanks again,
V/R,
Shannon
I have a combobox that I typed in the values I wanted:
1st
2nd
3rd
4th
These values represent the quarter a specific training was completed. I want the back color of the combobox to change red if the selected quarter is not the current quarter per date(month()). Example: This month is Sept (falls under 3rd calendar quarter) and if I have any value other than "3rd" is selected, then it will automatically turn red.
Thanks again,
V/R,
Shannon
As DatePart returns a number, I would do like this:
Dim lngBackColor As Long
If Me!ComboBox.ListIndex = DatePart("q", Date) - 1 Then
lngBackColor = vbWhite
Else
lngBackColor = vbRed
End If
Me!ComboBox.BackColor = lngBackColor
/gustav
Dim lngBackColor As Long
If Me!ComboBox.ListIndex = DatePart("q", Date) - 1 Then
lngBackColor = vbWhite
Else
lngBackColor = vbRed
End If
Me!ComboBox.BackColor = lngBackColor
/gustav
ASKER
So I tried the conditional formating:
[cboCAPEQuarter].Value="1s t" And (Int(Month(Date()))/4+1)<> 1
[cboCAPEQuarter].Value="2n d" And (Int(Month(Date()))/4+1)<> 2
[cboCAPEQuarter].Value="3r d" And (Int(Month(Date()))/4+1)<> 3
[cboCAPEQuarter].Value="4t h" And (Int(Month(Date()))/4+1)<> 4
the problem is, no matter whick value I select, the combobox is always green. Any suggestions?
V/R,
Shannon
[cboCAPEQuarter].Value="1s
[cboCAPEQuarter].Value="2n
[cboCAPEQuarter].Value="3r
[cboCAPEQuarter].Value="4t
the problem is, no matter whick value I select, the combobox is always green. Any suggestions?
V/R,
Shannon
You probably need one test only:
Val(Nz([cboCAPEQuarter].Va lue,0))=Da tePart("q" ,Date())
/gustav
Val(Nz([cboCAPEQuarter].Va
/gustav
ASKER
<You probably need one test only:>
what do you mean?
Shannon
what do you mean?
Shannon
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
?? I wrote the complete expression/test? What else do you need?
/gustav
/gustav
ASKER
Thank you all for your help,
[cboCAPEQuarter].ListIndex +1=DatePar t("q",Date ())
That was what I was looking for. The only issue that I am having is I want the combobox to change to red if it is not correct. I would just set the combobox's back color to red but it changes the color of the drop down options backcolor as well.
V/R,
Shannon
[cboCAPEQuarter].ListIndex
That was what I was looking for. The only issue that I am having is I want the combobox to change to red if it is not correct. I would just set the combobox's back color to red but it changes the color of the drop down options backcolor as well.
V/R,
Shannon
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for your help.
V/R,
Shannon
V/R,
Shannon
Open in new window