2013-02-11 33 views
1

我正在使用以下代碼的代碼更改組合框和標籤的屬性。我還有另外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 

回答

0

您可以創建一個子過程,接收作爲參數的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 
相關問題