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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • freeskier
    New Member
    • Oct 2006
    • 65

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

    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.SetFoc us" 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.

    Code:
    Private Sub EnableControls(ByRef frm As Form, ByRef CtrlTypeName As String, Enable As Boolean)
      Dim nCount As Integer
      For nCount = 0 To frm.Controls.Count - 1
        If TypeName(frm.Controls(nCount)) = CtrlTypeName Then
          frm.Controls(nCount).Enabled = Enable
        End If
      Next
    End Sub
    thank you for your time
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Have you tried setting focus to the form itself?

    Comment

    • FishVal
      Recognized Expert Specialist
      • Jun 2007
      • 2656

      #3
      Take a look at this thread
      Another SetFocus Question - SetFocus from SubForm

      Comment

      • freeskier
        New Member
        • Oct 2006
        • 65

        #4
        Originally posted by FishVal
        is that really the best workaround? to create an empty option group and set borderstyle = transparent?

        Comment

        • FishVal
          Recognized Expert Specialist
          • Jun 2007
          • 2656

          #5
          Originally posted by freeskier
          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
          Code:
          DoCmd.RunCommand acCmdSelectRecord

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by freeskier
            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.SetFoc us" 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.

            Code:
            Private Sub EnableControls(ByRef frm As Form, ByRef CtrlTypeName As String, Enable As Boolean)
              Dim nCount As Integer
              For nCount = 0 To frm.Controls.Count - 1
                If TypeName(frm.Controls(nCount)) = CtrlTypeName Then
                  frm.Controls(nCount).Enabled = Enable
                End If
              Next
            End Sub
            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:
            [CODE=vb]Me!subfChild.En abled = False[/CODE]

            Comment

            • freeskier
              New Member
              • Oct 2006
              • 65

              #7
              Originally posted by ADezii
              If you're disabling all Text Boxes on a Sub-Form, simply disable the Sub-Form Control itself as in:
              [CODE=vb]Me!subfChild.En abled = False[/CODE]
              this method doesn't shade disabled objects gray. i got a lot of feedback like "why can't i click on the text box"

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                Originally posted by freeskier
                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:
                [CODE=vb]Dim ctl As Control
                Const conNormal = 1

                Me!subfChild.En abled = False

                For Each ctl In Me!subfChild.Fo rm.Controls
                If ctl.ControlType = acTextBox Then
                ctl.BackStyle = conNormal
                ctl.BackColor = 12632256
                End If
                Next[/CODE]

                Comment

                • TerryNah
                  New Member
                  • Oct 2014
                  • 1

                  #9
                  Thanks!

                  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.RunComma nd acCmdSelectReco rd) 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.activeco ntrol 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.Previous Conntrol 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.
                  Originally posted by FishVal
                  Another workaround may be to select current record before disabling last control
                  Code:
                  DoCmd.RunCommand acCmdSelectRecord

                  Comment

                  Working...