By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

Where should I SetFocus to resolve "can't disable a control when it has the focus"

P: 65
I have been using the following code to cycle through a subform and disable all textboxes on a form. If a textbox on the form has the focus when this is run I get error "can't disable a control when it has the focus." When I add a line like "txtTest.SetFocus" where txtTest is a textbox on the main form I still get the same error. How do I resolve this issue? Also, please note that I disable all controls on the subform.

Expand|Select|Wrap|Line Numbers
  1. Private Sub EnableControls(ByRef frm As Form, ByRef CtrlTypeName As String, Enable As Boolean)
  2.   Dim nCount As Integer
  3.   For nCount = 0 To frm.Controls.Count - 1
  4.     If TypeName(frm.Controls(nCount)) = CtrlTypeName Then
  5.       frm.Controls(nCount).Enabled = Enable
  6.     End If
  7.   Next
  8. End Sub
  9.  
thank you for your time
Aug 27 '07 #1
Share this Question
Share on Google+
8 Replies


JKing
Expert 100+
P: 1,206
Have you tried setting focus to the form itself?
Aug 27 '07 #2

FishVal
Expert 2.5K+
P: 2,653
Take a look at this thread
Another SetFocus Question - SetFocus from SubForm
Aug 27 '07 #3

P: 65
Take a look at this thread
Another SetFocus Question - SetFocus from SubForm
is that really the best workaround? to create an empty option group and set borderstyle = transparent?
Aug 30 '07 #4

FishVal
Expert 2.5K+
P: 2,653
is that really the best workaround? to create an empty option group and set borderstyle = transparent?
Another workaround may be to select current record before disabling last control
Expand|Select|Wrap|Line Numbers
  1. DoCmd.RunCommand acCmdSelectRecord 
  2.  
Aug 30 '07 #5

ADezii
Expert 5K+
P: 8,615
I have been using the following code to cycle through a subform and disable all textboxes on a form. If a textbox on the form has the focus when this is run I get error "can't disable a control when it has the focus." When I add a line like "txtTest.SetFocus" where txtTest is a textbox on the main form I still get the same error. How do I resolve this issue? Also, please note that I disable all controls on the subform.

Expand|Select|Wrap|Line Numbers
  1. Private Sub EnableControls(ByRef frm As Form, ByRef CtrlTypeName As String, Enable As Boolean)
  2.   Dim nCount As Integer
  3.   For nCount = 0 To frm.Controls.Count - 1
  4.     If TypeName(frm.Controls(nCount)) = CtrlTypeName Then
  5.       frm.Controls(nCount).Enabled = Enable
  6.     End If
  7.   Next
  8. End Sub
  9.  
thank you for your time
If you're disabling all Text Boxes on a Sub-Form, simply disable the Sub-Form Control itself as in:
Expand|Select|Wrap|Line Numbers
  1. Me!subfChild.Enabled = False
Aug 31 '07 #6

P: 65
If you're disabling all Text Boxes on a Sub-Form, simply disable the Sub-Form Control itself as in:
Expand|Select|Wrap|Line Numbers
  1. Me!subfChild.Enabled = False
this method doesn't shade disabled objects gray. i got a lot of feedback like "why can't i click on the text box"
Sep 10 '07 #7

ADezii
Expert 5K+
P: 8,615
this method doesn't shade disabled objects gray. i got a lot of feedback like "why can't i click on the text box"
You could 'mimic' the effect:
Expand|Select|Wrap|Line Numbers
  1. Dim ctl As Control
  2. Const conNormal = 1
  3.  
  4. Me!subfChild.Enabled = False
  5.  
  6. For Each ctl In Me!subfChild.Form.Controls
  7.   If ctl.ControlType = acTextBox Then
  8.     ctl.BackStyle = conNormal
  9.     ctl.BackColor = 12632256
  10.   End If
  11. Next
Sep 10 '07 #8

P: 1
Many thanks! and amazed that this riddle is over. I was looking for a solution that solves a hypothetical prob, where a form consists of 1 text control and a label and when the Label is pressed, the text control is disabled.

After all, if MS access is workable, it's going to have to stand up with these basic features. The solution you provided (DoCmd.RunCommand acCmdSelectRecord) even with a form that doesn't consist a dataset, works.

I was trying to achieve similar results via setting attempting to set the screen.activecontrol to the form object itself. Of course it didn't set the focus to the Form like I was hoping it would; And I was hoping not to have to settle for anything less, like setting focus to arbitrary controls, or as one suggested Screen.PreviousConntrol via PreviousControl.SetFocus because a) makes the code unnecessarily messy and b) you'll need to have at least one other control that the focus can be set to, which doesn't cater for the above prob.
@FishVal
Oct 26 '14 #9

Post your reply

Sign in to post your reply or Sign up for a free account.