UNLOCK BUTTON ERROR - Run-time error 2164 You can’t disable a control while it has the focus

Dear Sir

I have form “Rcvd_Main_1” and subform “Rcvd_detail1_subform_1”.

The field of subforms are:

S_P_ID (Combo box to select the items),

STATUS(combo box to select IN or out)

O_BAL1: (Opening balance)

QTY_IN: (Rcvd qty)

QTY_OUT: (Issue qty)

C_BAL: (Closing balance)

I have changed the control property of subform from Enable to No and Locked to Yes.

When I open the main form the fields are locked.

I have placed two button LOCK and UNLOCK on main form. When I click on UNLOCK it changes the properties of Enable to Yes and Locked to, to feed the inputs. But when I click on UNLOCK the message “Run-time error 2164 You can’t disable a control while it has the focus”

In combo box there is lost focus or get focus I placed. The codes on LOCK button is:

Private Sub txtPassword_AfterUpdate()

Const MESSAGETEXT = "You are not allowed access to the ''""Special Form""'"

Dim Frm As SubForm

Dim ctrl As Control

Set Frm = [Forms]![Rcvd_Main_1]![RCVD_DETAIL1_subform1]

If Me.txtPassword = "Farooq" Then

For Each ctrl In Frm.Controls

        If ctrl.Tag = "EditMe" Then

        ctrl.Enabled = False

            ctrl.Locked = True

        End If

    Next ctrl

    DoCmd.Close acForm, Me.Name

Else

   MsgBox "Incorrect Password!" & vbCrLf & vbCrLf & MESSAGETEXT, vbCritical, "Invalid Password"

 DoCmd.Close acForm, Me.Name

End If

End Sub

Please advise how to handle this error to resolve.

Regards

Muhammad Farooq

You'll first need to ensure that a control which is not to be locked/disabled has focus.  This could be an unbound control in the form such as a command button or similar:

If Me.txtPassword = "Farooq" Then
    Frm.Controls("SomeControlName").SetFocus
    For Each ctrl In Frm.Controls
_____________________
Ken Sheridan,
Newport, Shropshire, England

"Don't write it down until you understand it!" - Richard Feynman

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Dear Ken


Thanks for your reply and sorry for late replying from myside.


There are only two filed which are combox


S_P_ID (Combo box to select the items), [I select name of item]

Name: Combo28

Control Source: S_P_ID

I want to block this because if it is unlock, anyone can change


STATUS(combo box to select IN or out)

Name: Combo50

Control Source: STATUS

I want to block this because if it is unlock, anyone can change


I put the "Combo28 and combo 50 in the field you provided [Frm.Controls("SomeControlName").SetFocus] but it is still creating problem /


Please help as I could not catch your idea properly.


Please


Rgds


Muhammad Farooq




Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

You'll need another control in the form, which can be left enabled.  I'd suggest adding something like a 'Close Form' button, which the button wizard can easily create, and set the focus to that.
_____________________
Ken Sheridan,
Newport, Shropshire, England

"Don't write it down until you understand it!" - Richard Feynman

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I placed a CLOSEFORM button on main form. It works but when I click in subform and work and after than when I click on lock button it doesnt work /means there is error "Run-time error 2164 You can’t disable a control while it has the focus”

The code is as follows

Dim Frm As SubForm
Dim Frm1 As Form

Dim ctrl As Control

Set Frm = [Forms]![Rcvd_Main_1]![RCVD_DETAIL1_subform1]
Set Frm1 = [Forms]![Rcvd_Main_1]

If Me.txtPassword = "Farooq" Then

Frm1.Controls("Closeform").SetFocus

For Each ctrl In Frm.Controls
        If ctrl.Tag = "EditMe" Then
        ctrl.Enabled = False
            ctrl.Locked = True
        End If
    Next ctrl

Regards

Muhamamd Farooq

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

At first sight I can't see why you are experiencing that behaviour.  Have you debugged the code?  I'd suggest setting a breakpoint on the If Me.txtPassword = "Farooq" Then line and then stepping into the code line by line after clicking the lock button.
_____________________
Ken Sheridan,
Newport, Shropshire, England

"Don't write it down until you understand it!" - Richard Feynman

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Thanks for sparing time for my queries to solve. The main problem is in "ctrl.Enabled = False" so I have changed / deleted and only kept lock and combo are visible but can not be changed by anyone least to put password.

Reiterate thanks

Rgds,

Muhammad Farooq

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 759 Applies to: