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

SetFocus

P: 69
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.
Aug 10 '07 #1
Share this Question
Share on Google+
5 Replies


Rabbit
Expert Mod 10K+
P: 12,389
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.
Aug 10 '07 #2

P: 65
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
Aug 11 '07 #3

missinglinq
Expert 2.5K+
P: 3,532
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub ContactMethod_AfterUpdate()
  2.   If ContactMethod = "email" Then EmailAddress.SetFocus
  3.   If ContactMethod = "phone" Then PhoneNumber.SetFocus
  4. End Sub
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)>
Aug 11 '07 #4

P: 65
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:
Expand|Select|Wrap|Line Numbers
  1. Private Sub ContactMethod_AfterUpdate()
  2.   If ContactMethod = "email" Then EmailAddress.SetFocus
  3.   If ContactMethod = "phone" Then PhoneNumber.SetFocus
  4. End Sub
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.
Aug 13 '07 #5

missinglinq
Expert 2.5K+
P: 3,532
Just trying to help you as well as the original poster!

Linq ;0)>
Aug 13 '07 #6

Post your reply

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