Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Disabling Controls in VBA -- SetFocus not working

Posted on 2003-03-18
Medium Priority
Last Modified: 2007-12-19
I am trying to disable sone controls on a form based on whether a checkbox in the main form is checked.  I am using the Form_Current event to disable and enable the controls on the subform accordingly.  Before disabling any controls, I set the focus to a control on the main form, using the SetFocus method.  However, I still get an error that says I can't disble a control while it has the focus.  I tried the DoCmd.GoToControl method and that didn't work either.

The Else part of the block is the problematic portion of the code...  That is where I get the error message about disabling a control that has the focus.  But, it shouldn't have the focus because I set the focus elsewhere before that block of code executes.

If curUser = "limerick" And Me.ynFieldService = True Then

    Form_sfrmFieldService.dtArrivalDate.Enabled = True
    Form_sfrmFieldService.dtArrivalDate.Locked = False
    Form_sfrmFieldService.tmArrivalTime.Enabled = True
    Form_sfrmFieldService.tmArrivalTime.Locked = False
    Form_sfrmFieldService.dtCompleteDate.Enabled = True
    Form_sfrmFieldService.dtCompleteDate.Locked = False
    Form_sfrmFieldService.tmCompleteTime.Enabled = True
    Form_sfrmFieldService.tmCompleteTime.Locked = False
    Form_sfrmFieldService.intActionCode.Enabled = True
    Form_sfrmFieldService.intActionCode.Locked = False
    Form_sfrmFieldService.intActivityCode.Enabled = True
    Form_sfrmFieldService.intActivityCode.Locked = False
    Form_sfrmFieldService.intFaultCode.Enabled = True
    Form_sfrmFieldService.intFaultCode.Locked = False

    Form_sfrmFieldService.dtArrivalDate.Enabled = False
    Form_sfrmFieldService.dtArrivalDate.Locked = True
    Form_sfrmFieldService.tmArrivalTime.Enabled = False
    Form_sfrmFieldService.tmArrivalTime.Locked = True
    Form_sfrmFieldService.dtCompleteDate.Enabled = False
    Form_sfrmFieldService.dtCompleteDate.Locked = True
    Form_sfrmFieldService.tmCompleteTime.Enabled = False
    Form_sfrmFieldService.tmCompleteTime.Locked = True
    Form_sfrmFieldService.intActionCode.Enabled = False
    Form_sfrmFieldService.intActionCode.Locked = True
    Form_sfrmFieldService.intActivityCode.Enabled = False
    Form_sfrmFieldService.intActivityCode.Locked = True
    Form_sfrmFieldService.intFaultCode.Enabled = False
    Form_sfrmFieldService.intFaultCode.Locked = True
End If
Question by:spacebaby

Expert Comment

ID: 8159466
Disable your controls on the Focus event of the cboSerialNumber control

Author Comment

ID: 8159515
The gotFocus event?  But if I disable the controls on that event, I will be diabling them at times when they shouldn't be...

Author Comment

ID: 8159533
The gotFocus event?  But if I disable the controls on that event, I will be diabling them at times when they shouldn't be...
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 8159545
The gotFocus event?  But if I disable the controls on that event, I will be diabling them at times when they shouldn't be...

Author Comment

ID: 8161762
So -- I am having the same problem with other events.  I have tried doing this in the OnLoad event.  The program still complains about not being able to disable a control that has the focus, but that control doesn't have the focus.  I even tried to set the focus to another control right before the line of code that disables the control....

What is going on with this program?


Expert Comment

ID: 8161805
try the subform's events i can't think of enything else...

Author Comment

ID: 8161942
So, I tried to set the focus to another control and then I used its GotFocus event to test and see if I could then disable the controls I wanted to, and I *still* get the same error....  

Expert Comment

ID: 8161978
try .Visible = false

Author Comment

ID: 8162042
Same problem --  essentially there is *no* way that the control in question has the focus, but I can't disable it or hide it because the program claims that it has the focus.  It doesn't.  I know it doesn't.  I even used a variable to get the name of the active control right before the enabled= false or the visible = false line executes and the active control wasn't the one I was working on.  Is this a bug in access?  Because I can't think of any other reason for my database to behave this way.
LVL 41

Accepted Solution

shanesuebsahakarn earned 800 total points
ID: 8163367
Forms and subforms maintain their own focuses.

When you set the focus to the main form, the subform still has a control in it with its "own" focus. What you will need to do is set the focus in the subform to a control that is not being disabled:
LVL 18

Expert Comment

ID: 8163372
try a different approach:

put the code on the sub-form:
on the sub form, the fields are generally open
if the the condition on the parent form is met, the fields are deactivated.

Private Sub Form_Open(Cancel As Integer)
  If Me.Parent.checkF = -1 Then GoTo Sperren
  Exit Sub
  Me.field1.Enabled = False
  Me.field1.Locked = True
End Sub

of corse this will only be sufficent, if the subform has not to respond to changes on the parent-form. in this case you will need a requery of the sub-form on change of a relevant field on the parent.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question