How to program separate items in a combobox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ArrogantZebra
    New Member
    • Aug 2016
    • 1

    How to program separate items in a combobox?

    So i need to add a button that saves and resets for each different item in a combobox.
    for example an attendance roll
    (Name1 Name2 Name3 are combobox items)

    when Name 1 is selected in the combobox a button appears and can be clicked to change colour between red and green (red = absent, green = attending)

    after name1 button has been changed to the correct colour the user changes the selected item to Name2 and does the same.

    However, if the user selects the item Name1 again the button will change to the colour selected when the Name1 was originally selected.

    *Edit: the string for Name1, Name2 and Name3 is inputted by the user so this will not work.
    Code:
    If ComboBox1.text = "Name1" then
    Button1.Visible = True
    Last edited by ArrogantZebra; Aug 19 '16, 07:54 AM. Reason: More Information
  • PhilOfWalton
    Recognized Expert Top Contributor
    • Mar 2016
    • 1430

    #2
    I think there may be a problem with your logic. To my mind, a person can be either absent, attending or don't know.
    So your table should use a byte (value 0,1 & 2) rather than a Yes/No field. I call it Box in my table.

    Here is a very crude form


    Only the Box field is bound

    This is the code
    Code:
    Option Compare Database
    Option Explicit
    
        Dim BoxRelay As Byte
    
    Private Sub ChangeBox()
    
        Box = BoxRelay
        
        DoCmd.RunCommand acCmdSaveRecord
        
        Form_Current
        
    End Sub
    
    Private Sub DontNoBox_AfterUpdate()
    
        If DontNoBox = True Then
            YesBox = False
            NoBox = False
        End If
    
        Box = 0
        
        BoxRelay = Box
        ChangeBox
        
    End Sub
    
    Private Sub Form_Current()
        
        If Box = 1 Then
            Button.BackColor = vbGreen
        ElseIf Box = 2 Then
            Button.BackColor = vbRed
        ElseIf Box = 0 Then
            Button.BackColor = vbYellow
        End If
        
    End Sub
    
    Private Sub NoBox_AfterUpdate()
    
        If NoBox = True Then
            YesBox = False
            DontNoBox = False
        End If
    
        Box = 2
        BoxRelay = Box
        ChangeBox
        
    End Sub
    
    Private Sub YesBox_AfterUpdate()
    
        If YesBox = True Then
            NoBox = False
            DontNoBox = False
        End If
        
        Box = 1
        BoxRelay = Box
        
        ChangeBox
        
    End Sub
    So clicking one of the 3 tick boxes changes and saves the value of the bound Box field.

    The colour of the button is related to the value of Box

    Phil

    Comment

    Working...