Dyann
Dyann

Reputation: 317

ACCESS : VBA Code Button Visibility not working

I have a problem in hiding my buttons and not working mostly is when the button itself was clicked, it supposed to be hidden. I have a code below from one of my buttons but most of the button visibility codes are not working. I tried to add:

DoEvents

but still don't work. I don't know what the problem is. Some Enable properties were working but some are not.

Private Sub cmdSave_Click()

If MsgBox("Are you sure to save record?", vbYesNo, "Save Record") = vbYes Then

DoEvents

    imgPic.Enabled = False
    txtLRN.Enabled = False
    txtLN.Enabled = False
    txtFN.Enabled = False
    txtMN.Enabled = False
    txtS.Enabled = False
    txtA.Enabled = False
    txtBD.Enabled = False
    txtBP.Enabled = False
    txtMT.Enabled = False
    txtIP.Enabled = False
    txtRG.Enabled = False
    txtAH.Enabled = False
    txtAB.Enabled = False
    txtAM.Enabled = False
    txtAP.Enabled = False
    txtF.Enabled = False
    txtM.Enabled = False
    txtGN.Enabled = False
    txtGR.Enabled = False
    txtCN.Enabled = False
    txtR.Enabled = False

DoEvents
    cmdSearch.Visible = True
    cmdFirst.Visible = True
    cmdPrev.Visible = True
    cmdNext.Visible = True
    cmdNext.Visible = True
    cmdLast.Visible = True
    cmdNew.Visible = True
    cmdDelete.Visible = True
    cmdEdit.Visible = True


DoEvents
    cmdSave.Visible = False
    cmdCancel.Visible = False

DoEvents

End if

End Sub

Upvotes: 1

Views: 3043

Answers (2)

HansUp
HansUp

Reputation: 97101

This simplified version of your procedure will trigger error #2165, "You can't hide a control that has the focus."

Private Sub cmdSave_Click()
    Me.cmdSave.Visible = False
End Sub

During cmdSave_Click, cmdSave has focus, so you can't hide it without first moving focus to another unhidden control. This version will avoid that error:

Private Sub cmdSave_Click()
    Me.cmdSearch.Visible = True
    Me.cmdSearch.SetFocus
    Me.cmdSave.Visible = False
End Sub

Upvotes: 2

Mike Bradley
Mike Bradley

Reputation: 115

In similar cases, I've found that you need to specify the form reference ("Me.").

So try (for example)

    Me.cmdCancel.Visible = True

In other words specify "Me." in front of the control name.

Upvotes: 0

Related Questions