Search code examples
vbams-accesssetfocus

Is SetFocus a Function?


I saw this line of code txtNewCaption.SetFocus and was told that SetFocus is used as a sub here but it is also a function. I also read online that the difference between a sub and a function is that a sub doesn't return any value but a function returns a value. I couldn't imagine what kind of value SetFoucs could return, so I searched quite some articles online about SetFocus but none of them gave me an example of SetFocus used as a function to return a value. I am assuming there is something inaccurate or wrong with either my understanding or what I was told.

Could you please help me to clarify the confusion?

Thank you!


Solution

  • Well, in this case and example, .setFocus is not a sub, nor is it a function.

    A sub is a separate bit of code, you call it like

    Call MySub
    

    (or a shorter form skips the word "call" - it is optional) eg:

    MySub
    

    Or, if there are parameters, you might go:

    Call MySub(InvoiceNumber)
    

    In the case of a function? That is again a external bit of code. You can use functions that return a value and IGNORE that return value.

    So

    MyVar = MyFunction()
    

    And just like subs, the function can accept parameters:

    MyVar = MyFunction(InvoiceNumber)
    

    And as I noted, you can "ignore" the return value of a function like this:

    MyFuncton  InvoiceNumber
    

    If you write as per above, the you are passing the value, but the return value of the function is not "placed" or put into anything. so you can "use" a function that returns a value. You also skip use of the () around the value you pass.

    However, in the above two examples we are talking about VBA sub and functions YOU write.

    SomeTextBox.SetFocus is NOT sub, nor is it a function.

    What the above is called? It is called a Method of the object.

    So, when you use Access objects, say record Sets, controls etc? Well that "object" will have what we call properties (usually things that you can set or get a value from).

    So, for a text box, you have:

    MyControl.Value = "Hello"
    

    The above would set the value (text) of a text box to Hello. Thus ".value" is the property of that text box. It also happens to be the default property. So you can go:

    MyControl = "Hello"
    

    Now, if you go:

     Msgbox("Value of MyControl = " & MyControl.Value)
    

    So a property of a "thing" (a object) often can be set a value, and you can get (retrieve) a value. This is called a property.

    However, these objects also have what are called methods. Methods are a "action" and are similar to a sub/function (but YOU did not write that method).

    And such methods are "some code" that runs that is part of the object. So sometimes the difference between a property of a object, and a method of a object is "gray". Some methods can accept values and return values, but the distinction between the two (method and property) is that MORE then setting a value can occur with a method. (so it not just limited to get and set).

    A "method" can be thought of some "action" and code. Use of that Method tends to be assumed that some code will run. (and it not your code in most cases - it is "code" attached to the built in object.

    So

    MyTextBox.SetFocus
    

    It is a method of the text box control, or the given control in question. Not all controls necessary may have this "method".

    So .SetFocus is not sub, not a function, but is in fact a method of the control in question.