Textbox_AfterUpdate not working with another control - ms-access

Why I can't change value of another control in access form in afterupdate function?
Private Sub cNPSrate_AfterUpdate() 'Form_new_opinion_in!
If Not IsNull(cNPSRate) Then
Select Case cNPSRate
Case 1 To 6
cNPSRate.BackColor = RGB(255, 0, 0) And cSegmentNPS.Text = "KRYTYK"
Case 7 To 8
cNPSRate.BackColor = RGB(255, 255, 0) And cSegmentNPS.Text = "NEUTRALNY"
Case 9 To 10
cNPSRate.BackColor = RGB(0, 255, 0) And cSegmentNPS.Text = "PROMOTOR"
End Select
Else
cNPSRate.BackColor = RGB(255, 255, 255) And cSegmentNPS.Text = Null
End If
End Sub
The code works if I remove And cSegmentNPS.Text. Can't I use another control in this function or I make some mistake?
cNPSRate and cSegmentNPS are TEXTBOXes

A text box's .Text property is only available when that text box has focus. At any other time, use its .Value property instead.
I would also put each action on its own line instead of combining them with And on one line.
So I suggest you revise your code to follow this pattern ...
cNPSRate.BackColor = RGB(255, 0, 0)
cSegmentNPS.Value = "KRYTYK"

Related

chtobj.Chart.ChartArea.Format.Fill.Visible= msoFalse getting mismatch error

I have created a object and need to set Format.Glow, Format.Fill and Format.SoftEdge.Radius etc
The code from excel uses With Selection but that's not allowed in access. I used the watch window and see the properties but can't assign anything.
enter code here
For Each chtobj In shtTemp.ChartObjects
If chtobj.Name = "Chart4" Then
Debug.Print chtobj.Name
'
chtobj.Chart.ChartArea.Format.Fill.Visible = msoTrue
chtobj.Chart.ChartArea.Format.ForeColor.RGB = RGB(0, 176, 80)
chtobj.Chart.ChartArea.Format.Transparency = 0
chtobj.Chart.ChartArea.Format.Solid
With chtobj.Chart.ChartArea.Format.Glow
.Color.RGB = RGB(255, 255, 0)
.Transparency = 0
.Radius = 10
End With
chtobj.Chart.ChartArea.Format.Line.Visible = msoFalse
chtobj.Chart.ChartArea.Format.SoftEdge.Radius = 1
End If
Next
I found the correct syntax for VBA:
chtobj.Chart.Shapes.Parent.ChartArea.Format.Fill.Visible = msoTrue

How can I check a checkbox in Access as I move from record to record?

I have an Access database which has a checkbox that I need to see if it's "false" or "true" as I go from record to record. If the checkbox is true the label font color and name should change.
This is my code but it's not working properly:
Private Sub Check796_Click()
lngRed = RGB(255, 0, 0)
lngBlack = RGB(0, 0, 0)
If Me.Check796 = vbTrue Then
Me.Label797.Caption = "Hold"
Me.Label797.ForeColor = lngRed
Else
Me.Label797.Caption = "UnHold"
Me.Label797.ForeColor = lngBlack
End If
End Sub
If it's checked it should be red and say "Hold" but, if it's unchecked it should say "UnHold". If I check the "Hold" checkbox it's works but, when I go to the next record which is unchecked it says "Hold" as well. When I close the application and go back into the record with the checkbox clicked the label says "UnHold" when it should say "Hold".
Just execute the code again on Form_Current:
Private Sub Form_Current()
lngRed = RGB(255, 0, 0)
lngBlack = RGB(0, 0, 0)
If Me.Check796 = vbTrue Then
Me.Label797.Caption = "Hold"
Me.Label797.ForeColor = lngRed
Else
Me.Label797.Caption = "UnHold"
Me.Label797.ForeColor = lngBlack
End If
End Sub
To do it without any VBA:
Add your checkbox with the Control Source set to your boolean
(Yes/No) field.
Add a textbox (format to look like a label).
Give it a Control
Source of =Choose(Abs([Check796])+1,'UnHold','Hold')
A boolean
returns -1 for TRUE and 0 for FALSE.
ABS removes the sign, so
it's now 0 and 1.
Add 1 as CHOOSE needs to start at 1.
Select the text and in the FORMAT ribbon in Form Design Tools
select Conditional Formatting.
Select Field Value Is equal to
"Hold" and format as red font.

Comparing value in Access report field to a query using VBA

