Help me to finish this code (1 Viewer)

ekadecade

New member
Local time
Yesterday, 22:18
Joined
Aug 13, 2018
Messages
4
Hi i'm new into access and i just start it recently and i dont know how to finih my if statment and i had been searching for the code from youtube still cant find it. Please help to find it.
I have a table called BlockPlace and there're only 2 fields Block and Status
and this's code that i use to change color in the from_load

If [Block] = 1 And [Status] = "AVAILABLE" Then
Label1.ForeColor = vbGreen
ElseIf [Block].Value = 1 And [Status] = "OCCUPIED" Then
Label1.ForeColor = vbRed
End If

this code is working properly but this one

If [Block] = 2 And [Status] = "AVAILABLE" Then
Label2.ForeColor = vbGreen
ElseIf [Block].Value = 2 And [Status] = "OCCUPIED" Then
Label2.ForeColor = vbRed
End If

is not working for number 2
so basically i just want get the label font color to green and red it's simple but the source is from the table and i dont know how to use it, can someone tell me what i must i change for [block] to get the right record

thanks ~
 

isladogs

MVP / VIP
Local time
Today, 06:18
Joined
Jan 14, 2017
Messages
18,209
Do you have records to trigger both parts of the Block=2 code?
Where are you using it?

The code is identical apart from changing 1 to 2.
When you say it's not working, what happens?
Nothing? Error message? Access crashes? What else?
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 15:18
Joined
Jan 20, 2009
Messages
12,851
Your code only looks at the current record on a form or report.

You probably want ConditionalFormatting. Right click on the textbox to access it and add a condition.
 

isladogs

MVP / VIP
Local time
Today, 06:18
Joined
Jan 14, 2017
Messages
18,209
I've just realised the code is in Form_Load so it will only affect the first record.
If it's a single form you could use the form current event instead.

However, I would also go for CF as Galaxiom suggested.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 01:18
Joined
May 21, 2018
Messages
8,525
You probably want ConditionalFormatting. Right click on the textbox to access it and add a condition.
it is a label so no con format. but i would agree to change the labels into text boxes and use con format.
 

ekadecade

New member
Local time
Yesterday, 22:18
Joined
Aug 13, 2018
Messages
4
prnt.sc/khwjri

this is the the table that i'm using right now there'r only 2
prnt.sc/khwjg7

so the code is only working at number 1 not the number 2 and the rest of them
 

Mark_

Longboard on the internet
Local time
Yesterday, 22:18
Joined
Sep 12, 2017
Messages
2,111
Label2.ForeColor should really be Me.Label2.ForeColor, that way you avoid any ambiguous references. It will also help you identify if Label2 is really Label2 or not.

For myself, I prefer to NOT have multiple redundant checks on the same value, so I'd probably code it as
Code:
If [Block] = 2 then
   case select [Status] 
      case "AVAILABLE" 
         Me.Label2.ForeColor = vbGreen
      case "OCCUPIED"
         Me.Label2.ForeColor = vbRed
      End Select
End If
That way I have all possible responses for [Block] = 2 in one spot and I can even default a color if needed.

Just to be clear, you are using two different labels for your two values for [block], correct?
 

Users who are viewing this thread

Top Bottom