My form is not refreshing for Mircosoft Access forms?

70 Views Asked by At

Below is the VBA code

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

The name of the field is MedicalCondition which has a yes/no option. Upon users selection it will disable or enable fields of HealthStaus and HealthStatusDESCR. If user select Yes enable the 2 fields; If user select No disable the 2 fields. I have looked at the naming fields and I cant see any errors; Is it the code ? Please help.

Image for the fields

1

There are 1 best solutions below

3
Andre On BEST ANSWER

You don't need Requery or Refresh to enable/disable controls. Remove all that.

If your combobox has "Yes" and "No" as values, you can't test for True.

Try

Private Sub MedicalCondition_AfterUpdate()

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

    If Me.MedicalCondition.Value = "Yes" Then

If "Yes" doesn't work, check the Debug.Print output.

You can simplify your code with a variable:

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