Combobox (1 Viewer)

romanlo

Member
Local time
Today, 20:06
Joined
Feb 24, 2020
Messages
38
Hi there
I am trying to use combox with selection the right label.
When I select a name from the combobox called "choose" I would like to change the colour just the label where record is assinged to. When I select lets say name1 then the label nr1 is colored.
THis what I have is changing me all labels
Code:
Private Sub choose_AfterUpdate()
    Dim lbl As Access.Control
    Dim msg, title, response
    Dim criteria As String
    
    criteria = "id_tomb =" & Me.choose.Value
    
    MsgBox (criteria)
    
    
    msg = "Wpis nieodnaleziony"
    title = "ATTENTION!!!!!"
    
    
  For Each lbl In Me.Controls 'dla każdej zmiennej w zestawie kotrolek tego formualrza
 
    If TypeOf lbl Is Label Then
        lbl.OnClick = "=LabelClick(" & Mid(lbl.Name, Len(LABEL_PREFIX) + 1) & ")"
      
    End If
    
  Next
    
With Me.RecordsetClone
    .MoveFirst
    .FindFirst criteria
      
    
If idx >= 1 Then
    If lColour >= 1 Then
      If Not Me(LABEL_PREFIX & idx).BackColor = lColour Then Me(LABEL_PREFIX & idx).BackColor = lColour
    
    
      If Not Me(LABEL_PREFIX & idx).BackStyle = 1 Then Me(LABEL_PREFIX & idx).BackStyle = 1
      
      
    Else
      If Not Me(LABEL_PREFIX & idx).BackStyle = 0 Then Me(LABEL_PREFIX & idx).BackStyle = 0
      Me(LABEL_PREFIX & idx).BackStyle = 0
    
      
    End If
    
    Call LblClickGoToRecord(idx) 'wywołanie funkcji gotorecord
    
  Else
      For Each lbl In Me.Controls
     If lbl.ControlType = acLabel Then
      If lColour >= 0 Then
        If Not lbl.BackColor = lColour Then lbl.BackColor = lColour
       If Not lbl.BackStyle = 1 Then lbl.BackStyle = 1
      Else
        If Not lbl.BackStyle = 0 Then lbl.BackStyle = 0
      End If
    End If
    
          
    Next
 End If
 
 
 
   If .NoMatch Then
      Beep
      response = MsgBox(msg, vbOKOnly, title)
   Else
      Me.Bookmark = .Bookmark
   End If
End With
''GoTo Finish

''Errhand1:
''MsgBox Err.Number & " " & Err.Description
''Resume Next
End Sub
 

Ranman256

Well-known member
Local time
Today, 15:06
Joined
Apr 9, 2015
Messages
4,339
no need for a loop.

Code:
if isnull(cboBox) then
   lblLabl.backcolor = vbRed
else
   lblLabl.backcolor = vbGreen
endif
 

romanlo

Member
Local time
Today, 20:06
Joined
Feb 24, 2020
Messages
38
it gives me run-timer error:91
object variable not set
Code:
 If IsNull(Me.choose) Then
    lbl.BackColor = vbRed
Else
    lbl.BackColor = vbGreen
End If
 

Ranman256

Well-known member
Local time
Today, 15:06
Joined
Apr 9, 2015
Messages
4,339
it does work if:
lbl is the actual label name
and choose is the actual combo name.

if you are using the loop, your Typename function is wrong:

if TypeName (lbl) = "Label" then
lbl.backcolor = vbRed
 

romanlo

Member
Local time
Today, 20:06
Joined
Feb 24, 2020
Messages
38
Dear Ranman.
I am not using the loop but still get the error: object variable not set. the lbl is a control type
 

Micron