I currently have a report that lists steps for various tasks and tracks the revision count. Each page is one task, with all of the steps under it. At the end of the report is a Revision history where it will list what was changed to update the revision count. I have two queries for the report, one to generate the report data and the other to generate the revision history.
What I need to do, is to show which tasks have changes on them. I want to do that by comparing the Task_ID on each report page to the Task_ID found in the revision history query.
I've tried a few varitions of dlookups and dcounts without any luck. Whenever I monitor the code, it keeps treating it as false and will end it.
If DLookup("[Task_ID]", "[qry_revision_history_conversions]") = [Reports]![rpt_WI_Book].[Report]![Task] Then
[Reports]![rpt_WI_Book].[Report]![Rev_Change].Visible = False
It's probably something really simple I'm missing, but I can't seem to wrap my head around it.
Here's the code I used to make it work.
Dim LookupTask As Variant
Dim lngRed As Long, lngYellow As Long, lngWhite As Long
lngRed = RGB(255, 0, 0)
lngBlack = RGB(0, 0, 0)
lngYellow = RGB(255, 255, 0)
lngWhite = RGB(255, 255, 255)
LookupTask = DLookup("[Task_ID]", "[qry_task_check]", [Reports]![rpt_WI_Book].[Report]![Text474])
If LookupTask = [Reports]![rpt_WI_Book].[Report]![Text474] Then
[Reports]![rpt_WI_Book].[Report]![Text474].BackColor = lngYellow
Else
[Reports]![rpt_WI_Book].[Report]![Text474].BackColor = lngWhite
End If
Not sure exactly what fields are in your table, but maybe I can assume you're trying to match TaskID to the Report Task field?
From the DLookup docs
if you don't supply a value for criteria, the DLookup function returns a random value
Use your criteria inside DLookup and then check for Null:
Dim LookupTask as Variant
LookupTask = DLookup("[Task_ID]", "[qry_revision_history_conversions]", "[Task_ID] = " & [Reports]![rpt_WI_Book]![Task])
If Not IsNull(LookupTask) Then
[Reports]![rpt_WI_Book]![Rev_Change].Visible = False
End If

MS Access - change colors of the bar chart based on series value

I'm trying to write a code in MS Access 2013 that will change the color of the each bar in the chart based on value of the series. Data are stored in query that changes according to the set of conditions.
In the form is chain of combo boxes and listboxes witch set the conditions for query. After requery the output is a graph.
Private Sub listS_AfterUpdate()
Me.graph.Requery
Dim i
With graph
For i = 1 To .SeriesCollection.Count
If .SeriesCollection(i).Value < 0.78 Then
.SeriesCollection(i).Border.Color = RGB(255, 0, 0)
.SeriesCollection(i).Interior.Color = RGB(255, 0, 0)
Else
.SeriesCollection(i).Border.Color = RGB(0, 255, 0)
.SeriesCollection(i).Interior.Color = RGB(0, 255, 0)
End If
Next i
End With
End Sub
The problem lies in the IF statement:
If .SeriesCollection(i).Value < 0.78 Then
Error:
Object doesn't support this property or method
Can anyone help me out with this?
Thank you
I've done this in Excel, but the way I had to do it was to have multiple ranges in the bar chart, each with a different color, and then the data in each range was selected based upon its values. Maybe this would work in Access as well?

VBA Chart Coloring

I've got a problem with coloring chosen records from chart with VBA.
I've got some code which colors every record in my chart. Here it is:
SeriesCount = MyChart.SeriesCollection.Count
For i = 1 To SeriesCount
MyChart.SeriesCollection(i).Interior.Color = RGB(255, 0, 0)
Next
What I want is to change color of chosen record, for example 20 on picture below. Thanks for help.
You can hard code it by position. Since 20 is the 5th point in the graph you can use this code:
ActiveChart.SeriesCollection(1).Points(5).Interior.color = RGB(55, 130, 150)
If you need to search for 20, it's a little more difficult.
Sub color()
Dim MyChart As Chart
Set MyChart = ActiveChart
SeriesCount = MyChart.SeriesCollection.Count
For i = 1 To SeriesCount
Set s = MyChart.SeriesCollection(i)
vals = s.XValues
For x = LBound(vals) To UBound(vals)
If vals(x) = 20 Then
s.Points(x).Interior.color = RGB(55, 130, 150)
Else
s.Points(x).Interior.color = RGB(255, 0, 0)
End If
Next x
Next
End Sub
Tested: