[英](Access, VBA) Changing the color of a button on a Form based on CheckBox values from a Table located on the Forms's Record Source
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.基于 Table 上的两个不同复选框以及有关各个池的信息,我希望按钮更改颜色。 Here the data from the Table:
这里是表中的数据:
Checkbox names: [Reserva] and [Condicion?].复选框名称:[Reserva] 和 [Condicion?]。 [Base-Número] is the pools ID.
[Base-Número] 是池 ID。 All three values are in the Form's RecordSource:
所有三个值都在表单的 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.编辑:设法完成我想要的:按钮根据 CheckBox 值更改颜色,以下代码并不优雅,因为我必须为每个按钮输入相同的内容,并且大约有 150 个。 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.为了不重复代码 150 次,请使用构造命令按钮名称的循环以及带有变量的 [Base-Número] 参数。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.