control variable

I usually choose a function unless I'm really sure I just need a sub
So do I. Both subs and functions can return multiple values when passed as byRef parameters (the default), but a function can also return a value - typically a boolean. For example can be used like this


if somefunction(a,b,c)=true then debug.print a, b, c

Code:
function somefunction(a as string, b as long, c as date) as boolean

    somefunction=false
    'do something to modify the values of a, b and c

    'if all values modified OK then somefunction=true

end function

I've used something like this to optimise the size of a container (H/W/D) for a given volume to fit on a pallet.
 
here is another Textbox calculator demo.
open form1.
post #15 of https://www.access-programmers.co.uk/forums/threads/calculator.325703/#post-1946293

for the AfterUpdate of those two textbox:
Code:
Public Function RetAmt()
    Dim tbx As Control
    Set tbx = Screen.ActiveControl
    Select Case tbx.Name
        Case "txtPayment"
            tbx.Parent!Receipt = Null
            tbx.Parent!txtReceipt = Null
            tbx.Parent!Amount = -tbx.Value
        Case "txtReceipt"
            tbx.Parent!Payment = Null
            tbx.Parent!txtPayment = Null
            tbx.Parent!Amount = tbx.Value
    End Select
End Function
 

Attachments

Last edited:
You can also use functions in a query, but not subs
 
  • Sub Procedures perform actions but do not return a value to the calling code.
  • Event-handling procedures are Sub procedures that execute in response to an event raised by user action or by an occurrence in a program.
  • Function Procedures return a value to the calling code. They can perform other actions before returning.
    Property Procedures return and assign values of properties on objects or modules.
 
Is there a disadvantage to using a function over a sub?
That should not be the question. The question should be what it should do. See and follow Command Query Separation:
The fundamental idea is that we should divide an object's methods into two sharply separated categories:
  • Queries: Return a result and do not change the observable state of the system (are free of side effects).
  • Commands: Change the state of a system but do notreturn a value.

So both, functions and subs, are procedures. Both can have parameters (which could be used for quasi return values if declared ByRef), but only functions can have a return value.

You can also use functions in a query, but not subs
That is correct, but then take care to use an error handler in these functions that doesn't reraise the error as a function used in that way is a so called 'entry function' where code execution starts and can't bubble errors up to any higher procedure. The same belongs to functions which are used directly in event properties.
 
Last edited:
  • Queries: Return a result and do not change the observable state of the system (are free of side effects).
Please clarify.

What about:
SQL:
UPDATE SomeTable
SET
  Field1 = NULL
WHERE id = 123
;

-- Or:
DELETE FROM SomeTable
WHERE DateField < #2025-01-01#
;
?
 
The article is not talking about queries in a database sense. It is a "query" where you ask something and get something back without doing an action. It is talking about types of code structure that can either not change the state of the application or change the state.

A function that does not alter the state but returns a value is a "query" type of command.
But a function can be both doing "command" or "query"
 
The article is not talking about queries in a database sense. It is a "query" where you ask something and get something back without doing an action. It is talking about types of code structure that can either not change the state of the application or change the state.
😬

Oops! Missed reading the article - thanks for the clarification! (y)
 

Users who are viewing this thread

Back
Top Bottom