SetFocus does not work in Access form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Veeves
    New Member
    • Sep 2009
    • 5

    SetFocus does not work in Access form

    I have an unbound field on a form. In the onClick event I have code to populate a text field. I first setFocus to it and enable it. Both of these actions work. But when my code reaches the the line whete I set the textfieldname.t ext to a value, I get an error: You can`t reference a property or method unless the control has focus.

    My code is:
    Code:
    txtFileName.Enabled = True
    txtFileName.Visible = True
    txtFileName.SetFocus
    txtFileName.Text = "some value"
    The error occurs on the last line.
    Last edited by NeoPa; Sep 18 '09, 02:19 PM. Reason: Please use the [CODE] tags provided.
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #2
    Are you able to use the Value property instead? Also have you tried the syntax Me!txtFileName. SetFocus. I haven't done much with access so those might not work, but we do have a specific forum section for these types of questions so a moderator will probably move this topic over there.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by Veeves
      I have an unbound field on a form. In the onClick event I have code to populate a text field. I first setFocus to it and enable it. Both of these actions work. But when my code reaches the the line whete I set the textfieldname.t ext to a value, I get an error: You can`t reference a property or method unless the control has focus.

      My code is:
      txtFileName.Ena bled = True
      txtFileName.Vis ible = True
      txtFileName.Set Focus
      txtFileName.Tex t = "some value"

      The error occurs on the last line.
      Code:
      Dim txtB As TextBox
      
      Set txtB = Me![txtFileName]
      
      txtB.Enabled = True
      txtB.Visible = True
      txtB.SetFocus
      txtB.Text = "some value"

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32662

        #4
        I checked the help file & I can't see why that wouldn't work.

        Does this happen in all circumstances? perhaps there's something else going on we don't know about.

        Comment

        • Veeves
          New Member
          • Sep 2009
          • 5

          #5
          This code was working until I added a couple of queries and changed some code in a different function. I don`t get it.

          Comment

          • ajalwaysus
            Recognized Expert Contributor
            • Jul 2009
            • 266

            #6
            Does any of those changes have an effect on your current form? If so, setting focus should be the last thing you do, to allow for all the changes to happen.

            -AJ

            Comment

            • missinglinq
              Recognized Expert Specialist
              • Nov 2006
              • 3533

              #7
              What happens if you omit the last line of code, simply using
              Code:
              txtFileName.Enabled = True
              txtFileName.Visible = True
              txtFileName.SetFocus
              Is the cursor blinking in the textbox?

              Welcome to Bytes!

              Linq ;0)>

              BTW, your original code works just fine for me!

              Comment

              • Veeves
                New Member
                • Sep 2009
                • 5

                #8
                If I omit the last line: txtFileName.Tex t = "some value", then I avoid the error but don`t get the textbox filled with a value.

                I changed txtFileName.Tex t to txtFileName.Val ue, and this works but the textbox shows as empty instead of the text I want.

                I did some investigating and found that the problem starts when I delete all the records from the table which is the record source for the form using:
                CurrentDb().Exe cute "DELETE FROM Primary_Data"

                After running this the problems with SetFocus started.

                Comment

                • ajalwaysus
                  Recognized Expert Contributor
                  • Jul 2009
                  • 266

                  #9
                  try this:
                  Code:
                  Dim txtB As TextBox
                   
                  Set txtB = Me![txtFileName]
                   
                  txtB.Enabled = True
                  txtB.Visible = True
                  txtB.SetFocus
                  txtB.Value= "some value"
                  txtB.Requery
                  the requery should display the value after it's been assigned, if the value still exists.

                  -AJ

                  Comment

                  • missinglinq
                    Recognized Expert Specialist
                    • Nov 2006
                    • 3533

                    #10
                    Originally posted by Veeves
                    If I omit the last line: txtFileName.Tex t = "some value", then I avoid the error but don`t get the textbox filled with a value.
                    You didn't answer my question, though! If you omit the last line, then run the code (click the button,) does the textbox, in fact, receive the focus, i.e. does the cursor appear in the textbox?

                    Linq ;0)>

                    Comment

                    • Veeves
                      New Member
                      • Sep 2009
                      • 5

                      #11
                      If I omit the last line: txtFileName.Tex t = "some value", then the textbox does receive focus.

                      One time I stopped execution right after the textBox.SetFocu s. All the properties had a value of :Cannot access the property of a control that does not have focus (not exactly this message but very close), with one exception, the value property had a value and I was able to display it.

                      Comment

                      • Veeves
                        New Member
                        • Sep 2009
                        • 5

                        #12
                        Yes the textbox.requery does display the value. Thanks.

                        Comment

                        Working...