Solved check if listbox contain specific value (1 Viewer)

eshai

Registered User.
Local time
Today, 05:16
Joined
Jul 14, 2015
Messages
193
hi:
i have a listbox 2 Column that get the value from a query
in the form there is also textbox
my vba code should change the background color of the textbox if some value exist in the listbox
and if not set different background
i have like a blank in my mind and i can't remember
Code:
If Me.listbox = "1" Then Me.textbox.BackColor = RGB(255, 0, 0) Else Me.textbox.BackColor = RGB(0, 255, 0)
.value
.Column
.rowsource
.???
 

June7

AWF VIP
Local time
Yesterday, 18:16
Joined
Mar 9, 2014
Messages
5,465
Try the code you show. Every instance of control will show same value.

Use Conditional Formatting and no VBA needed and color will be dynamic for each record.
 

Eugene-LS

Registered User.
Local time
Today, 05:16
Joined
Dec 7, 2018
Messages
481
Use ListIndex property :
Code:
If Me.listbox.ListIndex > -1 Then 'exist in the listbox
       Me.textbox.BackColor = RGB(255, 0, 0)
Else
       Me.textbox.BackColor = RGB(0, 255, 0)
End If
 

eshai

Registered User.
Local time
Today, 05:16
Joined
Jul 14, 2015
Messages
193
Use ListIndex property :
Code:
If Me.listbox.ListIndex > -1 Then 'exist in the listbox
       Me.textbox.BackColor = RGB(255, 0, 0)
Else
       Me.textbox.BackColor = RGB(0, 255, 0)
End If
there are all kind of numbers 1,1a,1b,2 ect'
your code is giving the "Else"
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:16
Joined
Feb 19, 2002
Messages
43,223
I am mystified by the logic. Why are you trying to find out if the RowSource of the listbox contains a specific value?
 

eshai

Registered User.
Local time
Today, 05:16
Joined
Jul 14, 2015
Messages
193
Try the code you show. Every instance of control will show same value.

Use Conditional Formatting and no VBA needed and color will be dynamic for each record.
thank you
my confuse its a label not a textbox
any way i don't like to use Conditional Formatting access is not a graphical software
 

Eugene-LS

Registered User.
Local time
Today, 05:16
Joined
Dec 7, 2018
Messages
481
there are all kind of numbers 1,1a,1b,2 ect'
your code is giving the "Else"
you asked: if some value exist in the listbox ? ...
The ListIndex property returns -1 when it's value not exist in ListBox.RowSouce ...
 

eshai

Registered User.
Local time
Today, 05:16
Joined
Jul 14, 2015
Messages
193
I am mystified by the logic. Why are you trying to find out if the RowSource of the listbox contains a specific value?
I'm building a student dormitory drawing and the list box well show me the name and the label well show my Whether the bed is occupied or not
 

eshai

Registered User.
Local time
Today, 05:16
Joined
Jul 14, 2015
Messages
193
you asked: if some value exist in the listbox ? ...
The ListIndex property returns -1 when it's value not exist in ListBox.RowSouce ...
thank you
so in words the code should be like "if listbox contain "1b" then background =...
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:16
Joined
Feb 19, 2002
Messages
43,223
If you are using a listbox, the listbox can show that information since it shows multiple columns. You don't need code to populate a label.
 

Eugene-LS

Registered User.
Local time
Today, 05:16
Joined
Dec 7, 2018
Messages
481
so in words the code should be like "if listbox contain "1b" then background =...
No! I think not that way.
Please publish your form here - it well be easier to answer
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:16
Joined
Feb 19, 2002
Messages
43,223
Maybe you should post the database with instructions regarding what you want to see.

my vba code should change the background color of the textbox if some value exist in the listbox
This statement does not make sense. What is the criteria for the RowSource of the listbox? Why do you want to know if a value is in the listbox's RowSource? Why do you not want to know what is selected?
 

eshai

Registered User.
Local time
Today, 05:16
Joined
Jul 14, 2015
Messages
193
Maybe you should post the database with instructions regarding what you want to see.


This statement does not make sense. What is the criteria for the RowSource of the listbox? Why do you want to know if a value is in the listbox's RowSource? Why do you not want to know what is selected?
my db is in other language
il explain what i did. I built a room drawing took my original drawing and put it in the back of the form Next to each room is a list box
that show the name and the bad number on the drawing i used labels as bads 1,2,3,4 ect' now if the number of the bad is in the list box
so the bad should be red and if not green
for every list box i used query builder as Row Source that take the data from the main query
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:16
Joined
Feb 19, 2002
Messages
43,223
Access isn't very good at graphic stuff. Your table also sounds like it is unnormalized. you can open a recordset and load it into an array since it sounds like it will be small. Then you have to loop through the controls for each record and look in the array for the control's value. Use that to change the color of the control. The alternative to the array is to use dlookup to the table/query holding the bad items.
 

Gasman

Enthusiastic Amateur
Local time
Today, 03:16
Joined
Sep 21, 2011
Messages
14,231
thank you
my confuse its a label not a textbox
any way i don't like to use Conditional Formatting access is not a graphical software
Yet, you are trying to use it as such? :)
 

eshai

Registered User.
Local time
Today, 05:16
Joined
Jul 14, 2015
Messages
193
Yet, you are trying to use it as such?
haha first this end user is working on 27" screen an a powerful pc
second i did some crazy stuff with access graphics
this only small part of one room a needed to edit it sorry
 

Attachments

  • צילום מסך 2022-02-27 235328.png
    צילום מסך 2022-02-27 235328.png
    13.2 KB · Views: 264

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 22:16
Joined
May 21, 2018
Messages
8,525
Highlightlabel.jpg

Code:
Private Sub List16_AfterUpdate()
  LoopSelections
End Sub

Public Sub LoopSelections()
  Dim lst As Access.ListBox
  Dim i As Integer
  Dim Val As Variant
  Set lst = Me.List16

  'Unselect all colors
  For i = 0 To lst.ListCount - 1
    Val = lst.Column(0, i)
    UnSelectedColor Val
Next i
'Reselect
For i = 0 To lst.ItemsSelected.Count - 1
    Val = lst.Column(0, lst.ItemsSelected(i))
    SelectedColor Val
  Next i

End Sub

Public Sub SelectedColor(Val As Variant)
  Me.Controls("Label" & Val).BackColor = vbRed
End Sub
Public Sub UnSelectedColor(Val As Variant)
  Me.Controls("Label" & Val).BackColor = vbGreen
End Sub
 

Attachments

  • lisboxSelectionsColors.accdb
    432 KB · Views: 240
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Yesterday, 22:16
Joined
May 21, 2018
Messages
8,525
The code is written to call from any event. Probably want to call from current event.
 

eshai

Registered User.
Local time
Today, 05:16
Joined
Jul 14, 2015
Messages
193
The code is written to call from any event. Probably want to call from current event.
Can you check what I did here?
 

Attachments

  • lisboxSelectionsColors.accdb
    476 KB · Views: 245

Users who are viewing this thread

Top Bottom