AWF VIP
Local time
Today, 15:06
Joined
Oct 20, 2018
Messages
3,476
I am not using the loop but still get the error: object variable not set.
Probably because you tried exactly what was posted rather than incorporating it into your existing code?
If you only want to colour the label of one field, why loop through all of them? Just colour the one directly. Which one is that (which one is
"the label where record is assinged to" ?
 

romanlo

Member
Local time
Today, 20:06
Joined
Feb 24, 2020
Messages
38
Guys I know I am very new and I am trying to manage. sometimes making some silly mistakes.
I am placing some code which is working only when I firstly press the label called,"Etykieta". However I need such functionality without clicking that label. Only when I select the name from the combox I would expect the label color change. this must be in loop because the data are assigned to each label
Code:
Private Sub choose_AfterUpdate()
    Dim msg, title, response
    Dim criteria As String
    
    criteria = "id_tomb =" & Me.choose.Value
    
      
    MsgBox (criteria)
    
    
    msg = "Wpis nieodnaleziony"
    title = "ATTENTION!!!!!"
    
        
With Me.RecordsetClone
    .MoveFirst
    .FindFirst criteria
    
 'START
   Dim i As Integer
  
   For i = 1 To 10 Step 1

 If IsNull(Me.choose) Then
 Me.Controls("Etykieta" & i).BackColor = vbRed
 
Else
     Me.Controls("Etykieta" & i).BackColor = vbGreen ' is is working only when I first press the "Etykieta" on the form
  
End If

 Next i
 'koniec
   If .NoMatch Then
      Beep
      response = MsgBox(msg, vbOKOnly, title)
   Else
      Me.Bookmark = .Bookmark
   End If
End With


End Sub
 

Micron

AWF VIP
Local time
Today, 15:06
Joined
Oct 20, 2018
Messages
3,476
I think language is part of the problem, but you being a novice is not a problem. We were all beginners at one time too. Maybe if you wrote what you want to say in Polish and translate with Google then post the translation here it might help. For me, part of the problem is thing you say, like labels having data because they don't. So if you choose name1 what does the data in the bound column of that combo really look like? What does the name of the corresponding label that you want to colour look like? What's the important part of that match,? Is it because the combo value ends in 1 and so does the label? These are the sort of details that are missing from your posts.
Based on this
When I select lets say name1 then the label nr1 is colored.
I came up with my interpretation of what you want to do, but it might not even be close.

Code:
Private Sub choose_AfterUpdate()
Dim ctl As Control

For Each ctl in Me.Controls
  If ctl.ControlType = acTextbox And Instr(ctl.Name,choose) > 0 Then
    ctl.Controls(0).BackColor = vbRed
  Else
    ctl.Controls(0).BackColor = vbGreen
  End If
Next

End Sub
 

romanlo

Member
Local time
Today, 20:06
Joined
Feb 24, 2020
Messages
38
Thank you Micron for your comments. Right I need to clarify what I really want.
Ok.
I have a form. The form is bound to the table where I am taking data from.
It looks like a graveyard. On the form there are some labels coreposnding to each tomb. When I press label1 in the text boxes shows me the right records from the table and the label1 changes the colour to yellow. When I press label2 the second record from the table is presented in textboxes and the label1 changes the colour.
It works fine.
I am also using combo box to find a name in the database. When I find and select the record is shown in the boxes.
What I want.
When I select first record via combobox I want label1 change the colour and the record is shown in textboxes. And that's it.
I hope more clear.
 

Micron

AWF VIP
Local time
Today, 15:06
Joined
Oct 20, 2018
Messages
3,476
Well, it helps a bit but not enough. Usually there is no connection between any particular control (label or anything else) that can be directly tied to a record, which is how I understand your post. I'm having trouble conceiving that. Can you post a db copy? I think that after this many posts it's about the only way I or anyone else will be able to figure this out.
 

romanlo

Member
Local time
Today, 20:06
Joined
Feb 24, 2020
Messages
38
here u are. You can see the table I am using to get the data from
 

Attachments

  • testowa.zip
    47.4 KB · Views: 92

Micron

AWF VIP
Local time
Today, 15:06
Joined
Oct 20, 2018
Messages
3,476
just the table is not much use. Need the form(s) with the code you've tried :confused:?
 

romanlo

Member
Local time
Today, 20:06
Joined
Feb 24, 2020
Messages
38
Sorry understood db as a database.
 

Attachments

  • LCT_v1_26.02.2020_NEW.zip
    714.5 KB · Views: 104

Micron

AWF VIP
Local time
Today, 15:06
Joined
Oct 20, 2018
Messages
3,476
Well it does - but that includes whatever db contains the form(s) so that we have something to work with. :)
can take a look later if someone doesn't beat me to it.
 

Users who are viewing this thread

Top Bottom