Ms access 将代码应用于多个控制字段

Ms access 将代码应用于多个控制字段,ms-access,Ms Access,我正在使用下面的代码来更改组合框和标签的属性。我还有40个组合框和标签(combo2、combo3、combo4………)。有没有一种方法可以重复使用代码,而不是复制代码并手动更改每个组合框和标签的名称 If (Combo1 = 1) Then DoCmd.SetProperty "Combo1", acPropertyBackColor, "255" DoCmd.SetProperty "Label1", acPropertyCaption, "POOR" ElseIf (Com

我正在使用下面的代码来更改组合框和标签的属性。我还有40个组合框和标签(combo2、combo3、combo4………)。有没有一种方法可以重复使用代码,而不是复制代码并手动更改每个组合框和标签的名称

If (Combo1 = 1) Then
    DoCmd.SetProperty "Combo1", acPropertyBackColor, "255"
    DoCmd.SetProperty "Label1", acPropertyCaption, "POOR"
ElseIf (Combo1 = 2) Then
    DoCmd.SetProperty "Combo1", acPropertyBackColor, "2895086"
    DoCmd.SetProperty "Label1", acPropertyCaption, "FAIR"
ElseIf (Combo1 = 3) Then
    DoCmd.SetProperty "Combo1", acPropertyBackColor, "35584"
    DoCmd.SetProperty "Label1", acPropertyCaption, "GOOD"
ElseIf (Combo1 = 4) Then
    DoCmd.SetProperty "Combo1", acPropertyBackColor, "52480"
    DoCmd.SetProperty "Label1", acPropertyCaption, "VERY GOOD"
ElseIf (Combo1 = 5) Then
    DoCmd.SetProperty "Combo1", acPropertyBackColor, "64636"
    DoCmd.SetProperty "Label1", acPropertyCaption, "EXCELLENT"
Else
    DoCmd.SetProperty "Combo1", acPropertyBackColor, "16579836"
    DoCmd.SetProperty "Label1", acPropertyCaption, ","
End If

您可以创建一个子过程,该子过程接收combo1整数值以及ComboBox和Listbox控件作为参数。该过程如下所示:

Public Sub SetupProperties(combo1 As Integer, ChangeComboBox As Access.ComboBox, ChangeLabel As Access.Label)

Dim longBackColour As Long
Dim stringCaption As String
' set up  background colour and caption
If (combo1 = 1) Then
    longBackColour = 255
    stringCaption = "POOR"
ElseIf (combo1 = 2) Then
    longBackColour = 2895086
    stringCaption = "FAIR"
ElseIf (combo1 = 3) Then ' and so on...

End If
' Change properties of the controls
ChangeComboBox.BackColor = longBackColour
ChangeLabel.Caption = stringCaption
端接头

然后从循环中调用该过程,如下所示:

Dim combo1 As Integer
combo1 = 1
Dim integerCounter As Integer
For integerCounter = 1 To 40
    SetupProperties combo1, Me.Controls("Combo" & Trim(CStr(integerCounter))), Me.Controls("Label" & Trim(CStr(integerCounter)))
Next