Link to home
Start Free TrialLog in
Avatar of spacebaby
spacebaby

asked on

Disabling Controls in VBA -- SetFocus not working

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

    Me.cboSerialNumber.SetFocus
   
    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
Else

    Me.cboSerialNumber.SetFocus
   
    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
Avatar of a_ro_no
a_ro_no
Flag of Greece image

Disable your controls on the Focus event of the cboSerialNumber control
Avatar of spacebaby
spacebaby

ASKER

The gotFocus event?  But if I disable the controls on that event, I will be diabling them at times when they shouldn't be...
The gotFocus event?  But if I disable the controls on that event, I will be diabling them at times when they shouldn't be...
The gotFocus event?  But if I disable the controls on that event, I will be diabling them at times when they shouldn't be...
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?

try the subform's events i can't think of enything else...
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....  
try .Visible = false
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.
ASKER CERTIFIED SOLUTION
Avatar of shanesuebsahakarn
shanesuebsahakarn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Sperren:
  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.