(Access, VBA) 根据位于表单记录源上的 Table 中的 CheckBox 值更改表单上按钮的颜色
(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
我正在为水产养殖部门创建一个数据库。在一个表格中,我为每个存在的“池”放置了按钮:
所以你明白了,按钮有以下代码转发到另一个表单,其中包含关于给定池的特定信息:
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
基于 Table 上的两个不同复选框以及有关各个池的信息,我希望按钮更改颜色。这里的数据来自 Table:
复选框名称:[Reserva] 和 [Condicion?]。 [Base-Número] 是池 ID。所有三个值都在表单的 RecordSource 中:
编辑:
设法完成我想要的:按钮根据 CheckBox 值更改颜色!下面的代码并不优雅,因为我必须为每个按钮键入相同的内容并进行小的更改,其中大约有 150 个,但它有效......使用的代码是:
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
为了不重复代码 150 次,使用循环构造命令按钮名称以及带有变量的 [Base-Número] 参数。考虑:
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
我正在为水产养殖部门创建一个数据库。在一个表格中,我为每个存在的“池”放置了按钮:
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
基于 Table 上的两个不同复选框以及有关各个池的信息,我希望按钮更改颜色。这里的数据来自 Table:
复选框名称:[Reserva] 和 [Condicion?]。 [Base-Número] 是池 ID。所有三个值都在表单的 RecordSource 中:
编辑: 设法完成我想要的:按钮根据 CheckBox 值更改颜色!下面的代码并不优雅,因为我必须为每个按钮键入相同的内容并进行小的更改,其中大约有 150 个,但它有效......使用的代码是:
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
为了不重复代码 150 次,使用循环构造命令按钮名称以及带有变量的 [Base-Número] 参数。考虑:
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