我的表单对于 Mircosoft Access 表单没有刷新?

问题描述 投票:0回答:1

下面是VBA代码

Option Compare Database

Private Sub Form_AfterUpdate()
        Me.Refresh
End Sub

Private Sub MedicalCondition_AfterUpdate()
    If Me.MedicalCondition = True Then
        ' If Yes is selected, enable HealthStatus and HealthDescription
        Me.HealthStatus.Enabled = True
        Me.HealthStatusDESCR.Enabled = True
    Else
        ' If No is selected, disable HealthStatus and HealthDescription
        Me.HealthStatus.Enabled = False
        Me.HealthStatusDESCR.Enabled = False
    End If

    ' Requery the form to refresh the controls
    Me.Requery
End Sub

该字段的名称是 MedicalCondition,有是/否选项。根据用户选择,它将禁用或启用 HealthStaus 和 HealthStatusDESCR 字段。如果用户选择“是”,则启用 2 个字段;如果用户选择“否”,则禁用这 2 个字段。我查看了命名字段,没有发现任何错误;是代码吗?请帮忙。

vba ms-access ms-access-2016 ms-access-forms
1个回答
0
投票

您不需要

Requery
Refresh
来启用/禁用控件。删除所有这些。

如果您的组合框具有“是”和“否”作为值,则无法测试

True

尝试

Private Sub MedicalCondition_AfterUpdate()

    ' Ctrl+G opens Immediate Window where this goes:
    Debug.Print "MedicalCondition: " & Me.MedicalCondition.Value

    If Me.MedicalCondition.Value = "Yes" Then

如果“是”不起作用,请检查 Debug.Print 输出。

您可以使用变量简化代码:

Private Sub MedicalCondition_AfterUpdate()

    Dim bMedicalCondition As Boolean
    bMedicalCondition = (Me.MedicalCondition.Value = "Yes")

    ' No need for If Then Else:
    Me.HealthStatus.Enabled = bMedicalCondition 
    Me.HealthStatusDESCR.Enabled = bMedicalCondition 

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.