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:
- Private Sub ContactMethod_AfterUpdate()
-
If ContactMethod = "email" Then EmailAddress.SetFocus
-
If ContactMethod = "phone" Then PhoneNumber.SetFocus
-
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)>