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.SetFocu s
Form_sfrmFieldService.dtAr rivalDate. Enabled = True
Form_sfrmFieldService.dtAr rivalDate. Locked = False
Form_sfrmFieldService.tmAr rivalTime. Enabled = True
Form_sfrmFieldService.tmAr rivalTime. Locked = False
Form_sfrmFieldService.dtCo mpleteDate .Enabled = True
Form_sfrmFieldService.dtCo mpleteDate .Locked = False
Form_sfrmFieldService.tmCo mpleteTime .Enabled = True
Form_sfrmFieldService.tmCo mpleteTime .Locked = False
Form_sfrmFieldService.intA ctionCode. Enabled = True
Form_sfrmFieldService.intA ctionCode. Locked = False
Form_sfrmFieldService.intA ctivityCod e.Enabled = True
Form_sfrmFieldService.intA ctivityCod e.Locked = False
Form_sfrmFieldService.intF aultCode.E nabled = True
Form_sfrmFieldService.intF aultCode.L ocked = False
Else
Me.cboSerialNumber.SetFocu s
Form_sfrmFieldService.dtAr rivalDate. Enabled = False
Form_sfrmFieldService.dtAr rivalDate. Locked = True
Form_sfrmFieldService.tmAr rivalTime. Enabled = False
Form_sfrmFieldService.tmAr rivalTime. Locked = True
Form_sfrmFieldService.dtCo mpleteDate .Enabled = False
Form_sfrmFieldService.dtCo mpleteDate .Locked = True
Form_sfrmFieldService.tmCo mpleteTime .Enabled = False
Form_sfrmFieldService.tmCo mpleteTime .Locked = True
Form_sfrmFieldService.intA ctionCode. Enabled = False
Form_sfrmFieldService.intA ctionCode. Locked = True
Form_sfrmFieldService.intA ctivityCod e.Enabled = False
Form_sfrmFieldService.intA ctivityCod e.Locked = True
Form_sfrmFieldService.intF aultCode.E nabled = False
Form_sfrmFieldService.intF aultCode.L ocked = True
End If
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.SetFocu
Form_sfrmFieldService.dtAr
Form_sfrmFieldService.dtAr
Form_sfrmFieldService.tmAr
Form_sfrmFieldService.tmAr
Form_sfrmFieldService.dtCo
Form_sfrmFieldService.dtCo
Form_sfrmFieldService.tmCo
Form_sfrmFieldService.tmCo
Form_sfrmFieldService.intA
Form_sfrmFieldService.intA
Form_sfrmFieldService.intA
Form_sfrmFieldService.intA
Form_sfrmFieldService.intF
Form_sfrmFieldService.intF
Else
Me.cboSerialNumber.SetFocu
Form_sfrmFieldService.dtAr
Form_sfrmFieldService.dtAr
Form_sfrmFieldService.tmAr
Form_sfrmFieldService.tmAr
Form_sfrmFieldService.dtCo
Form_sfrmFieldService.dtCo
Form_sfrmFieldService.tmCo
Form_sfrmFieldService.tmCo
Form_sfrmFieldService.intA
Form_sfrmFieldService.intA
Form_sfrmFieldService.intA
Form_sfrmFieldService.intA
Form_sfrmFieldService.intF
Form_sfrmFieldService.intF
End If
Disable your controls on the Focus event of the cboSerialNumber control
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...
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...
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...
ASKER
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?
What is going on with this program?
try the subform's events i can't think of enything else...
ASKER
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
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.