SetFocus

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KMEscherich
    New Member
    • Jun 2007
    • 69

    SetFocus

    Using Access '97 and newer versions as well.

    Hi there, am wondering if someone can please explain how and when you would use SetFocus when developing a form.

    Am also wondering if it is wise to have SetFocus on each of the controls in a form or just specific ones.

    Thank you VERY much for your assistance.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Originally posted by KMEscherich
    Using Access '97 and newer versions as well.

    Hi there, am wondering if someone can please explain how and when you would use SetFocus when developing a form.

    Am also wondering if it is wise to have SetFocus on each of the controls in a form or just specific ones.

    Thank you VERY much for your assistance.
    You only use setfocus if you want a certain object to gain control after a certain event. Otherwise you can just use tab order to provide natural flow.

    Comment

    • abolos
      New Member
      • Apr 2007
      • 65

      #3
      Originally posted by KMEscherich
      Using Access '97 and newer versions as well.

      Hi there, am wondering if someone can please explain how and when you would use SetFocus when developing a form.

      Am also wondering if it is wise to have SetFocus on each of the controls in a form or just specific ones.

      Thank you VERY much for your assistance.

      Suppose you have a combobox in the header of the form and you have many textboxes in the detail. If you want to have the combobox in the header to be selected when running the form, then you have to use the setfocus command.

      For the textboxes, whenever you want to use the value or the text that the textbox contains, then you must use the setfocus. ex. if using <SELECT ... WHERE textbox=??>, then you have to setfocus the textbox beforet the SQL statement.

      Abolos

      Comment

      • missinglinq
        Recognized Expert Specialist
        • Nov 2006
        • 3533

        #4
        Abolos, your response to this post runs the gamut from simply being confusing to being flat out wrong!

        Your reference to using setfocus in order to send the focus to a combobox in the header of the form sounds as if this is a special function of setfocus, when in fact, as Rabbit said, it is used to send focus to any control that can receive focus, using VBA code.

        While it is true that you have to set focus on a textbox before using the .text property of that control, you do not have to do this when you want to use the .value property! This, plus the fact that .value is the default property of textboxes, is the reason that the .text property is generally only used for a very limited purpose, when the contents of a textbox has to be accessed before the content has been saved. Because .value is the default property for textboxes, instead of coding, say

        MyVariable = MyTextBox.Value

        you can simply use

        MyVariable = MyTextBox.

        Lastly, you absolutely do not have to setfocus to a textbox before referring to it's value in a SQL statement! You simply have to use the proper syntax for referring to it.

        As Rabbit said "You only use setfocus if you want a certain object to gain control after a certain event. Otherwise you can just use tab order to provide natural flow."

        While you normally move from control to control according to how you've set up your Tab Order, there may be times when you want the order to change depending on , for instance, what is entered in a control. Say you have a contact form for customers who call a service hotline when they have problems. You may want a means of contacting them at a later date to see if their problem has been resolved, so you have a control named ContactMethod. You could use code like this:
        [CODE=vb]Private Sub ContactMethod_A fterUpdate()
        If ContactMethod = "email" Then EmailAddress.Se tFocus
        If ContactMethod = "phone" Then PhoneNumber.Set Focus
        End Sub[/CODE]If your CSR person fills in the ContactMethod with "phone" the focus moves automatically to the field where the phone number needs to be filled in; if "email" is filled in, the focus moves to the EmailAddress field so that this data can be entered. Notice that the contents of the textbox ContactMethod is referred to without using .Value and without setting focus to the textbox!

        Linq ;0)>

        Comment

        • abolos
          New Member
          • Apr 2007
          • 65

          #5
          Originally posted by missinglinq
          Abolos, your response to this post runs the gamut from simply being confusing to being flat out wrong!

          Your reference to using setfocus in order to send the focus to a combobox in the header of the form sounds as if this is a special function of setfocus, when in fact, as Rabbit said, it is used to send focus to any control that can receive focus, using VBA code.

          While it is true that you have to set focus on a textbox before using the .text property of that control, you do not have to do this when you want to use the .value property! This, plus the fact that .value is the default property of textboxes, is the reason that the .text property is generally only used for a very limited purpose, when the contents of a textbox has to be accessed before the content has been saved. Because .value is the default property for textboxes, instead of coding, say

          MyVariable = MyTextBox.Value

          you can simply use

          MyVariable = MyTextBox.

          Lastly, you absolutely do not have to setfocus to a textbox before referring to it's value in a SQL statement! You simply have to use the proper syntax for referring to it.

          As Rabbit said "You only use setfocus if you want a certain object to gain control after a certain event. Otherwise you can just use tab order to provide natural flow."

          While you normally move from control to control according to how you've set up your Tab Order, there may be times when you want the order to change depending on , for instance, what is entered in a control. Say you have a contact form for customers who call a service hotline when they have problems. You may want a means of contacting them at a later date to see if their problem has been resolved, so you have a control named ContactMethod. You could use code like this:
          [CODE=vb]Private Sub ContactMethod_A fterUpdate()
          If ContactMethod = "email" Then EmailAddress.Se tFocus
          If ContactMethod = "phone" Then PhoneNumber.Set Focus
          End Sub[/CODE]If your CSR person fills in the ContactMethod with "phone" the focus moves automatically to the field where the phone number needs to be filled in; if "email" is filled in, the focus moves to the EmailAddress field so that this data can be entered. Notice that the contents of the textbox ContactMethod is referred to without using .Value and without setting focus to the textbox!

          Linq ;0)>

          Thanks linq for the explanation and sorry for making things this way.

          Comment

          • missinglinq
            Recognized Expert Specialist
            • Nov 2006
            • 3533

            #6
            Just trying to help you as well as the original poster!

            Linq ;0)>

            Comment

            Working...