如果存在记录,则启用控件

时间:2016-07-26 11:11:19

标签: ms-access combobox access-vba

我有一个数据库,参与者回答了3个调查(1,2和3),我正在尝试创建一个表单,用于确定特定参与者(ID)已完成哪些调查(如果有),然后禁用现有记录的按钮(添加调查1,添加调查2和添加调查3)。

表格(Surveys)表格被链接到具有字段ID和Survey_Number(1到3)。在这里,一个参与者(ID)可以有0到3个条目,具体取决于输入的调查数量。

我目前的工作非常不稳定:我使用了级联的Combobox方法,其中第二个Combobox用作临时数组(我为此道歉 - 我不编码)来保存Survey_Number的ID值(在第一个Combobox中。

Private Sub Combo29_AfterUpdate()
Dim i As Integer

 If Not IsNull(Me.Combo29) Then
  If DCount("ID", "Surveys", "[ID] =" & Me.Combo29) > 0 Then
    Combo2.RowSource = "SELECT Surveys.Survey_Number " & _
                       "FROM Surveys " & _
                       "WHERE Surveys.ID = " & Combo29.Value
Me.Combo2.Requery

i = 0
For i = 0 To (Me.Combo2.ListCount - 1)

Select Case Me.Combo2.ItemData(i)
Case "1"
Me!Command34.Enabled = False
Case "2"
Me!Command35.Enabled = False
Case "3"
Me!Command36.Enabled = False
End Select
i = i + 1
Next
  Else
    MsgBox "This is the first time this participant has responded"
    Me!Command34.Enabled = True
    Me!Command35.Enabled = True
    Me!Command36.Enabled = True
    Me.Combo2 = Me.Combo2.ItemData(0)
  End If
 End If

End Sub

当没有条目(启用所有按钮)但是当存在一些/一个调查时它没有正确读取ItemData(1),这很有效。如果输入了1,2和3,则仅禁用按钮2和3。

2 个答案:

答案 0 :(得分:1)

我不确定Combo2有多么有用,但是,使用你已有的控件,这应该可行:

Private Sub Combo29_AfterUpdate()

Dim i As Integer

Me!Command34.Enabled = True
Me!Command35.Enabled = True
Me!Command36.Enabled = True

If Not IsNull(Me.Combo29) Then
    If DCount("Survey_Number", "Surveys", "[ID] =" & Me.Combo29) > 0 Then
        Combo2.RowSource = "SELECT Surveys.Survey_Number " & _
                           "FROM Surveys " & _
                           "WHERE Surveys.ID = " & Combo29.Value
        Me.Combo2.Requery

        With Combo2
            For i = 0 To .ListCount - 1
                Select Case .ItemData(i)
                    Case 1
                        Me!Command34.Enabled = False
                    Case 2
                        Me!Command35.Enabled = False
                    Case 3
                        Me!Command36.Enabled = False
                    Case Else
                        'Nothing
                End Select
            Next i
        End With
    Else
        Me!Command34.Enabled = True
        Me!Command35.Enabled = True
        Me!Command36.Enabled = True
    End If

End If

End Sub

答案 1 :(得分:0)

使用Form的当前事件的启用/禁用代码。当您将焦点更改为任何其他记录时,可能需要更改启用/禁用控件。您的启用/禁用代码仅适用于更新后的组合。我们无法启用/禁用所选行控制,它会启用/禁用列的所有控件。