I am working on creating a database for the Aquaculture sector. In a Form I have placed buttons for each "pool" that exists on place:
So you get an idea, the buttons have following code that forwards to another Form with specific information about given pool:
Private Sub cmd2_Click()
On Error GoTo cmd2_Click_Err
codBaseNum = "AT 1/2"
DoCmd.OpenForm "InfoBotonTanque", acNormal, "", "[Base-Número]=""AT 1/2""", , acLast
cmd2_Click_Exit:
Exit Sub
cmd2_Click_Err:
MsgBox Error$
Resume cmd2_Click_Exit
End Sub
Based on two different checkboxes on a Table with information about individual pools, I want the buttons to change color. Here the data from the Table:
Checkbox names: [Reserva] and [Condicion?]. [Base-Número] is the pools ID. All three values are in the Form's RecordSource:
EDIT: Managed to acomplish what I wanted: buttons change color based on CheckBox values! The following code is not ellegant, for I have to type the same with small changes for each button and there are about 150 of them, but it works... The used code is:
Private Sub Form_Load()
If DLookup("[Reserva]", "Tanques", "[Base-Número] = 'AT 1/2'") = True And DLookup("[Condicion?]", "Tanques", "[Base-Número] = 'AT 1/2'") = False Then
Me.cmd2.BackColor = RGB(255, 215, 0)
ElseIf DLookup("[Reserva]", "Tanques", "[Base-Número] = 'AT 1/2'") = False And DLookup("[Condicion?]", "Tanques", "[Base-Número] = 'AT 1/2'") = True Then
Me.cmd2.BackColor = RGB(255, 0, 0)
ElseIf DLookup("[Reserva]", "Tanques", "[Base-Número] = 'AT 1/2'") = False And DLookup("[Condicion?]", "Tanques", "[Base-Número] = 'AT 1/2'") = False Then
Me.cmd2.BackColor = RGB(51, 171, 249)
Else
Me.cmd2.BackColor = RGB(120, 120, 120)
End If
End Sub
In order not to repeat code 150 times, use a loop that constructs command button name as well as [Base-Número] parameter with a variable. Consider:
Private Sub Form_Load()
Dim x As Integer
For x = 1 to 150
If DLookup("[Reserva]", "Tanques", "[Base-Número] = 'AT 1/" & x & "'") = True _
And DLookup("[Condicion?]", "Tanques", "[Base-Número] = 'AT 1/" & x & "'") = False Then
Me("cmd" & x ).BackColor = RGB(255, 215, 0)
ElseIf DLookup("[Reserva]", "Tanques", "[Base-Número] = 'AT 1/" & x & "'") = False _
And DLookup("[Condicion?]", "Tanques", "[Base-Número] = 'AT 1/" & x & "'") = True Then
Me("cmd" & x).BackColor = RGB(255, 0, 0)
ElseIf DLookup("[Reserva]", "Tanques", "[Base-Número] = 'AT 1/" & x & "'") = False _
And DLookup("[Condicion?]", "Tanques", "[Base-Número] = 'AT 1/" & x & "'") = False Then
Me("cmd" & x).BackColor = RGB(51, 171, 249)
Else
Me("cmd" & x).BackColor = RGB(120, 120, 120)
End If
Next
End Sub