0

我正在研究访问 2010 数据库。我对此很陌生,对VBA知之甚少。

我有一个表单(基于查询),在文本框中显示某些项目的测试频率。它被命名为“TestFrequency”

然后我有 3 个组合框(分别命名为: Year1 、 Year2 和 Year3 )。

我添加了一个按钮并在“Onclick”中创建了一个事件,我试图做的是根据测试频率值设置组合框的值。我还添加了一些消息框以在事件完成时弹出。

我尝试使用以下代码但无法正常工作:

Private Sub Reset_Click()
Dim Response As VbMsgBoxResult
 Response = MsgBox("Do you want to reset planning to default test frequency?", vbQuestion + vbYesNo, "Planning Settings")
If Response = vbNo Then Exit Sub Else
Dim ctrl AS Controls
For Each ctrl From Me.Controls
 If TestFrequency.Value = "Test Annually" Then
   Me.Year1.Value = "Yes"
   Me.Year2.Value = "Yes"
   Me.Year3.Value = "Yes"
   ElseIf TestFrequency.Value = "Test Every 2 years" Then
   Me.Year1.Value = "No"
   Me.Year2.Value = "Yes"
   Me.Year3.Value = "No"
   ElseIf TestFrequency.Value = "Test every 3 years" Then
   Me.Year1.Value = "No"
   Me.Year2.Value = "No"
   Me.Year3.Value = "Yes"
   ElseIf TestFrequency.Value = "Ad-hoc" Then
   Me.Year1.Value = "No"
   Me.Year2.Value = "No"
   Me.Year3.Value = "No"
   End If
Next ctrl
  MsgBox "Settings were changed.", vbInformation
 End If
End Sub

有人能帮帮我吗?谢谢!

4

1 回答 1

0

如果列表顶部是“是”,下一个是“否”,那么试试这个(ComboBox 用于.ListIndex选择):

Private Sub Reset_Click()
    If vbNo = MsgBox("Do you want to reset planning to default test frequency?", vbQuestion + vbYesNo, "Planning Settings") Then Exit Sub
    Select Case TestFrequency.Value
        Case "Test Annually"
            Year1.ListIndex = 0
            Year2.ListIndex = 0
            Year3.ListIndex = 0
        Case "Test Every 2 years"
            Year1.ListIndex = 1
            Year2.ListIndex = 0
            Year3.ListIndex = 1
        Case "Test every 3 years"
            Year1.ListIndex = 1
            Year2.ListIndex = 1
            Year3.ListIndex = 0
        Case "Ad-hoc"
            Year1.ListIndex = 1
            Year2.ListIndex = 1
            Year3.ListIndex = 1
    End Select
    If Err.Number = 0 Then MsgBox "Settings were changed.", vbInformation
End Sub
于 2013-09-11T07:16:32.887 回答