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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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...
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  


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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

765 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