Vba code to display the department names on diff labels

aman

Registered User.
Local time
Today, 00:52
Joined
Oct 16, 2008
Messages
1,251
Hi Guys

I have a combobox with 10 departments. On the form there are 10 labels as well.My requirement is when I select a dept name from the combobox then it should display that on label1 and for the rest of the labels, all other departments should be displayed.
Now suppose I select another department from the combobox then again on label1 the current selected department name should be displayed and the rest of the labels would display the other 9 departments.

Can anyone please help me with the vba code for this please.I have written the following code in excel/vba but combobox list property is not working in access/vba.
Code:
Private Sub Combo8_LostFocus()
Dim lngIndex As Long
    Dim lngControlIndex As Long
    
    lngControlIndex = 18
    For lngIndex = 0 To Combo8.ListCount - 1
        If lngIndex = Combo8.ListIndex Then
            Me.Controls("Label18").Caption = Combo8.List(lngIndex)
        Else
            lngControlIndex = lngControlIndex + 1
            Me.Controls("Label" & lngControlIndex).Caption = Combo8.List(lngIndex)
        End If
    Next
End Sub

Regards
AMan
 
sorry about that. try this:
Code:
      if me.combo.column(0, introw) = me.combo then
         goto NextLoop
      else
         me.controls("label" & cstr(integerCOUNTERvariable)).caption = _
         me.combo.column(0, introw)
     end if

NextLoop:
   next introw
 
Last edited:
Hi

Thanks for your reply.But I am getting an error on the line ' goto next introw

Can you please help me

Thanks
AMan
 
sorry about that. try this:
Code:
      if me.combo.column(0, introw) = me.combo then
         goto NextLoop
      else
         me.controls("label" & cstr(integerCOUNTERvariable)).caption = _
         me.combo.column(0, introw)
     end if

NextLoop:
   next introw
 
Hi

Sorry but its not working.I have written the following code but its giving me an error next without for.Also, I have a combobox with one column only.
Code:
Private Sub Combo8_LostFocus()
Dim str As String
Me.Label18.Caption = Me.Combo8
   For introw = 0 To Me.Combo8.ListCount - 1
      If Me.Combo8.Column(0, introw) = Me.Combo8 Then
         
         GoTo NextLoop
      
      Else
         Me.Controls("label" & CStr(integerCOUNTERvariable)).Caption = _
         Me.Combo8.Column(0, introw)
     End If
   Next introw
NextLoop:
   Next introw
    
End Sub

Regards
AMan
 
....................
Hi

Sorry but its not working.I have written the following code but its giving me an error next without for.Also, I have a combobox with one column only.
Code:
Private Sub Combo8_LostFocus()
Dim str As String
Me.Label18.Caption = Me.Combo8
   For introw = 0 To Me.Combo8.ListCount - 1
      If Me.Combo8.Column(0, introw) = Me.Combo8 Then
         
         GoTo NextLoop
      
      Else
         Me.Controls("label" & CStr(integerCOUNTERvariable)).Caption = _
         Me.Combo8.Column(0, introw)
     End If
   Next introw [COLOR="Red"]<----TAKE THIS OUT!  too many NEXT's in this place!  ;)[/COLOR]
NextLoop:
   Next introw
    
End Sub

Regards
AMan
 
Thanks for your reply Ajet but its not giving me the right answer.

In my form, I have 10 labels namely label18,19,20......27. and 10 depts A,B,C...J

Initially Label1 refers to A
Label2 refers to B
Label3 refers to C
......
Label27 refers to J

now with this code If I select C from combobox then it appears on Label18 which is fine but on Label3,the dept C also appears. So it appears twice.
But I want to display these only once in all the labels.

Please help me me how to figure it out .
Regards
AMan
 
Thanks a lot for that.It works a treat...:)
 

Users who are viewing this thread

Back
Top Bottom